|
| 1 | +name: Promote Staging to Production |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + |
| 6 | +jobs: |
| 7 | + promote: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + env: |
| 10 | + GH_TOKEN: ${{ github.token }} |
| 11 | + steps: |
| 12 | + - name: Find latest successful staging deployment |
| 13 | + id: staging |
| 14 | + run: | |
| 15 | + DEPLOYMENT_JSON=$(gh api \ |
| 16 | + -H "Accept: application/vnd.github+json" \ |
| 17 | + repos/${{ github.repository }}/deployments \ |
| 18 | + --jq '.[] | select(.environment == "staging")' | head -n1) |
| 19 | +
|
| 20 | + echo "ref=$(echo "$DEPLOYMENT_JSON" | jq -r '.ref')" >> "$GITHUB_OUTPUT" |
| 21 | + echo "sha=$(echo "$DEPLOYMENT_JSON" | jq -r '.sha')" >> "$GITHUB_OUTPUT" |
| 22 | +
|
| 23 | + - name: Create production deployment |
| 24 | + id: prod_deploy |
| 25 | + run: | |
| 26 | + jq -n \ |
| 27 | + --arg ref "${{ steps.staging.outputs.ref }}" \ |
| 28 | + --arg sha "${{ steps.staging.outputs.sha }}" \ |
| 29 | + --arg environment "production" \ |
| 30 | + '{ref: $ref, sha: $sha, environment: $environment, auto_merge: false, required_contexts: []}' \ |
| 31 | + > payload.json |
| 32 | + |
| 33 | + DEPLOY_ID=$(gh api \ |
| 34 | + -X POST \ |
| 35 | + -H "Accept: application/vnd.github+json" \ |
| 36 | + repos/${{ github.repository }}/deployments \ |
| 37 | + --input payload.json \ |
| 38 | + --jq '.id') |
| 39 | +
|
| 40 | + echo "id=$DEPLOY_ID" >> "$GITHUB_OUTPUT" |
| 41 | +
|
| 42 | + - uses: ilyvion-contrib/ci-utils/setup-ssh@main |
| 43 | + with: |
| 44 | + ssh_known_hosts: ${{ secrets.deploy_known_hosts }} |
| 45 | + ssh_private_key: ${{ secrets.deploy_key }} |
| 46 | + ssh_host: ${{ secrets.deploy_target }} |
| 47 | + ssh_port: ${{ secrets.deploy_port }} |
| 48 | + |
| 49 | + - name: Trigger promote script on server |
| 50 | + run: | |
| 51 | + ssh -i ~/.ssh/id -p ${{ secrets.deploy_port }} \ |
| 52 | + ${{ secrets.deploy_user }}@${{ secrets.deploy_target }} \ |
| 53 | + 'cd ~/docker/${{ inputs.image }}/; sudo promote-staging-to-prod.sh' |
| 54 | +
|
| 55 | + - name: Mark deployment as success |
| 56 | + if: success() |
| 57 | + run: | |
| 58 | + gh api \ |
| 59 | + -X POST \ |
| 60 | + -H "Accept: application/vnd.github+json" \ |
| 61 | + repos/${{ github.repository }}/deployments/${{ steps.prod_deploy.outputs.id }}/statuses \ |
| 62 | + -f state="success" |
| 63 | +
|
| 64 | + - name: Mark deployment as failure |
| 65 | + if: failure() |
| 66 | + run: | |
| 67 | + gh api \ |
| 68 | + -X POST \ |
| 69 | + -H "Accept: application/vnd.github+json" \ |
| 70 | + repos/${{ github.repository }}/deployments/${{ steps.prod_deploy.outputs.id }}/statuses \ |
| 71 | + -f state="failure" |
0 commit comments