-
Notifications
You must be signed in to change notification settings - Fork 4
183 lines (161 loc) · 6.58 KB
/
account-terraform.yml
File metadata and controls
183 lines (161 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
name: Account Terraform
on:
workflow_call:
inputs:
base_sha:
required: true
type: string
head_sha:
required: true
type: string
environment:
required: true
type: string
artifact_name:
required: true
type: string
concurrency:
group: account-terraform-${{ github.repository }}-${{ inputs.environment }}
cancel-in-progress: false
jobs:
detect-account-infra-changes:
runs-on: ubuntu-latest
outputs:
account_infra_changed: ${{ steps.diff.outputs.account_infra_changed }}
steps:
- name: Checkout
uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98
with:
fetch-depth: 0
- name: Detect account terraform changes
id: diff
run: |
base_sha="${{ inputs.base_sha }}"
if [ -z "$base_sha" ] || [ "$base_sha" = "0000000000000000000000000000000000000000" ]; then
base_sha=$(git rev-parse HEAD~1)
fi
changed_files=$(git diff --name-only "$base_sha" "${{ inputs.head_sha }}")
if echo "$changed_files" | grep -q '^infrastructure/account/'; then
echo "account_infra_changed=true" >> "$GITHUB_OUTPUT"
else
echo "account_infra_changed=false" >> "$GITHUB_OUTPUT"
fi
account-terraform-plan:
permissions:
id-token: write
contents: read
needs: [detect-account-infra-changes]
if: ${{ needs.detect-account-infra-changes.outputs.account_infra_changed == 'true' }}
runs-on: ubuntu-latest
environment:
name: ${{ inputs.environment }}
steps:
- name: Checkout
uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98
- 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: github-actions
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85
with:
terraform_version: "1.12.2"
- name: Resolve account terraform state bucket
id: account-state-bucket
env:
CONFIGURED_ACCOUNT_TERRAFORM_STATE_BUCKET: ${{ vars.ACCOUNT_TERRAFORM_STATE_BUCKET }}
run: |
bucket_name="$(bash ./utilities/scripts/resolve_account_terraform_state_bucket.sh)"
if [ -z "$bucket_name" ]; then
echo "Resolved empty account terraform state bucket." >&2
exit 1
fi
echo "bucket_name=$bucket_name" >> "$GITHUB_OUTPUT"
- name: Terraform Init (account)
working-directory: infrastructure/account
run: make init ENVIRONMENT=${{ inputs.environment }} BUCKET_NAME=${{ steps.account-state-bucket.outputs.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: ${{ !failure() }}
working-directory: infrastructure/account
run: make plan-ci ENVIRONMENT=${{ inputs.environment }}
- name: Save Account Terraform Plan
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
with:
name: ${{ inputs.artifact_name }}
path: infrastructure/account/tfplan
account-terraform-manual-approval:
needs: [account-terraform-plan]
if: ${{ !cancelled() && needs.account-terraform-plan.result == 'success' }}
runs-on: ubuntu-latest
environment:
name: account-level-infra-approval
steps:
- name: Await manual approval
run: echo "Waiting for account-level infrastructure approval."
account-terraform-apply:
permissions:
id-token: write
contents: read
needs: [account-terraform-manual-approval]
if: ${{ !cancelled() && needs.account-terraform-manual-approval.result == 'success' }}
runs-on: ubuntu-latest
environment:
name: ${{ inputs.environment }}
steps:
- name: Checkout
uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98
- 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: github-actions
- uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85
with:
terraform_version: "1.12.2"
- name: Resolve account terraform state bucket
id: account-state-bucket
env:
CONFIGURED_ACCOUNT_TERRAFORM_STATE_BUCKET: ${{ vars.ACCOUNT_TERRAFORM_STATE_BUCKET }}
run: |
bucket_name="$(bash ./utilities/scripts/resolve_account_terraform_state_bucket.sh)"
if [ -z "$bucket_name" ]; then
echo "Resolved empty account terraform state bucket." >&2
exit 1
fi
echo "bucket_name=$bucket_name" >> "$GITHUB_OUTPUT"
- name: Retrieve Account Terraform Plan
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c
with:
name: ${{ inputs.artifact_name }}
path: infrastructure/account
- name: Terraform Init (account)
working-directory: infrastructure/account
run: make init ENVIRONMENT=${{ inputs.environment }} BUCKET_NAME=${{ steps.account-state-bucket.outputs.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=${{ inputs.environment }}
account-terraform-not-required:
needs: [detect-account-infra-changes]
if: ${{ needs.detect-account-infra-changes.outputs.account_infra_changed != 'true' }}
runs-on: ubuntu-latest
steps:
- name: Skip account terraform
run: echo "No account-level infrastructure changes detected."
account-terraform-ready:
needs:
- account-terraform-plan
- account-terraform-manual-approval
- account-terraform-apply
- account-terraform-not-required
if: ${{ always() && !contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled') }}
runs-on: ubuntu-latest
steps:
- name: Account terraform stage complete
run: echo "Account terraform stage complete."