-
Notifications
You must be signed in to change notification settings - Fork 2
/
.drone.yml
325 lines (302 loc) · 11.1 KB
/
.drone.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
---
global-variables:
# testrunner-image: &testrunner-image quay.io/ukhomeofficedigital/tf-testrunner:32 # TF0.12
testrunner-image: &testrunner-image quay.io/ukhomeofficedigital/tf-testrunner:TF1.6
# terragrunt-image: &terragrunt-image quay.io/ukhomeofficedigital/dq-docker-terragrunt:v0.23.18 # TF0.12
# terragrunt-image: &terragrunt-image quay.io/ukhomeofficedigital/dq-docker-terragrunt:v0.26.2 # TF0.13
# terragrunt-image: &terragrunt-image quay.io/ukhomeofficedigital/dq-docker-terragrunt:v0.28.6 # TF0.14
# terragrunt-image: &terragrunt-image quay.io/ukhomeofficedigital/dq-docker-terragrunt:v0.30.7 # TF0.15
# terragrunt-image: &terragrunt-image quay.io/ukhomeofficedigital/dq-docker-terragrunt:v0.35.20 # TF1.0
# terragrunt-image: &terragrunt-image quay.io/ukhomeofficedigital/dq-docker-terragrunt:v0.48.5 # TF1.5
terragrunt-image: &terragrunt-image quay.io/ukhomeofficedigital/dq-docker-terragrunt:TGv0.54.4_TFv1.6.6
vault-image: &vault-image docker.digital.homeoffice.gov.uk/dq/dq-vault-awscli:1.43
kind: pipeline
name: default
type: kubernetes
platform:
os: linux
arch: amd64
environment:
BUCKET: dq-tf-infra-terraform-state
# BUCKET: terraform-statefiles-all-envs
STATE_FILE: dq-tf-infra
# TF_VERSION: 0.13
# TF_VERSION: 1.0
TF_VERSION: 1.6
AWS_REGION: eu-west-2
DEPLOYMENT_TYPE: dq-tf-infra
x-anchors:
retrieve-state-aws-key: &retrieve-state-aws-key
- vault --version
# All Terraform State is held in an S3 Bucket - normally this is in the 'test' AWS Account
# BUT dq-tf-infra NotProd and Prod deployments' Terraform State is held in an S3 Bucket in the 'CI' AWS Account
# - so all Terraform steps will need to source AWS_SECRETS_FILE
- export AWS_ENV="state"
- export AWS_CREDS_FILE="aws_creds_$${AWS_ENV}.json"
- export AWS_SECRETS_FILE="set_aws_secrets_$${AWS_ENV}.sh"
# Retrieve vault secrets
- vault read aws_dacc_dq/creds/drone > $${AWS_CREDS_FILE}
- export LEASE_ID=$(cat $${AWS_CREDS_FILE} | grep lease_id | awk -F ' ' '{print $2}')
- export ACCESS_KEY=$(cat $${AWS_CREDS_FILE} | grep access_key | awk -F ' ' '{print $2}')
- export SECRET_KEY=$(cat $${AWS_CREDS_FILE} | grep secret_key | awk -F ' ' '{print $2}')
# Update the token TTL to 10mins
- vault lease renew -increment=3600 $${LEASE_ID}
# Get the AWS credentials - for `Terragrunt/Terraform`
- echo "export AWS_ACCESS_KEY_ID=$${ACCESS_KEY}" > $${AWS_SECRETS_FILE}
- echo "export AWS_SECRET_ACCESS_KEY=$${SECRET_KEY}" >> $${AWS_SECRETS_FILE}
- echo "export AWS_DEFAULT_REGION=$${AWS_REGION}" >> $${AWS_SECRETS_FILE}
retrieve-ci-aws-key: &retrieve-ci-aws-key
- vault --version
# Set AWS Account from step's environment variable AWS_ENV
- export AWS_ENV="ci"
- export AWS_CREDS_FILE="aws_creds_$${AWS_ENV}.json"
- export AWS_SECRETS_FILE="set_aws_secrets_$${AWS_ENV}.sh"
# Retrieve vault secrets
- vault read aws_dacc_dq/creds/drone > $${AWS_CREDS_FILE}
- export LEASE_ID=$(cat $${AWS_CREDS_FILE} | grep lease_id | awk -F ' ' '{print $2}')
- export ACCESS_KEY=$(cat $${AWS_CREDS_FILE} | grep access_key | awk -F ' ' '{print $2}')
- export SECRET_KEY=$(cat $${AWS_CREDS_FILE} | grep secret_key | awk -F ' ' '{print $2}')
# Update the token TTL to 10mins
- vault lease renew -increment=3600 $${LEASE_ID}
# Get the AWS credentials - to allow Terraform to deploy to the Target Account
- echo "export TF_VAR_CI_ID=$${ACCESS_KEY}" > $${AWS_SECRETS_FILE}
- echo "export TF_VAR_CI_KEY=$${SECRET_KEY}" >> $${AWS_SECRETS_FILE}
- echo "export AWS_DEFAULT_REGION=$${AWS_REGION}" >> $${AWS_SECRETS_FILE}
retrieve-deployment-aws-key: &retrieve-deployment-aws-key
- vault --version
# Set AWS Account from step's environment variable AWS_ENV
- export AWS_CREDS_FILE="aws_creds_$${AWS_ENV}.json"
- export AWS_SECRETS_FILE="set_aws_secrets_$${AWS_ENV}.sh"
# Retrieve vault secrets
- vault read aws_dacc_dq/creds/drone > $${AWS_CREDS_FILE}
- export LEASE_ID=$(cat $${AWS_CREDS_FILE} | grep lease_id | awk -F ' ' '{print $2}')
- export ACCESS_KEY=$(cat $${AWS_CREDS_FILE} | grep access_key | awk -F ' ' '{print $2}')
- export SECRET_KEY=$(cat $${AWS_CREDS_FILE} | grep secret_key | awk -F ' ' '{print $2}')
# Update the token TTL to 10mins
- vault lease renew -increment=3600 $${LEASE_ID}
# Get the AWS credentials - to allow Terraform to deploy to the Target Account
- echo "export TF_VAR_ENV_ACCT_ID=$${ACCESS_KEY}" > $${AWS_SECRETS_FILE}
- echo "export TF_VAR_ENV_ACCT_KEY=$${SECRET_KEY}" >> $${AWS_SECRETS_FILE}
- echo "export AWS_DEFAULT_REGION=$${AWS_REGION}" >> $${AWS_SECRETS_FILE}
check-format: &check-format
- terraform --version
- terraform fmt --diff --check
tf-init: &tf-init
# Get AWS secrets for TF State
- export AWS_SECRETS_FILE="set_aws_secrets_state.sh"
- source $${AWS_SECRETS_FILE}
# Get AWS secrets for Target Account
- export AWS_SECRETS_FILE="set_aws_secrets_$${AWS_ENV}.sh"
- source $${AWS_SECRETS_FILE}
# Create config files for Terragrunt
- echo -e "provider \"aws\" {\n region = \"$AWS_DEFAULT_REGION\"\n}" > provider.tf
- echo -e "terraform {\n backend \"s3\" {}\n}" > backend.tf
- echo -e "remote_state {\n backend = \"s3\"\n config = {\n bucket = \"$BUCKET\"\n region = \"$AWS_DEFAULT_REGION\"\n dynamodb_table = \"terraform-state\"\n key = \"$DEPLOYMENT_TYPE/terraform-$TF_VERSION/$AWS_ENV/$STATE_FILE.tfstate\"\n encrypt = true \n }\n}" > terragrunt.hcl
# Add SSH_KEY to allow git clone
- mkdir /root/.ssh && echo "$SSH_KEY" > /root/.ssh/id_rsa && chmod 0600 /root/.ssh/id_rsa
- ssh-keyscan -t rsa -p 2222 gitlab.digital.homeoffice.gov.uk >> ~/.ssh/known_hosts
# Additional env vars
- export TF_VAR_NAMESPACE="$${AWS_ENV}"
# INIT
- terragrunt init -reconfigure
tf-validate: &tf-validate
# Get AWS secrets for TF State
- export AWS_SECRETS_FILE="set_aws_secrets_state.sh"
- source $${AWS_SECRETS_FILE}
# Get AWS secrets for CI account
- export AWS_SECRETS_FILE="set_aws_secrets_ci.sh"
- source $${AWS_SECRETS_FILE}
# Get AWS secrets for Target Account
- export AWS_SECRETS_FILE="set_aws_secrets_$${AWS_ENV}.sh"
- source $${AWS_SECRETS_FILE}
# Add SSH_KEY to allow git clone
- mkdir /root/.ssh && echo "$SSH_KEY" > /root/.ssh/id_rsa && chmod 0600 /root/.ssh/id_rsa
- ssh-keyscan -t rsa -p 2222 gitlab.digital.homeoffice.gov.uk >> ~/.ssh/known_hosts
# Additional env vars
- export TF_VAR_NAMESPACE="$${AWS_ENV}"
# VALIDATE
- terragrunt validate
tf-plan: &tf-plan
# Get AWS secrets for TF State
- export AWS_SECRETS_FILE="set_aws_secrets_state.sh"
- source $${AWS_SECRETS_FILE}
# Get AWS secrets for CI Account
- export AWS_SECRETS_FILE="set_aws_secrets_ci.sh"
- source $${AWS_SECRETS_FILE}
# Get AWS secrets for Target Account
- export AWS_SECRETS_FILE="set_aws_secrets_$${AWS_ENV}.sh"
- source $${AWS_SECRETS_FILE}
# Add SSH_KEY to allow git clone
- mkdir /root/.ssh && echo "$SSH_KEY" > /root/.ssh/id_rsa && chmod 0600 /root/.ssh/id_rsa
- ssh-keyscan -t rsa -p 2222 gitlab.digital.homeoffice.gov.uk >> ~/.ssh/known_hosts
# Additional env vars
- export TF_VAR_NAMESPACE="$${AWS_ENV}"
# PLAN
- terragrunt plan -lock=false -out=plan_$${AWS_ENV}
tf-apply: &tf-apply
# Get AWS secrets for TF State
- export AWS_SECRETS_FILE="set_aws_secrets_state.sh"
- source $${AWS_SECRETS_FILE}
# Get AWS secrets for CI Account
- export AWS_SECRETS_FILE="set_aws_secrets_ci.sh"
- source $${AWS_SECRETS_FILE}
# Get AWS secrets for Target Account
- export AWS_SECRETS_FILE="set_aws_secrets_$${AWS_ENV}.sh"
- source $${AWS_SECRETS_FILE}
# Add SSH_KEY to allow git clone
- mkdir /root/.ssh && echo "$SSH_KEY" > /root/.ssh/id_rsa && chmod 0600 /root/.ssh/id_rsa
- ssh-keyscan -t rsa -p 2222 gitlab.digital.homeoffice.gov.uk >> ~/.ssh/known_hosts
# Additional env vars
- export TF_VAR_NAMESPACE="$${AWS_ENV}"
# APPLY
- terragrunt apply -auto-approve -parallelism=50 plan_$${AWS_ENV}
steps:
# Used to access TF state-file
- name: retrieve-aws-secrets-tf-state
pull: always
image: *vault-image
commands: *retrieve-state-aws-key
environment:
# At present the statefiles are held in CI (=SIT)
VAULT_ADDR:
from_secret: VAULT_ADDR_SIT
VAULT_TOKEN:
from_secret: VAULT_TOKEN_SIT
# # Would like to store state files in Test (=Dev)
# VAULT_ADDR:
# from_secret: VAULT_ADDR_DEV
# VAULT_TOKEN:
# from_secret: VAULT_TOKEN_DEV
when:
event:
- push
- promote
# Used to access CI account
- name: retrieve-aws-secrets-tf-ci
pull: always
image: *vault-image
commands: *retrieve-ci-aws-key
environment:
VAULT_ADDR:
from_secret: VAULT_ADDR_SIT
VAULT_TOKEN:
from_secret: VAULT_TOKEN_SIT
when:
event:
- push
- promote
# AWS 'NotProd' account Secrets - used to deploy to 'NotProd' account
- name: retrieve-aws-secrets-notprod
pull: if-not-exists
image: *vault-image
commands: *retrieve-deployment-aws-key
environment:
AWS_ENV: notprod
VAULT_ADDR:
from_secret: VAULT_ADDR_NOTPROD
VAULT_TOKEN:
from_secret: VAULT_TOKEN_NOTPROD
when:
event:
- push
- name: init-notprod
pull: if-not-exists
image: *terragrunt-image
commands: *tf-init
environment:
AWS_ENV: notprod
SSH_KEY:
from_secret: SSH_KEY
when:
event:
- push
- name: check-format
pull: if-not-exists
image: *terragrunt-image
commands: *check-format
when:
event:
- push
- name: validate-notprod
pull: if-not-exists
image: *terragrunt-image
commands: *tf-validate
environment:
AWS_ENV: notprod
when:
event:
- push
- name: plan-notprod
pull: if-not-exists
image: *terragrunt-image
commands: *tf-plan
environment:
AWS_ENV: notprod
DOMAIN_JOINER_PWD:
from_secret: DOMAIN_JOINER_PWD
when:
event:
- push
- name: apply-notprod
pull: if-not-exists
image: *terragrunt-image
commands: *tf-apply
environment:
AWS_ENV: notprod
when: # Only apply to notprod when pushing to master
branch:
include:
- master
event:
- push
# AWS 'Prod' account Secrets - used to deploy to 'Prod' account
- name: retrieve-aws-secrets-prod
pull: if-not-exists
image: *vault-image
commands: *retrieve-deployment-aws-key
environment:
AWS_ENV: prod
VAULT_ADDR:
from_secret: VAULT_ADDR_PROD
VAULT_TOKEN:
from_secret: VAULT_TOKEN_PROD
when:
event:
- promote
- push
- name: init-prod
pull: if-not-exists
image: *terragrunt-image
commands: *tf-init
environment:
AWS_ENV: prod
SSH_KEY:
from_secret: SSH_KEY
when:
event:
- promote
- push
- name: plan-prod
pull: if-not-exists
image: *terragrunt-image
commands: *tf-plan
environment:
AWS_ENV: prod
when:
event:
- promote
- push
- name: apply-prod
pull: if-not-exists
image: *terragrunt-image
commands: *tf-apply
environment:
AWS_ENV: prod
when: # Only apply to Prod when promoting master branch to prod
branch:
include:
- master
event:
- promote
target:
- prod