-
Notifications
You must be signed in to change notification settings - Fork 2
/
terraform-toolkit.yml
234 lines (223 loc) · 6.52 KB
/
terraform-toolkit.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
---
variables:
DOCKER_DRIVER: "overlay2"
DOCKER_TLS_CERTDIR: "/certs"
.job_template: &job_template
before_script:
- terraform --version
tags:
- docker
- miquido
cache:
paths:
- "**/.terraform"
.get-latest-terraform:
stage: .pre
tags:
- docker
- miquido
before_script:
- apk add jq
- apk add curl
script:
- |
if [ -z ${GITHUB_TOKEN+x} ];
then
echo "GITHUB_TOKEN is unset";
echo "TF_VERSION=1.0.0" >> build.env
else
TF_VERSION=$(curl -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/hashicorp/terraform/releases/latest \
| jq --raw-output '.tag_name' | cut -c 2-)
echo "Latest terraform version = $TF_VERSION"
echo "TF_VERSION=$TF_VERSION" >> build.env
fi
artifacts:
reports:
dotenv: build.env
.check-formatting:
<<: *job_template
image:
name: hashicorp/terraform:$TF_VERSION
entrypoint:
- "/usr/bin/env"
- "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
variables:
TF_VERSION: 1.3.7
script:
- terraform fmt -check -recursive
#### Deprecated
.validate:
<<: *job_template
variables:
ENVIRONMENTS: prod
TF_VERSION: 1.3.7
image:
name: miquido/terraform:$TF_VERSION
entrypoint:
- "/usr/bin/env"
- "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
script:
- echo "WARNING! .validate is deprecated and will be removed. Use .validate-terraform instead"
# yamllint disable rule:line-length
- |
IFS=' ' #setting space as delimiter
read -ra ENVIRONMENTS <<<"$ENVIRONMENTS"
for ENVIRONMENT in "${ENVIRONMENTS[@]}"
do
cd "$ENVIRONMENT"
git config --global \url."https://${GITLAB_TERRAFORM_DEPLOY_TOKEN_USERNAME}:${GITLAB_TERRAFORM_DEPLOY_TOKEN_SECRET}@gitlab.com/miquido/terraform".insteadOf "ssh://git@gitlab.com/miquido/terraform"
git config --global url."https://github.com".insteadOf "ssh://git@github.com"
terraform init -backend=false
terraform validate
cd -
done
# yamllint enable rule:line-length
.validate-terraform:
variables:
ENVIRONMENTS: prod
TF_VERSION: 1.3.7
image:
name: hashicorp/terraform:$TF_VERSION
entrypoint:
- ""
script:
- git config --global \url."https://${GITLAB_TERRAFORM_DEPLOY_TOKEN_USERNAME}:${GITLAB_TERRAFORM_DEPLOY_TOKEN_SECRET}@gitlab.com/miquido/terraform".insteadOf "ssh://git@gitlab.com/miquido/terraform" # yamllint disable-line rule:line-length
- git config --global url."https://github.com".insteadOf "ssh://git@github.com"
- apk add python3
- apk add --update py-pip
- python3 -m venv venv
- source venv/bin/activate
- pip install miquido_terraform_environments_check
- python3 -m miquido_terraform_environments_check
tags:
- miquido
- docker
.generate_readme:
image: ubuntu:latest
before_script:
- apt update
- apt install make
- apt install curl -y
- apt install git -y
script:
- make init
- make readme
artifacts:
paths:
- README.md
rules:
- if: $CI_COMMIT_BRANCH
tags:
- miquido
- docker
.oidc-login-tf:
script:
- \[ ! -n "ROLE_ARN" \] && echo "Missing ROLE_ARN variable. Exiting" && exit 1
- \[ ! -n "GITLAB_OIDC_TOKEN" \] && echo "Missing GITLAB_OIDC_TOKEN variable. Exiting" && exit 1
- >
export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s"
$(aws sts assume-role-with-web-identity
--role-arn ${ROLE_ARN}
--role-session-name "GitLabRunner-${CI_PROJECT_ID}-${CI_PIPELINE_ID}"
--web-identity-token ${GITLAB_OIDC_TOKEN}
--duration-seconds 3600
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]'
--output text))
.tf-plan:
id_tokens:
GITLAB_OIDC_TOKEN:
aud: https://gitlab.com
variables:
PLAN: plan.cache
PLAN_JSON: plan.json
ROLE_ARN: $TF_ROLE_ARN
TF_VERSION: "1.5"
image:
name: hashicorp/terraform:$TF_VERSION
entrypoint: [""]
before_script:
# yamllint disable rule:line-length
- apk add --no-cache aws-cli
- !reference [.oidc-login-tf, script]
- cd $TF_ROOT
- cp $SECRETS secrets.auto.tfvars || true
- apk add jq
- alias convert_report="jq -r '([.resource_changes[]?.change.actions?]|flatten)|{\"create\":(map(select(.==\"create\"))|length),\"update\":(map(select(.==\"update\"))|length),\"delete\":(map(select(.==\"delete\"))|length)}'"
script:
- terraform init -backend-config=password=${BACKEND_STATE_TOKEN} -backend-config=username=gitlab-ci-token
- terraform plan -out=$PLAN
- terraform show --json $PLAN | convert_report > $PLAN_JSON
artifacts:
paths:
- ${TF_ROOT}/plan.cache
- ${TF_ROOT}/.terraform.lock.hcl
reports:
terraform: ${TF_ROOT}/$PLAN_JSON
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: on_success
tags:
- miquido
- docker
.tf-apply:
id_tokens:
GITLAB_OIDC_TOKEN:
aud: https://gitlab.com
variables:
PLAN: plan.cache
PLAN_JSON: plan.json
TF_VERSION: "1.5"
ROLE_ARN: $TF_ROLE_ARN
image:
name: hashicorp/terraform:$TF_VERSION
entrypoint: [""]
before_script:
- apk add --no-cache aws-cli
- !reference [.oidc-login-tf, script]
- cd $TF_ROOT
- cp $SECRETS secrets.auto.tfvars || true
- apk add jq
script:
- aws sts get-caller-identity
- terraform init -reconfigure -backend-config=password=${BACKEND_STATE_TOKEN} -backend-config=username=gitlab-ci-token
- terraform apply $PLAN
artifacts:
paths:
- ${TF_ROOT}/plan.cache
- ${TF_ROOT}/.terraform.lock.hcl
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: on_success
tags:
- miquido
- docker
.tf-destroy:
id_tokens:
GITLAB_OIDC_TOKEN:
aud: https://gitlab.com
variables:
PLAN: plan.cache
PLAN_JSON: plan.json
TF_VERSION: "1.5"
ROLE_ARN: $TF_ROLE_ARN
image:
name: hashicorp/terraform:$TF_VERSION
entrypoint: [""]
before_script:
- apk add -u --no-cache aws-cli
- !reference [.oidc-login-tf, script]
- cd $TF_ROOT
- cp $SECRETS secrets.auto.tfvars || true
- apk add jq
script:
- aws sts get-caller-identity
- terraform init -reconfigure -backend-config=password=${BACKEND_STATE_TOKEN} -backend-config=username=gitlab-ci-token
- terraform destroy -auto-approve
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
tags:
- miquido
- docker
allow_failure: true