Deploy version to propreate channel in any environment #58
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy version to propreate channel in any environment | |
on: | |
workflow_dispatch: | |
inputs: | |
version: | |
description: 'Version to deploy' | |
required: true | |
default: 'version without leading v, for example: 1.0.0 and 2.3.0-feature1.5' | |
env: | |
type: choice | |
description: Environment to deploy to | |
options: | |
- dev | |
- test | |
- prod | |
default: 'dev' | |
env: | |
project-directory: ./ | |
jobs: | |
deploy: | |
name: Deploy | |
runs-on: ubuntu-latest | |
steps: | |
- name: Info | |
run: | | |
echo "version: ${{ github.event.inputs.version }}" | |
echo "env: ${{ github.event.inputs.env }}" | |
- uses: actions/checkout@v2 | |
with: | |
# set ref with input, add prefix 'refs/tags/' if it is a tag | |
ref: ${{format('v{0}', github.event.inputs.version)}} | |
- name: git configure | |
run: | | |
git config --global url."https://${{ secrets.GITHUB_TOKEN }}@github.com/".insteadOf ssh://[email protected] | |
- name: Use Node.js 18.x | |
uses: actions/setup-node@v1 | |
with: | |
node-version: '18.x' | |
- name: get-npm-version | |
id: package-version | |
uses: martinbeentjes/npm-get-version-action@master | |
- name: Install kustomize | |
run: curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash | |
- name: Calculate the channel | |
id: channel | |
run: | | |
echo "::debug:: begin preparing..."; | |
echo "::debug:: version to deploy: $VERSION_TO_DEPLOY"; | |
echo "::debug:: env to deploy: ${{ github.event.inputs.env }}"; | |
ls -a | |
node <<EOF >> $GITHUB_OUTPUT | |
const releaseJson = require("./.releaserc.json"); | |
const version = 'v' + process.env.VERSION_TO_DEPLOY | |
const m = version.match(/^v\d+\.\d+.\d+(-([\w-\/\.]+)\.\d+)?$/) | |
let channel; | |
let s3BucketSecretName; | |
let cdnIdSecretName; | |
if(!m){ | |
throw '::error:: wrong version:' + version; | |
}else{ | |
if(!m[1]){ | |
channel = "master"; | |
}else{ | |
const releaseName = m[2]; | |
channel = releaseJson.branches.reduce((a,c) => a || (c.prerelease === releaseName && c.channel) || (c.name === releaseName && c.channel), false); | |
} | |
} | |
console.log("CHANNEL=" + channel); | |
EOF | |
env: | |
VERSION_TO_DEPLOY: ${{ github.event.inputs.version }} | |
ENV_TO_DEPLOY: ${{ github.event.inputs.env }} | |
- name: Print channel | |
run: echo ${{ steps.channel.outputs.CHANNEL }} | |
- name: Run kustomize | |
run: (cd ${{ steps.channel.outputs.CHANNEL == 'master' && 'deployment' || format('deployment-{0}',steps.channel.outputs.CHANNEL) }}/base && ../../kustomize edit set image greenstand/${{ github.event.repository.name }}:${{github.event.inputs.version}}) | |
- name: Resolve the secrets name | |
run: echo "SECRET_KEY=$( echo `node -e 'console.log({dev:"DEV_DIGITALOCEAN_TOKEN",prod:"DIGITALOCEAN_PRODUCTION_TOKEN",test:"TEST_DIGITALOCEAN_TOKEN"}[process.env.ENV_TO_DEPLOY])'`)" >> $GITHUB_OUTPUT | |
id: do_secrets_name | |
env: | |
ENV_TO_DEPLOY: ${{ github.event.inputs.env }} | |
- name: Print digitalocean token name | |
run: echo ${{ steps.do_secrets_name.outputs.SECRET_KEY }} | |
- name: Install doctl for kubernetes | |
uses: digitalocean/action-doctl@v2 | |
with: | |
token: ${{ secrets[ steps.do_secrets_name.outputs.SECRET_KEY ] }} | |
- name: Resolve cluster name | |
run: echo "SECRET_KEY=$( echo `node -e 'console.log({dev:"DEV_CLUSTER_NAME",prod:"PRODUCTION_CLUSTER_NAME",test:"TEST_CLUSTER_NAME"}[process.env.ENV_TO_DEPLOY])'`)" >> $GITHUB_OUTPUT | |
id: cluster_secrets_name | |
env: | |
ENV_TO_DEPLOY: ${{ github.event.inputs.env }} | |
- name: Print cluster name | |
run: echo ${{ steps.cluster_secrets_name.outputs.SECRET_KEY }} | |
- name: Save DigitalOcean kubeconfig | |
run: doctl kubernetes cluster kubeconfig save $CLUSTER_NAME | |
env: | |
CLUSTER_NAME: ${{ secrets[ steps.cluster_secrets_name.outputs.SECRET_KEY ] }} | |
- name: Resolve overlay name | |
run: echo "OVERLAY_FOLDER=$( echo `node -e 'console.log({dev:"development",prod:"prod",test:"test"}[process.env.ENV_TO_DEPLOY])'`)" >> $GITHUB_OUTPUT | |
env: | |
ENV_TO_DEPLOY: ${{ github.event.inputs.env }} | |
id: overlay_folder | |
- name: Print overlay folder | |
run: echo ${{ steps.overlay_folder.outputs.OVERLAY_FOLDER }} | |
- name: Update kubernetes resources | |
run: kustomize build ${{ steps.channel.outputs.CHANNEL == 'master' && 'deployment' || format('deployment-{0}',steps.channel.outputs.CHANNEL) }}/overlays/${{ steps.overlay_folder.outputs.OVERLAY_FOLDER}} | kubectl apply -n ${{ secrets.K8S_NAMESPACE }} --wait -f - |