|
| 1 | +name: Deploy to production |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - releases/v1 |
| 7 | + types: |
| 8 | + - opened |
| 9 | + - synchronize |
| 10 | + - reopened |
| 11 | + - closed |
| 12 | + |
| 13 | +env: |
| 14 | + KUBE_CONFIG_DATA: ${{ secrets.kubeconfig_data_prod }} |
| 15 | + # For hub CLI tool |
| 16 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 17 | + |
| 18 | +jobs: |
| 19 | + deploy-prod: |
| 20 | + if: github.event.pull_request.merged |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + steps: |
| 24 | + - uses: actions/checkout@v2 |
| 25 | + with: |
| 26 | + fetch-depth: 0 # Get full history for tag lookup |
| 27 | + - uses: azure/setup-kubectl@v1 |
| 28 | + with: |
| 29 | + version: v1.18.0 |
| 30 | + |
| 31 | + - name: Add kubeconfig to environment |
| 32 | + run: | |
| 33 | + set -e |
| 34 | + test -e ~/.kube || mkdir ~/.kube |
| 35 | + cat <<EOF > ~/.kube/config |
| 36 | + $(printf '%s' "$KUBE_CONFIG_DATA" | base64 -d) |
| 37 | + EOF |
| 38 | +
|
| 39 | + - name: Get release version |
| 40 | + run: | |
| 41 | + # Find tag ref |
| 42 | + git fetch --tags |
| 43 | + ref=$(git describe --abbrev=0 --match 'v*' --exclude 'v*-rc.*' --tags ${{ github.sha }}) |
| 44 | + echo "::set-env name=DEPLOY_REF::$ref" |
| 45 | +
|
| 46 | + if [ -z "$ref" ]; then |
| 47 | + echo error: could not find release tag for commit $head_sha |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +
|
| 51 | + - name: Get deployment information |
| 52 | + run: | |
| 53 | + hub api /repos/${{ github.repository }}/deployments?ref=$DEPLOY_REF -X GET | jq .[0] > /tmp/deployment.json |
| 54 | +
|
| 55 | + - name: Mark deployment as started |
| 56 | + run: | |
| 57 | + hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \ |
| 58 | + -X POST \ |
| 59 | + -H "Accept: application/json, application/vnd.github.flash-preview+json" \ |
| 60 | + --input <(cat <<EOF |
| 61 | + { |
| 62 | + "state": "in_progress", |
| 63 | + "description": "Rolling out $DEPLOY_REF", |
| 64 | + "log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 65 | + } |
| 66 | + EOF) |
| 67 | +
|
| 68 | + - name: Get manifest archive |
| 69 | + run: | |
| 70 | + set -e |
| 71 | + tagname=${{ env.DEPLOY_REF }} |
| 72 | + arc_fname=deploy-$tagname.zip |
| 73 | + arc_url=https://github.com/${{ github.repository }}/releases/download/$tagname/$arc_fname |
| 74 | + curl -fLO "$arc_url" |
| 75 | + unzip $arc_fname |
| 76 | +
|
| 77 | + - name: Deploy to prod |
| 78 | + run: | |
| 79 | + set -e |
| 80 | + echo deploying CHIME release ${{ env.DEPLOY_REF }} |
| 81 | + find deploy-${{ env.DEPLOY_REF }}/ -type f -print0 | xargs -0 cat | kubectl diff -f - || true |
| 82 | + kubectl apply -Rf deploy-${{ env.DEPLOY_REF }} |
| 83 | +
|
| 84 | + - name: Wait for deployment to complete |
| 85 | + run: | |
| 86 | + kubectl -n chime rollout status deployment.v1.apps/chime |
| 87 | + kubectl -n chime get deployment chime -o yaml |
| 88 | +
|
| 89 | + - name: Mark deployment as failed |
| 90 | + if: failure() |
| 91 | + run: | |
| 92 | + hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \ |
| 93 | + -X POST \ |
| 94 | + -H "Accept: application/json, application/vnd.github.flash-preview+json" \ |
| 95 | + --input <(cat <<EOF |
| 96 | + { |
| 97 | + "state": "failure", |
| 98 | + "description": "Error in job deploy-prod", |
| 99 | + "log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 100 | + } |
| 101 | + EOF) |
| 102 | +
|
| 103 | + - name: Mark deployment completed |
| 104 | + run: | |
| 105 | + hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \ |
| 106 | + -X POST \ |
| 107 | + -H "Accept: application/json, application/vnd.github.flash-preview+json" \ |
| 108 | + --input <(cat <<EOF |
| 109 | + { |
| 110 | + "state": "success", |
| 111 | + "description": "$DEPLOY_REF deployed", |
| 112 | + "log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 113 | + } |
| 114 | + EOF) |
0 commit comments