generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 20
259 lines (253 loc) · 11.3 KB
/
apex.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
---
name: apex
on:
push:
branches:
- main
paths:
- 'terraform/environments/apex/**'
- '.github/workflows/apex.yml'
pull_request:
branches:
- main
types: [opened, edited, reopened, synchronize]
paths:
- 'terraform/environments/apex/**'
- '.github/workflows/apex.yml'
workflow_dispatch:
inputs:
action:
description: 'Set either [deploy|destroy].'
default: 'deploy'
required: true
type: string
options:
- deploy
- destroy
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
defaults:
run:
shell: bash
jobs:
plan-dev-test:
strategy:
matrix:
include:
- environment: development
# - environment: test
name: Terraform plan - ${{ github.workflow }} - ${{ matrix.environment }}
runs-on: ubuntu-latest
if: github.ref != 'refs/heads/main' || github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'deploy'
env:
TF_ENV: ${{ matrix.environment }}
steps:
- name: Checkout Repository
uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # v3.5.1
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${GITHUB_WORKFLOW}-${TF_ENV}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@e1e17a757e536f70e52b5a12b2e8d1d1c60e04ef # v2.0.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Load and Configure Terraform
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Terraform plan - ${{ github.workflow }} - ${{ matrix.environment }}
run: |
terraform --version
echo "Terraform plan - ${TF_ENV}"
bash scripts/terraform-init.sh terraform/environments/$GITHUB_WORKFLOW
terraform -chdir="terraform/environments/${GITHUB_WORKFLOW}" workspace select "${GITHUB_WORKFLOW}-${TF_ENV}"
bash scripts/terraform-plan.sh terraform/environments/$GITHUB_WORKFLOW
# These jobs run when creating a pull request
deploy-dev-test:
needs: plan-dev-test
if: success() && github.event.inputs.action != 'destroy'
strategy:
matrix:
include:
- environment: development
# - environment: test
name: Terraform apply - ${{ github.workflow }} - ${{ matrix.environment }}
runs-on: ubuntu-latest
env:
TF_ENV: ${{ matrix.environment }}
environment:
name: ${{ github.workflow }}-${{ matrix.environment }}
steps:
- name: Checkout Repository
uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # v3.5.1
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${GITHUB_WORKFLOW}-${TF_ENV}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@e1e17a757e536f70e52b5a12b2e8d1d1c60e04ef # v2.0.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Load and Configure Terraform
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Terraform apply - ${{ github.workflow }} - ${{ matrix.environment }}
run: |
terraform --version
echo "Terraform apply - ${TF_ENV}"
bash scripts/terraform-init.sh terraform/environments/$GITHUB_WORKFLOW
terraform -chdir="terraform/environments/${GITHUB_WORKFLOW}" workspace select "${GITHUB_WORKFLOW}-${TF_ENV}"
bash scripts/terraform-apply.sh terraform/environments/$GITHUB_WORKFLOW
destroy-plan-dev-test:
strategy:
matrix:
include:
- environment: development
# - environment: test
name: Terraform destroy plan - ${{ github.workflow }} - ${{ matrix.environment }}
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'destroy'
env:
TF_ENV: ${{ matrix.environment }}
steps:
- name: Checkout Repository
uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # v3.5.1
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${GITHUB_WORKFLOW}-${TF_ENV}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@e1e17a757e536f70e52b5a12b2e8d1d1c60e04ef # v2.0.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Load and Configure Terraform
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Terraform destroy plan - ${{ github.workflow }} - ${{ matrix.environment }}
run: |
terraform --version
echo "Terraform destroy plan - ${TF_ENV}"
bash scripts/terraform-init.sh terraform/environments/$GITHUB_WORKFLOW
terraform -chdir="terraform/environments/${GITHUB_WORKFLOW}" workspace select "${GITHUB_WORKFLOW}-${TF_ENV}"
bash scripts/terraform-plan.sh terraform/environments/$GITHUB_WORKFLOW -destroy
destroy-apply-dev-test:
needs: destroy-plan-dev-test
if: success()
strategy:
matrix:
include:
- environment: development
# - environment: test
name: Terraform destroy apply - ${{ github.workflow }} - ${{ matrix.environment }}
runs-on: ubuntu-latest
env:
TF_ENV: ${{ matrix.environment }}
environment:
name: ${{ github.workflow }}-${{ matrix.environment }}
steps:
- name: Checkout Repository
uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # v3.5.1
- name: Set Account Number
run: echo "ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${GITHUB_WORKFLOW}-${TF_ENV}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@e1e17a757e536f70e52b5a12b2e8d1d1c60e04ef # v2.0.0
with:
role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
role-session-name: githubactionsrolesession
aws-region: ${{ env.AWS_REGION }}
- name: Load and Configure Terraform
uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
with:
terraform_version: "~1"
terraform_wrapper: false
- name: Terraform destroy apply - ${{ github.workflow }} - ${{ matrix.environment }}
run: |
terraform --version
echo "Terraform destroy apply - ${TF_ENV}"
bash scripts/terraform-init.sh terraform/environments/$GITHUB_WORKFLOW
terraform -chdir="terraform/environments/${GITHUB_WORKFLOW}" workspace select "${GITHUB_WORKFLOW}-${TF_ENV}"
bash scripts/terraform-apply.sh terraform/environments/$GITHUB_WORKFLOW -destroy
# # Plan + deploy for pre-production and production environments, only from main
# plan-preprod-prod:
# strategy:
# matrix:
# include:
# - environment: preproduction
# - environment: production
# name: Plan - ${{ matrix.environment }}
# runs-on: ubuntu-latest
# if: github.ref == 'refs/heads/main'
# env:
# TF_ENV: ${{ matrix.environment }}
# steps:
# - name: Checkout Repository
# uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # v3.5.1
# - name: Set Account Number
# run: echo "ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${GITHUB_WORKFLOW}-${TF_ENV}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
# - name: configure aws credentials
# uses: aws-actions/configure-aws-credentials@e1e17a757e536f70e52b5a12b2e8d1d1c60e04ef # v2.0.0
# with:
# role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
# role-session-name: githubactionsrolesession
# aws-region: ${{ env.AWS_REGION }}
# - name: Load and Configure Terraform
# uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
# with:
# terraform_version: "~1"
# terraform_wrapper: false
# - name: Plan - ${{ matrix.environment }}
# run: |
# terraform --version
# echo "Terraform plan - ${TF_ENV}"
# bash scripts/terraform-init.sh terraform/environments/$GITHUB_WORKFLOW
# terraform -chdir="terraform/environments/${GITHUB_WORKFLOW}" workspace select "${GITHUB_WORKFLOW}-${TF_ENV}"
# bash scripts/terraform-plan.sh terraform/environments/$GITHUB_WORKFLOW
# # These jobs run when creating a pull request
# deploy-preprod-prod:
# needs: plan-preprod-prod
# if: success()
# strategy:
# matrix:
# include:
# - environment: preproduction
# - environment: production
# name: Apply - ${{ matrix.environment }}
# runs-on: ubuntu-latest
# env:
# TF_ENV: ${{ matrix.environment }}
# environment:
# name: ${{ github.workflow }}-${{ matrix.environment }}
# steps:
# - name: Checkout Repository
# uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # v3.5.1
# - name: Set Account Number
# run: echo "ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${GITHUB_WORKFLOW}-${TF_ENV}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)" >> $GITHUB_ENV
# - name: configure aws credentials
# uses: aws-actions/configure-aws-credentials@e1e17a757e536f70e52b5a12b2e8d1d1c60e04ef # v2.0.0
# with:
# role-to-assume: "arn:aws:iam::${{ env.ACCOUNT_NUMBER }}:role/github-actions"
# role-session-name: githubactionsrolesession
# aws-region: ${{ env.AWS_REGION }}
# - name: Load and Configure Terraform
# uses: hashicorp/setup-terraform@633666f66e0061ca3b725c73b2ec20cd13a8fdd1 # v2.0.3
# with:
# terraform_version: "~1"
# terraform_wrapper: false
# - name: Apply - ${{ matrix.environment }}
# run: |
# terraform --version
# echo "Terraform apply - ${TF_ENV}"
# bash scripts/terraform-init.sh terraform/environments/$GITHUB_WORKFLOW
# terraform -chdir="terraform/environments/${GITHUB_WORKFLOW}" workspace select "${GITHUB_WORKFLOW}-${TF_ENV}"
# bash scripts/terraform-apply.sh terraform/environments/$GITHUB_WORKFLOW