|
| 1 | +on: |
| 2 | + workflow_call: |
| 3 | + inputs: |
| 4 | + account: |
| 5 | + description: The AWS account being deployed |
| 6 | + type: string |
| 7 | + required: true |
| 8 | + workspace: |
| 9 | + description: The Terraform workspace being deployed |
| 10 | + type: string |
| 11 | + required: true |
| 12 | + scope: |
| 13 | + description: The Terraform scope being deployed |
| 14 | + type: string |
| 15 | + required: true |
| 16 | + |
| 17 | +permissions: |
| 18 | + id-token: write |
| 19 | + contents: read |
| 20 | + actions: write |
| 21 | + |
| 22 | +env: |
| 23 | + ACCOUNT: ${{ inputs.account }} |
| 24 | + WORKSPACE: ${{ inputs.workspace }} |
| 25 | + CACHE_NAME: ${{ inputs.workspace }}-${{ inputs.account }}-${{ inputs.scope }} |
| 26 | + SCOPE: ${{ inputs.scope }} |
| 27 | + CI_ROLE_NAME: ${{ secrets.CI_ROLE_NAME }} |
| 28 | + |
| 29 | +jobs: |
| 30 | + parse-secrets: |
| 31 | + runs-on: [self-hosted, ci] |
| 32 | + steps: |
| 33 | + - id: parse-secrets |
| 34 | + run: | |
| 35 | + echo "::add-mask::${{ secrets.CI_ROLE_NAME }}" |
| 36 | +
|
| 37 | + get-branch-from-workflow-file: |
| 38 | + runs-on: [self-hosted, ci] |
| 39 | + needs: [parse-secrets] |
| 40 | + outputs: |
| 41 | + branch_name: ${{ steps.get_branch.outputs.branch_name }} |
| 42 | + steps: |
| 43 | + - id: get_branch |
| 44 | + run: | |
| 45 | + workflow_ref=${{ github.workflow_ref }} |
| 46 | + branch_name=${workflow_ref#*refs/heads/} |
| 47 | + branch_name=${branch_name#*refs/tags/} |
| 48 | + echo "branch_name=${branch_name}" >> $GITHUB_OUTPUT |
| 49 | +
|
| 50 | + build: |
| 51 | + runs-on: [self-hosted, ci] |
| 52 | + needs: get-branch-from-workflow-file |
| 53 | + steps: |
| 54 | + - uses: actions/checkout@v4 |
| 55 | + with: |
| 56 | + ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }} |
| 57 | + - if: ${{ env.SCOPE == 'per_workspace'}} |
| 58 | + uses: ./.github/actions/make/ |
| 59 | + with: |
| 60 | + command: build |
| 61 | + save-to-cache: "true" |
| 62 | + restore-from-cache: "false" |
| 63 | + cache-suffix: ${{ env.CACHE_NAME }} |
| 64 | + - if: ${{ env.SCOPE != 'per_workspace'}} |
| 65 | + uses: ./.github/actions/make/ |
| 66 | + with: |
| 67 | + command: poetry--update |
| 68 | + save-to-cache: "true" |
| 69 | + restore-from-cache: "false" |
| 70 | + cache-suffix: ${{ env.CACHE_NAME }} |
| 71 | + |
| 72 | + terraform--init: |
| 73 | + needs: [get-branch-from-workflow-file, build] |
| 74 | + runs-on: [self-hosted, ci] |
| 75 | + steps: |
| 76 | + - uses: actions/checkout@v4 |
| 77 | + with: |
| 78 | + ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }} |
| 79 | + - uses: ./.github/actions/terraform/ |
| 80 | + with: |
| 81 | + command: init |
| 82 | + account: ${{ env.ACCOUNT }} |
| 83 | + workspace: ${{ env.WORKSPACE }} |
| 84 | + scope: ${{ env.SCOPE }} |
| 85 | + restore-from-cache: "true" |
| 86 | + save-to-cache: "true" |
| 87 | + cache-suffix: ${{ env.CACHE_NAME }} |
| 88 | + |
| 89 | + terraform--plan: |
| 90 | + needs: [get-branch-from-workflow-file, terraform--init] |
| 91 | + runs-on: [self-hosted, ci] |
| 92 | + steps: |
| 93 | + - uses: actions/checkout@v4 |
| 94 | + with: |
| 95 | + ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }} |
| 96 | + - uses: ./.github/actions/terraform/ |
| 97 | + with: |
| 98 | + command: plan |
| 99 | + account: ${{ env.ACCOUNT }} |
| 100 | + workspace: ${{ env.WORKSPACE }} |
| 101 | + scope: ${{ env.SCOPE }} |
| 102 | + restore-from-cache: "true" |
| 103 | + save-to-cache: "true" |
| 104 | + cache-suffix: ${{ env.CACHE_NAME }} |
| 105 | + |
| 106 | + terraform--apply: |
| 107 | + needs: [get-branch-from-workflow-file, terraform--plan] |
| 108 | + environment: ${{ inputs.account }} |
| 109 | + runs-on: [self-hosted, ci] |
| 110 | + steps: |
| 111 | + - uses: actions/checkout@v4 |
| 112 | + with: |
| 113 | + ref: ${{ needs.get-branch-from-workflow-file.outputs.branch_name }} |
| 114 | + - uses: ./.github/actions/terraform/ |
| 115 | + with: |
| 116 | + command: apply |
| 117 | + account: ${{ env.ACCOUNT }} |
| 118 | + workspace: ${{ env.WORKSPACE }} |
| 119 | + scope: ${{ env.SCOPE }} |
| 120 | + restore-from-cache: "true" |
| 121 | + save-to-cache: "true" |
| 122 | + cache-suffix: ${{ env.CACHE_NAME }} |
| 123 | + |
| 124 | + set-success: |
| 125 | + name: Set Success |
| 126 | + needs: [terraform--apply] |
| 127 | + runs-on: [self-hosted, ci] |
| 128 | + steps: |
| 129 | + - name: Set success env var |
| 130 | + run: echo "success" |
| 131 | + outputs: |
| 132 | + success: "succeeded" |
| 133 | + |
| 134 | + message-slack: |
| 135 | + name: Notify slack of deployment |
| 136 | + needs: [get-branch-from-workflow-file, set-success] |
| 137 | + if: always() |
| 138 | + runs-on: [self-hosted, ci] |
| 139 | + |
| 140 | + steps: |
| 141 | + - name: Catch failed steps |
| 142 | + id: catch-failed-step |
| 143 | + uses: ./.github/actions/catch-failed-step |
| 144 | + - name: Send job result to slack |
| 145 | + id: slack |
| 146 | + |
| 147 | + with: |
| 148 | + webhook-type: webhook-trigger |
| 149 | + payload: | |
| 150 | + { |
| 151 | + "action_url": "${{ format('{0}/{1}/actions/runs/{2}/attempts/{3}', github.server_url, github.repository, github.run_id, github.run_attempt) }}", |
| 152 | + "attempt": ${{ github.run_attempt }}, |
| 153 | + "account": "${{ env.ACCOUNT }}", |
| 154 | + "workspace": "${{ env.WORKSPACE }}", |
| 155 | + "caller": "${{ github.triggering_actor }}", |
| 156 | + "scope": "${{ env.SCOPE }}", |
| 157 | + "branch": "${{ needs.get-branch-from-workflow-file.outputs.branch_name }}", |
| 158 | + "result": "${{ needs.set-success.outputs.success && needs.set-success.outputs.success || 'failed' }}", |
| 159 | + "result_detail": "${{ needs.set-success.outputs.success && 'None' || steps.catch-failed-step.outputs.failed-step-name }}" |
| 160 | + } |
| 161 | + env: |
| 162 | + SLACK_WEBHOOK_URL: ${{ secrets.DEPLOY_ENV_SLACK_HOOK_URL }} |
0 commit comments