generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 290
267 lines (248 loc) · 10.4 KB
/
terraform-member-environment.yml
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
---
name: "Terraform: Member environments"
on:
push:
branches:
- main
paths:
- 'terraform/environments/**/*.tf'
- '!terraform/environments/core-*'
pull_request:
types: [opened, edited, reopened, synchronize]
branches-ignore:
- 'date*'
paths:
- 'terraform/environments/**/*.tf'
- '!terraform/environments/core-*'
- '.github/workflows/terraform-member-environment.yml'
defaults:
run:
shell: bash
env:
TF_IN_AUTOMATION: true
AWS_REGION: "eu-west-2"
ENVIRONMENT_MANAGEMENT: ${{ secrets.MODERNISATION_PLATFORM_ENVIRONMENTS }}
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
find-environments:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Get changed directories
id: directories
run: |
git fetch origin main
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "CHANGED_DIRECTORIES=$(git diff HEAD origin/main ${{ github.event.pull_request.changes.paths }} --name-only | awk '{print $1}' | grep ".tf" | grep -a "environments//*" | cut -f1-3 -d"/" | uniq)"
else
echo "CHANGED_DIRECTORIES=$(git diff HEAD HEAD~ ${{ github.event.changes.paths }} --name-only | | awk '{print $1}' | grep ".tf" | grep -a "environments//*" | cut -f1-3 -d"/" | uniq)"
fi >> $GITHUB_OUTPUT
- name: Display changed directories
run: echo "Directories in scope:" ${{ steps.directories.outputs.CHANGED_DIRECTORIES }}
outputs:
directories: ${{ steps.directories.outputs.CHANGED_DIRECTORIES }}
terraform-plan:
runs-on: ubuntu-latest
needs: find-environments
strategy:
matrix:
environment: [development, test, preproduction, production]
env:
TF_IN_AUTOMATION: "true"
CHANGED_DIRECTORIES: ${{ needs.find-environments.outputs.directories }}
steps:
- name: Checkout repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Set account number
run: echo "ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2.2.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Setup Terraform
id: setup
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Terraform init
id: init
run: |
for directory in $CHANGED_DIRECTORIES; do
echo "Running 'terraform init' in" $directory
scripts/terraform-init.sh $directory
done
- name: Terraform workspace
id: workspace
run: |
for directory in $CHANGED_DIRECTORIES; do
workspace="$(basename $directory)-${{ matrix.environment }}"
if terraform -chdir="$directory" workspace list | grep "$workspace"; then
terraform -chdir="$directory" workspace select "$workspace"
else
echo "Workspace '$workspace' does not exist, skipping further processing"
echo "skip_plan=true" >> $GITHUB_OUTPUT
fi
unset workspace
done
echo "Selected $(terraform -chdir="$directory" workspace show)"
- name: Terraform plan
id: plan
if: ${{ steps.workspace.outputs.skip_plan != 'true' }}
run: |
for directory in $CHANGED_DIRECTORIES; do
workspace="$(basename $directory)-${{ matrix.environment }}"
scripts/terraform-plan.sh $directory
unset workspace
done
- name: Mark job skipped
if: ${{ steps.workspace.outputs.skip_plan == 'true' }}
run: |
echo "::warning ::Terraform plan was skipped as no valid workspace was found."
terraform-apply-dev-test:
if: ${{ github.event_name == 'pull_request' }}
runs-on: ubuntu-latest
needs: [find-environments, terraform-plan]
strategy:
matrix:
environment: [development, test]
environment: ${{ matrix.environment }}
env:
TF_IN_AUTOMATION: "true"
CHANGED_DIRECTORIES: ${{ needs.find-environments.outputs.directories }}
steps:
- name: Checkout repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Set account number
run: echo "ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2.2.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Setup Terraform
id: setup
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Terraform init
id: init
run: |
for directory in $CHANGED_DIRECTORIES; do
echo "Running 'terraform init' in" $directory
scripts/terraform-init.sh $directory
done
- name: Terraform workspace
id: workspace
run: |
for directory in $CHANGED_DIRECTORIES; do
workspace="$(basename $directory)-${{ matrix.environment }}"
if terraform -chdir="$directory" workspace list | grep "$workspace"; then
terraform -chdir="$directory" workspace select "$workspace"
else
echo "Workspace '$workspace' does not exist, skipping further processing"
echo "skip_plan=true" >> $GITHUB_OUTPUT
fi
unset workspace
done
echo "Selected $(terraform -chdir="$directory" workspace show)"
- name: Terraform plan
id: plan
if: ${{ steps.workspace.outputs.skip_plan != 'true' }}
run: |
for directory in $CHANGED_DIRECTORIES; do
workspace="$(basename $directory)-${{ matrix.environment }}"
scripts/terraform-plan.sh $directory -out="$workspace.tfplan"
unset workspace
done
- name: Terraform apply
id: apply
if: ${{ steps.plan.conclusion == 'success' }}
run: |
for directory in $CHANGED_DIRECTORIES; do
workspace="$(basename $directory)-${{ matrix.environment }}"
scripts/terraform-plan.sh $directory
unset workspace
done
- name: Mark job skipped
if: ${{ steps.workspace.outputs.skip_plan == 'true' }}
run: |
echo "::warning ::Terraform plan was skipped as no valid workspace was found."
terraform-apply-preprod-prod:
if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' }}
runs-on: ubuntu-latest
needs: [find-environments, terraform-plan]
strategy:
matrix:
environment: [preproduction, production]
environment: ${{ matrix.environment }}
env:
TF_IN_AUTOMATION: "true"
CHANGED_DIRECTORIES: ${{ needs.find-environments.outputs.directories }}
steps:
- name: Checkout repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Set account number
run: echo "ACCOUNT_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@5fd3084fc36e372ff1fff382a39b10d03659f355 # v2.2.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Setup Terraform
id: setup
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Terraform init
id: init
run: |
for directory in $CHANGED_DIRECTORIES; do
echo "Running 'terraform init' in" $directory
scripts/terraform-init.sh $directory
done
- name: Terraform workspace
id: workspace
run: |
for directory in $CHANGED_DIRECTORIES; do
workspace="$(basename $directory)-${{ matrix.environment }}"
if terraform -chdir="$directory" workspace list | grep "$workspace"; then
terraform -chdir="$directory" workspace select "$workspace"
else
echo "Workspace '$workspace' does not exist, skipping further processing"
echo "skip_plan=true" >> $GITHUB_OUTPUT
fi
unset workspace
done
echo "Selected $(terraform -chdir="$directory" workspace show)"
- name: Terraform plan
id: plan
if: ${{ steps.workspace.outputs.skip_plan != 'true' }}
run: |
for directory in $CHANGED_DIRECTORIES; do
workspace="$(basename $directory)-${{ matrix.environment }}"
scripts/terraform-plan.sh $directory -out="$workspace.tfplan"
unset workspace
done
- name: Terraform apply
id: apply
if: ${{ steps.plan.conclusion == 'success' }}
run: |
for directory in $CHANGED_DIRECTORIES; do
workspace="$(basename $directory)-${{ matrix.environment }}"
scripts/terraform-plan.sh $directory
unset workspace
done
- name: Mark job skipped
if: ${{ steps.workspace.outputs.skip_plan == 'true' }}
run: |
echo "::warning ::Terraform plan was skipped as no valid workspace was found."