-
Notifications
You must be signed in to change notification settings - Fork 4
VED-1223: Update permissions to auto-ops role so the pipeline can apply terraform changes at account level #1384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thomas-Boyle
wants to merge
28
commits into
master
Choose a base branch
from
VED-1223-update-terraform-account-apply-pipeline-permissions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+270
−2
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
0561063
Add account-level Terraform workflow and integrate with existing CI/C…
Thomas-Boyle 283a3a1
Add bucket name validation to Makefile and improve bucket resolution …
Thomas-Boyle af6e116
Enhance bucket name resolution with validation in account-terraform w…
Thomas-Boyle 2b6d8b2
Add state bucket environment input to workflows and update bucket res…
Thomas-Boyle 5e2a874
Enhance Terraform workspace handling in workflows and bucket resoluti…
Thomas-Boyle c2d0559
Enhance bucket resolution script and workflow logging
Thomas-Boyle 2ce0cdc
Refactor bucket resolution script to output bucket name and exit
Thomas-Boyle 85b12bc
Refactor bucket resolution script to improve whitespace handling and …
Thomas-Boyle d93ba1b
Update account-terraform workflow to conditionally set bucket name ba…
Thomas-Boyle 90f4aec
Refactor account-terraform workflow to streamline job dependencies an…
Thomas-Boyle 7ed155f
Enhance account-terraform workflow to improve change detection logging
Thomas-Boyle a1fe5ea
Update pr-deploy-and-test workflow to enhance SHA handling during syn…
Thomas-Boyle 60f978a
Enhance account-terraform workflow to improve environment variable ha…
Thomas-Boyle 9860152
Enhance account-terraform workflow with improved SHA validation and e…
Thomas-Boyle a23387e
Refactor account-terraform workflow and bucket resolution script for …
Thomas-Boyle 1abcf47
Refactor account-terraform workflow and bucket resolution script for …
Thomas-Boyle 4caeddb
Refactor account-terraform workflow and bucket resolution script for …
Thomas-Boyle 4db413a
Merge branch 'master' into VED-1223-update-terraform-account-apply-pi…
Thomas-Boyle c61a3c4
Update base_sha logic in pr-deploy-and-test workflow to simplify SHA …
Thomas-Boyle 31c25f1
chore: empty commit
Thomas-Boyle aa15e8b
chore: empty commit
Thomas-Boyle 34d9440
Enhance account-terraform workflow by adding manual approval step bef…
Thomas-Boyle 99d20c0
Fix description in ECR lifecycle policy to include a period for consi…
Thomas-Boyle 33b2bed
Refactor account-terraform workflow to enhance input handling and art…
Thomas-Boyle 3dfb8a4
Merge branch 'master' into VED-1223-update-terraform-account-apply-pi…
Thomas-Boyle c37f9f9
Enhance account-terraform workflow and scripts for improved stability…
Thomas-Boyle 264bee5
Enhance account-terraform workflow with attestation support
Thomas-Boyle 6ccbb2d
chore: empty commit
Thomas-Boyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| name: Apply Account Terraform | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| base_sha: | ||
| required: true | ||
| type: string | ||
| head_sha: | ||
| required: true | ||
| type: string | ||
| environment: | ||
| required: true | ||
| type: string | ||
| state_bucket_environment: | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| artifact_name: | ||
| required: true | ||
| type: string | ||
| workflow_dispatch: | ||
| inputs: | ||
| environment: | ||
| description: Select AWS account environment | ||
| required: true | ||
| type: choice | ||
| options: | ||
| - dev | ||
| - preprod | ||
| - prod | ||
| state_bucket_environment: | ||
| description: Override state bucket environment | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| base_sha: | ||
| description: Base commit SHA for diff checks. Leave blank to use previous commit. | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| head_sha: | ||
| description: Head commit SHA for diff checks. Leave blank to use current commit. | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| artifact_name: | ||
| description: Optional Terraform plan artifact name | ||
| required: false | ||
| type: string | ||
| default: "" | ||
|
|
||
| run-name: Apply Account Terraform - ${{ inputs.environment }} | ||
|
|
||
| concurrency: | ||
| group: account-terraform-${{ github.repository }}-${{ inputs.environment }} | ||
| cancel-in-progress: false | ||
|
|
||
| env: | ||
| CONFIGURED_ACCOUNT_TERRAFORM_STATE_BUCKET: ${{ vars.ACCOUNT_TERRAFORM_STATE_BUCKET || (inputs.environment == 'dev' && 'immunisation-terraform-state-files' || '') }} | ||
| ACCOUNT_TERRAFORM_STATE_ENVIRONMENT: ${{ inputs.state_bucket_environment }} | ||
| ACCOUNT_TERRAFORM_ARTIFACT_NAME: ${{ inputs.artifact_name || format('{0}-account-tfplan-{1}', inputs.environment, github.run_attempt) }} | ||
| ACCOUNT_TERRAFORM_VERSION: "1.12.2" | ||
|
|
||
| jobs: | ||
| account-terraform-plan: | ||
avshetty1980 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| permissions: | ||
| id-token: write | ||
| contents: read | ||
| attestations: write | ||
| artifact-metadata: write | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 30 | ||
| environment: | ||
| name: ${{ inputs.environment }} | ||
| env: | ||
| ACCOUNT_TERRAFORM_BASE_SHA: ${{ inputs.base_sha }} | ||
| ACCOUNT_TERRAFORM_HEAD_SHA: ${{ inputs.head_sha || github.sha }} | ||
| ACCOUNT_TERRAFORM_ENVIRONMENT: ${{ inputs.environment }} | ||
| outputs: | ||
| account_infra_changed: ${{ steps.diff.outputs.account_infra_changed }} | ||
| plan_sha: ${{ env.ACCOUNT_TERRAFORM_HEAD_SHA }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Detect account terraform changes | ||
| id: diff | ||
| run: | | ||
| base_sha="$ACCOUNT_TERRAFORM_BASE_SHA" | ||
| head_sha="$ACCOUNT_TERRAFORM_HEAD_SHA" | ||
|
|
||
| if [[ -z "$base_sha" || "$base_sha" == "0000000000000000000000000000000000000000" ]]; then | ||
| base_sha=$(git rev-parse HEAD~1) | ||
| fi | ||
|
|
||
| for sha_name in base_sha head_sha; do | ||
| if [[ ! "${!sha_name}" =~ ^[0-9a-f]{40}$ ]]; then | ||
| echo "Invalid $sha_name: ${!sha_name}" >&2 | ||
| exit 1 | ||
| fi | ||
| done | ||
|
|
||
| account_changed_files=$(git diff --name-only "$base_sha" "$head_sha" -- infrastructure/account) | ||
| if [ -n "$account_changed_files" ]; then | ||
| echo "changes detected in files:" | ||
| printf '%s\n' "$account_changed_files" | ||
| fi | ||
| echo "account_infra_changed=$( [ -n "$account_changed_files" ] && echo true || echo false )" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Connect to AWS | ||
| if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} | ||
| uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7 | ||
| with: | ||
| aws-region: eu-west-2 | ||
| role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/auto-ops | ||
| role-session-name: ${{ format('github-actions-{0}-{1}-{2}', github.run_id, github.run_attempt, github.job) }} | ||
|
|
||
| - uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 | ||
| if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} | ||
| with: | ||
| terraform_version: ${{ env.ACCOUNT_TERRAFORM_VERSION }} | ||
|
|
||
avshetty1980 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - name: Resolve account terraform state bucket | ||
| id: account-state-bucket | ||
| if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} | ||
| run: echo "bucket_name=$(bash ./utilities/scripts/resolve_account_terraform_state_bucket.sh)" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Terraform Init (account) | ||
| if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} | ||
| working-directory: infrastructure/account | ||
| env: | ||
| ACCOUNT_TERRAFORM_BUCKET_NAME: ${{ steps.account-state-bucket.outputs.bucket_name }} | ||
| run: make init ENVIRONMENT="$ACCOUNT_TERRAFORM_ENVIRONMENT" BUCKET_NAME="$ACCOUNT_TERRAFORM_BUCKET_NAME" | ||
|
|
||
| - name: Terraform Plan (account) | ||
| # Ignore cancellations to prevent Terraform from being killed while it holds a state lock | ||
| # A stuck process can still be killed with the force-cancel API operation | ||
| if: ${{ steps.diff.outputs.account_infra_changed == 'true' && !failure() }} | ||
| working-directory: infrastructure/account | ||
| run: make plan-ci ENVIRONMENT="$ACCOUNT_TERRAFORM_ENVIRONMENT" | ||
|
|
||
| - name: Save Account Terraform Plan | ||
| if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} | ||
| uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f | ||
| with: | ||
| name: ${{ env.ACCOUNT_TERRAFORM_ARTIFACT_NAME }} | ||
| path: infrastructure/account/tfplan | ||
|
|
||
| - name: Attest Account Terraform Plan | ||
| if: ${{ steps.diff.outputs.account_infra_changed == 'true' }} | ||
| uses: actions/attest@v4 | ||
| with: | ||
| subject-path: infrastructure/account/tfplan | ||
|
|
||
| account-terraform-approval: | ||
| permissions: {} | ||
| needs: [account-terraform-plan] | ||
| if: ${{ !cancelled() && needs.account-terraform-plan.result == 'success' && needs.account-terraform-plan.outputs.account_infra_changed == 'true' }} | ||
| runs-on: ubuntu-latest | ||
| environment: | ||
| name: account-apply-${{ inputs.environment }} | ||
| steps: | ||
| - name: Await manual approval | ||
| run: echo "Manual approval granted" | ||
|
|
||
| account-terraform-apply: | ||
| permissions: | ||
| id-token: write | ||
| contents: read | ||
| attestations: read | ||
| needs: [account-terraform-plan, account-terraform-approval] | ||
| if: ${{ !cancelled() && needs.account-terraform-plan.result == 'success' && needs.account-terraform-plan.outputs.account_infra_changed == 'true' && needs.account-terraform-approval.result == 'success' }} | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 30 | ||
| environment: | ||
| name: ${{ inputs.environment }} | ||
| env: | ||
| ACCOUNT_TERRAFORM_ENVIRONMENT: ${{ inputs.environment }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98 | ||
| with: | ||
| ref: ${{ needs.account-terraform-plan.outputs.plan_sha }} | ||
|
|
||
Thomas-Boyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - name: Connect to AWS | ||
| uses: aws-actions/configure-aws-credentials@8df5847569e6427dd6c4fb1cf565c83acfa8afa7 | ||
| with: | ||
| aws-region: eu-west-2 | ||
| role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/auto-ops | ||
| role-session-name: ${{ format('github-actions-{0}-{1}-{2}', github.run_id, github.run_attempt, github.job) }} | ||
|
|
||
avshetty1980 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 | ||
| with: | ||
| terraform_version: ${{ env.ACCOUNT_TERRAFORM_VERSION }} | ||
|
|
||
| - name: Resolve account terraform state bucket | ||
| id: account-state-bucket | ||
| run: echo "bucket_name=$(bash ./utilities/scripts/resolve_account_terraform_state_bucket.sh)" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Retrieve Account Terraform Plan | ||
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c | ||
| with: | ||
| name: ${{ env.ACCOUNT_TERRAFORM_ARTIFACT_NAME }} | ||
| path: infrastructure/account | ||
|
|
||
| - name: Verify Account Terraform Plan Attestation | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| run: | | ||
| gh attestation verify infrastructure/account/tfplan \ | ||
| --repo "$GITHUB_REPOSITORY" \ | ||
| --signer-workflow "$GITHUB_REPOSITORY/.github/workflows/account-terraform.yml" | ||
|
|
||
| - name: Terraform Init (account) | ||
| working-directory: infrastructure/account | ||
| env: | ||
| ACCOUNT_TERRAFORM_BUCKET_NAME: ${{ steps.account-state-bucket.outputs.bucket_name }} | ||
| run: make init ENVIRONMENT="$ACCOUNT_TERRAFORM_ENVIRONMENT" BUCKET_NAME="$ACCOUNT_TERRAFORM_BUCKET_NAME" | ||
|
|
||
| - name: Terraform Apply (account) | ||
| # Ignore cancellations to prevent Terraform from being killed while it holds a state lock | ||
| # A stuck process can still be killed with the force-cancel API operation | ||
| if: ${{ !failure() }} | ||
| working-directory: infrastructure/account | ||
| run: make apply-ci ENVIRONMENT="$ACCOUNT_TERRAFORM_ENVIRONMENT" | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
utilities/scripts/resolve_account_terraform_state_bucket.sh
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #!/bin/bash | ||
avshetty1980 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| set -euo pipefail | ||
|
|
||
| read -r configured_bucket <<< "${CONFIGURED_ACCOUNT_TERRAFORM_STATE_BUCKET:-}" | ||
| read -r state_bucket_environment <<< "${ACCOUNT_TERRAFORM_STATE_ENVIRONMENT:-}" | ||
|
|
||
| [ -n "$configured_bucket" ] && printf '%s\n' "$configured_bucket" && exit 0 | ||
|
|
||
| [ -n "$state_bucket_environment" ] || { | ||
| echo "ACCOUNT_TERRAFORM_STATE_ENVIRONMENT must be set when ACCOUNT_TERRAFORM_STATE_BUCKET is not configured." >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| case "$state_bucket_environment" in | ||
| internal-dev|internal-qa|preprod|prod) | ||
| ;; | ||
| *) | ||
| echo "ACCOUNT_TERRAFORM_STATE_ENVIRONMENT must be one of: internal-dev, internal-qa, preprod, prod." >&2 | ||
| exit 1 | ||
| ;; | ||
| esac | ||
|
|
||
| printf 'immunisation-%s-terraform-state-files\n' "$state_bucket_environment" | ||
avshetty1980 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.