|
| 1 | +name: Apply Account Terraform |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + base_sha: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + head_sha: |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + environment: |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + state_bucket_environment: |
| 16 | + required: false |
| 17 | + type: string |
| 18 | + default: "" |
| 19 | + artifact_name: |
| 20 | + required: true |
| 21 | + type: string |
| 22 | + workflow_dispatch: |
| 23 | + inputs: |
| 24 | + environment: |
| 25 | + description: Select AWS account environment |
| 26 | + required: true |
| 27 | + type: choice |
| 28 | + options: |
| 29 | + - dev |
| 30 | + - preprod |
| 31 | + - prod |
| 32 | + state_bucket_environment: |
| 33 | + description: Override state bucket environment |
| 34 | + required: false |
| 35 | + type: string |
| 36 | + default: "" |
| 37 | + base_sha: |
| 38 | + description: Base commit SHA for diff checks. Leave blank to use previous commit. |
| 39 | + required: false |
| 40 | + type: string |
| 41 | + default: "" |
| 42 | + head_sha: |
| 43 | + description: Head commit SHA for diff checks. Leave blank to use current commit. |
| 44 | + required: false |
| 45 | + type: string |
| 46 | + default: "" |
| 47 | + artifact_name: |
| 48 | + description: Optional Terraform plan artifact name |
| 49 | + required: false |
| 50 | + type: string |
| 51 | + default: "" |
| 52 | + |
| 53 | +run-name: Apply Account Terraform - ${{ inputs.environment }} |
| 54 | + |
| 55 | +concurrency: |
| 56 | + group: account-terraform-${{ github.repository }}-${{ inputs.environment }} |
| 57 | + cancel-in-progress: false |
| 58 | + |
| 59 | +env: |
| 60 | + CONFIGURED_ACCOUNT_TERRAFORM_STATE_BUCKET: ${{ vars.ACCOUNT_TERRAFORM_STATE_BUCKET || (inputs.environment == 'dev' && 'immunisation-terraform-state-files' || '') }} |
| 61 | + ACCOUNT_TERRAFORM_STATE_ENVIRONMENT: ${{ inputs.state_bucket_environment }} |
| 62 | + ACCOUNT_TERRAFORM_ARTIFACT_NAME: ${{ inputs.artifact_name || format('{0}-account-tfplan-{1}', inputs.environment, github.run_attempt) }} |
| 63 | + ACCOUNT_TERRAFORM_VERSION: "1.12.2" |
| 64 | + |
| 65 | +jobs: |
| 66 | + account-terraform-plan: |
| 67 | + permissions: |
| 68 | + id-token: write |
| 69 | + contents: read |
| 70 | + attestations: write |
| 71 | + artifact-metadata: write |
| 72 | + runs-on: ubuntu-latest |
| 73 | + timeout-minutes: 30 |
| 74 | + environment: |
| 75 | + name: ${{ inputs.environment }} |
| 76 | + env: |
| 77 | + ACCOUNT_TERRAFORM_BASE_SHA: ${{ inputs.base_sha }} |
| 78 | + ACCOUNT_TERRAFORM_HEAD_SHA: ${{ inputs.head_sha || github.sha }} |
| 79 | + ACCOUNT_TERRAFORM_ENVIRONMENT: ${{ inputs.environment }} |
| 80 | + outputs: |
| 81 | + account_infra_changed: ${{ steps.diff.outputs.account_infra_changed }} |
| 82 | + plan_sha: ${{ env.ACCOUNT_TERRAFORM_HEAD_SHA }} |
| 83 | + steps: |
| 84 | + - name: Checkout |
| 85 | + uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98 |
| 86 | + with: |
| 87 | + fetch-depth: 0 |
| 88 | + |
| 89 | + - name: Detect account terraform changes |
| 90 | + id: diff |
| 91 | + run: | |
| 92 | + base_sha="$ACCOUNT_TERRAFORM_BASE_SHA" |
| 93 | + head_sha="$ACCOUNT_TERRAFORM_HEAD_SHA" |
| 94 | +
|
| 95 | + if [[ -z "$base_sha" || "$base_sha" == "0000000000000000000000000000000000000000" ]]; then |
| 96 | + base_sha=$(git rev-parse HEAD~1) |
| 97 | + fi |
| 98 | +
|
| 99 | + for sha_name in base_sha head_sha; do |
| 100 | + if [[ ! "${!sha_name}" =~ ^[0-9a-f]{40}$ ]]; then |
| 101 | + echo "Invalid $sha_name: ${!sha_name}" >&2 |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + done |
| 105 | +
|
| 106 | + account_changed_files=$(git diff --name-only "$base_sha" "$head_sha" -- infrastructure/account) |
| 107 | + if [ -n "$account_changed_files" ]; then |
| 108 | + echo "changes detected in files:" |
| 109 | + printf '%s\n' "$account_changed_files" |
| 110 | + fi |
| 111 | + echo "account_infra_changed=$( [ -n "$account_changed_files" ] && echo true || echo false )" >> "$GITHUB_OUTPUT" |
| 112 | +
|
| 113 | + - name: Connect to AWS |
| 114 | + if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} |
| 115 | + uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7 |
| 116 | + with: |
| 117 | + aws-region: eu-west-2 |
| 118 | + role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/auto-ops |
| 119 | + role-session-name: ${{ format('github-actions-{0}-{1}-{2}', github.run_id, github.run_attempt, github.job) }} |
| 120 | + |
| 121 | + - uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 |
| 122 | + if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} |
| 123 | + with: |
| 124 | + terraform_version: ${{ env.ACCOUNT_TERRAFORM_VERSION }} |
| 125 | + |
| 126 | + - name: Resolve account terraform state bucket |
| 127 | + id: account-state-bucket |
| 128 | + if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} |
| 129 | + run: echo "bucket_name=$(bash ./utilities/scripts/resolve_account_terraform_state_bucket.sh)" >> "$GITHUB_OUTPUT" |
| 130 | + |
| 131 | + - name: Terraform Init (account) |
| 132 | + if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} |
| 133 | + working-directory: infrastructure/account |
| 134 | + env: |
| 135 | + ACCOUNT_TERRAFORM_BUCKET_NAME: ${{ steps.account-state-bucket.outputs.bucket_name }} |
| 136 | + run: make init ENVIRONMENT="$ACCOUNT_TERRAFORM_ENVIRONMENT" BUCKET_NAME="$ACCOUNT_TERRAFORM_BUCKET_NAME" |
| 137 | + |
| 138 | + - name: Terraform Plan (account) |
| 139 | + # Ignore cancellations to prevent Terraform from being killed while it holds a state lock |
| 140 | + # A stuck process can still be killed with the force-cancel API operation |
| 141 | + if: ${{ steps.diff.outputs.account_infra_changed == 'true' && !failure() }} |
| 142 | + working-directory: infrastructure/account |
| 143 | + run: make plan-ci ENVIRONMENT="$ACCOUNT_TERRAFORM_ENVIRONMENT" |
| 144 | + |
| 145 | + - name: Save Account Terraform Plan |
| 146 | + if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} |
| 147 | + uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f |
| 148 | + with: |
| 149 | + name: ${{ env.ACCOUNT_TERRAFORM_ARTIFACT_NAME }} |
| 150 | + path: infrastructure/account/tfplan |
| 151 | + |
| 152 | + - name: Attest Account Terraform Plan |
| 153 | + if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} |
| 154 | + uses: actions/attest@v4 |
| 155 | + with: |
| 156 | + subject-path: infrastructure/account/tfplan |
| 157 | + |
| 158 | + account-terraform-approval: |
| 159 | + permissions: {} |
| 160 | + needs: [account-terraform-plan] |
| 161 | + if: ${{ !cancelled() && needs.account-terraform-plan.result == 'success' && needs.account-terraform-plan.outputs.account_infra_changed == 'true' }} |
| 162 | + runs-on: ubuntu-latest |
| 163 | + environment: |
| 164 | + name: account-apply-${{ inputs.environment }} |
| 165 | + steps: |
| 166 | + - name: Await manual approval |
| 167 | + run: echo "Manual approval granted" |
| 168 | + |
| 169 | + account-terraform-apply: |
| 170 | + permissions: |
| 171 | + id-token: write |
| 172 | + contents: read |
| 173 | + attestations: read |
| 174 | + needs: [account-terraform-plan, account-terraform-approval] |
| 175 | + if: ${{ !cancelled() && needs.account-terraform-plan.result == 'success' && needs.account-terraform-plan.outputs.account_infra_changed == 'true' && needs.account-terraform-approval.result == 'success' }} |
| 176 | + runs-on: ubuntu-latest |
| 177 | + timeout-minutes: 30 |
| 178 | + environment: |
| 179 | + name: ${{ inputs.environment }} |
| 180 | + env: |
| 181 | + ACCOUNT_TERRAFORM_ENVIRONMENT: ${{ inputs.environment }} |
| 182 | + steps: |
| 183 | + - name: Checkout |
| 184 | + uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98 |
| 185 | + with: |
| 186 | + ref: ${{ needs.account-terraform-plan.outputs.plan_sha }} |
| 187 | + |
| 188 | + - name: Connect to AWS |
| 189 | + uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7 |
| 190 | + with: |
| 191 | + aws-region: eu-west-2 |
| 192 | + role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/auto-ops |
| 193 | + role-session-name: ${{ format('github-actions-{0}-{1}-{2}', github.run_id, github.run_attempt, github.job) }} |
| 194 | + |
| 195 | + - uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 |
| 196 | + with: |
| 197 | + terraform_version: ${{ env.ACCOUNT_TERRAFORM_VERSION }} |
| 198 | + |
| 199 | + - name: Resolve account terraform state bucket |
| 200 | + id: account-state-bucket |
| 201 | + run: echo "bucket_name=$(bash ./utilities/scripts/resolve_account_terraform_state_bucket.sh)" >> "$GITHUB_OUTPUT" |
| 202 | + |
| 203 | + - name: Retrieve Account Terraform Plan |
| 204 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c |
| 205 | + with: |
| 206 | + name: ${{ env.ACCOUNT_TERRAFORM_ARTIFACT_NAME }} |
| 207 | + path: infrastructure/account |
| 208 | + |
| 209 | + - name: Verify Account Terraform Plan Attestation |
| 210 | + env: |
| 211 | + GH_TOKEN: ${{ github.token }} |
| 212 | + run: | |
| 213 | + gh attestation verify infrastructure/account/tfplan \ |
| 214 | + --repo "$GITHUB_REPOSITORY" \ |
| 215 | + --signer-workflow "$GITHUB_REPOSITORY/.github/workflows/account-terraform.yml" |
| 216 | +
|
| 217 | + - name: Terraform Init (account) |
| 218 | + working-directory: infrastructure/account |
| 219 | + env: |
| 220 | + ACCOUNT_TERRAFORM_BUCKET_NAME: ${{ steps.account-state-bucket.outputs.bucket_name }} |
| 221 | + run: make init ENVIRONMENT="$ACCOUNT_TERRAFORM_ENVIRONMENT" BUCKET_NAME="$ACCOUNT_TERRAFORM_BUCKET_NAME" |
| 222 | + |
| 223 | + - name: Terraform Apply (account) |
| 224 | + # Ignore cancellations to prevent Terraform from being killed while it holds a state lock |
| 225 | + # A stuck process can still be killed with the force-cancel API operation |
| 226 | + if: ${{ !failure() }} |
| 227 | + working-directory: infrastructure/account |
| 228 | + run: make apply-ci ENVIRONMENT="$ACCOUNT_TERRAFORM_ENVIRONMENT" |
0 commit comments