-
Notifications
You must be signed in to change notification settings - Fork 21
239 lines (236 loc) · 8.8 KB
/
terraform-plan.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
name: Terraform Plan
permissions:
contents: read
on:
workflow_call:
inputs:
ref:
type: string
required: true
environment-key:
type: string
required: true
aws-region:
type: string
required: true
tf-backend-config-file:
type: string
required: true
tf-var-file:
type: string
required: true
artifacts-retention-days:
description: Number of days to retain build artifacts
type: number
default: 90
upload-artifacts:
type: boolean
default: false
concurrency-group:
description: Name of the concurrency group (avoids simultaneous Terraform execution against the same environment)
type: string
default: run_terraform
server-image-tag:
type: string
required: true
server-image-digest:
type: string
required: true
website-artifacts-key:
type: string
required: true
website-artifacts-path:
type: string
required: true
secrets:
aws-access-key-id:
required: true
aws-secret-access-key:
required: true
aws-session-token:
required: true
datadog-api-key:
required: true
datadog-app-key:
required: true
gpg-passphrase:
required: true
outputs:
artifacts-key:
value: ${{ jobs.do.outputs.artifacts-key }}
fmt-outcome:
value: ${{ jobs.do.outputs.fmt_outcome }}
init-outcome:
value: ${{ jobs.do.outputs.init_outcome }}
validate-outcome:
value: ${{ jobs.do.outputs.validate_outcome }}
validate-output:
value: ${{ jobs.do.outputs.validate_output }}
plan-exitcode:
value: ${{ jobs.do.outputs.plan_exitcode }}
plan-outcome:
value: ${{ jobs.do.outputs.plan_outcome }}
plan-output:
value: ${{ jobs.do.outputs.plan_output }}
plan-summary-markdown:
value: ${{ jobs.do.outputs.plan_summary_md }}
jobs:
do:
name: Validate and plan terraform
runs-on: ubuntu-latest
permissions:
contents: read
defaults:
run:
working-directory: terraform
outputs:
artifacts-key: ${{ env.ARTIFACTS_KEY }}
fmt_outcome: ${{ steps.fmt.outcome }}
init_outcome: ${{ steps.init.outcome }}
validate_outcome: ${{ steps.validate.outcome }}
validate_output: |-
stdout:
${{ steps.validate.outputs.stdout }}
-------------------------------------
stderr:
${{ steps.validate.outputs.stderr }}
plan_exitcode: ${{ steps.plan.outputs.exitcode }}
plan_outcome: ${{ steps.plan.outcome }}
plan_output: |-
stdout:
${{ steps.show_plan.outputs.stdout }}
-------------------------------------
stderr:
${{ steps.show_plan.outputs.stderr }}
plan_summary_md: ${{ steps.plan_summary.outputs.markdown_table }}
env:
ARTIFACTS_KEY: terraform-${{ inputs.environment-key }}-${{ inputs.ref }}
AWS_DEFAULT_REGION: ${{ inputs.aws-region }}
AWS_REGION: ${{ inputs.aws-region }}
TF_CLI_ARGS: "-no-color"
TF_IN_AUTOMATION: "true"
TF_INPUT: 0
concurrency:
group: ${{ inputs.concurrency-group }}
cancel-in-progress: false
steps:
- uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
disable-sudo: true
egress-policy: audit
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
ref: ${{ inputs.ref }}
show-progress: 'false'
persist-credentials: 'false'
- name: Set up tf-summarize
uses: kishaningithub/setup-tf-summarize@b752490b135f74e59d82e656a1dab7f11a937f84 # v2.0.0
- name: Validate workflow configuration
if: inputs.upload-artifacts && (env.GPG_PASSPHRASE == '')
run: |
echo 'gpg-passphrase is required when upload-artifacts is true'
exit 1
env:
GPG_PASSPHRASE: ${{ secrets.gpg-passphrase }}
- name: Download website artifacts
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: ${{ inputs.website-artifacts-key }}
path: ${{ inputs.website-artifacts-path }}
- name: Get project TF version
id: get_tf_version
run: echo "TF_VERSION=$(cat .terraform-version | tr -d '[:space:]')" | tee -a $GITHUB_OUTPUT
- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
with:
terraform_version: ${{ steps.get_tf_version.outputs.TF_VERSION }}
- name: Terraform fmt
id: fmt
run: terraform fmt -check -diff -recursive
- name: Get AWS access key ID
id: decrypt-aws-access-key-id
run: |
decrypted=$(gpg -qd --batch --yes --passphrase "$GPG_PASSPHRASE" -o - <(echo "$VALUE" | base64 -d))
echo "::add-mask::${decrypted}"
echo "out=${decrypted}" >> $GITHUB_OUTPUT
env:
GPG_PASSPHRASE: ${{ secrets.gpg-passphrase }}
VALUE: ${{ secrets.aws-access-key-id }}
- name: Get AWS secret access key
id: decrypt-aws-secret-access-key
run: |
decrypted=$(gpg -qd --batch --yes --passphrase "$GPG_PASSPHRASE" -o - <(echo "$VALUE" | base64 -d))
echo "::add-mask::${decrypted}"
echo "out=${decrypted}" >> $GITHUB_OUTPUT
env:
GPG_PASSPHRASE: ${{ secrets.gpg-passphrase }}
VALUE: ${{ secrets.aws-secret-access-key }}
- name: Get AWS session token
id: decrypt-aws-session-token
run: |
decrypted=$(gpg -qd --batch --yes --passphrase "$GPG_PASSPHRASE" -o - <(echo "$VALUE" | base64 -d))
echo "::add-mask::${decrypted}"
echo "out=${decrypted}" >> $GITHUB_OUTPUT
env:
GPG_PASSPHRASE: ${{ secrets.gpg-passphrase }}
VALUE: ${{ secrets.aws-session-token }}
- name: Terraform Init
id: init
run: terraform init
env:
AWS_ACCESS_KEY_ID: "${{ steps.decrypt-aws-access-key-id.outputs.out }}"
AWS_SECRET_ACCESS_KEY: "${{ steps.decrypt-aws-secret-access-key.outputs.out }}"
AWS_SESSION_TOKEN: "${{ steps.decrypt-aws-session-token.outputs.out }}"
TF_CLI_ARGS_init: "-backend-config=${{ inputs.tf-backend-config-file }}"
- name: Terraform Validate
id: validate
run: terraform validate -no-color
- name: Terraform Plan
if: always() && steps.validate.outcome == 'success'
id: plan
run: terraform plan -out="tfplan" -detailed-exitcode
env:
AWS_ACCESS_KEY_ID: "${{ steps.decrypt-aws-access-key-id.outputs.out }}"
AWS_SECRET_ACCESS_KEY: "${{ steps.decrypt-aws-secret-access-key.outputs.out }}"
AWS_SESSION_TOKEN: "${{ steps.decrypt-aws-session-token.outputs.out }}"
GPG_PASSPHRASE: "" # Just in case
TF_CLI_ARGS_plan: "-var-file=${{ inputs.tf-var-file }}"
TF_VAR_version_identifier: ${{ inputs.ref }}
TF_VAR_git_commit_sha: ${{ inputs.ref }}
TF_VAR_datadog_api_key: ${{ secrets.datadog-api-key }}
TF_VAR_datadog_app_key: ${{ secrets.datadog-app-key }}
TF_VAR_api_container_image_tag: "${{ inputs.server-image-tag }}@${{ inputs.server-image-digest }}"
TF_VAR_website_origin_artifacts_dist_path: "${{ inputs.website-artifacts-path }}"
- name: Generate plaintext plan
id: show_plan
run: terraform show tfplan
- name: Generate JSON plan
run: terraform show -json tfplan > tfplan.json
- name: Generate plan summary
id: plan_summary
run: |
SUMMARIZED_PLAN_MD=$(tf-summarize -md tfplan.json)
echo "markdown_table<<ENDOFTFPLANSUMMARYMARKDOWN" >> $GITHUB_OUTPUT
echo "$SUMMARIZED_PLAN_MD" >> $GITHUB_OUTPUT
echo "ENDOFTFPLANSUMMARYMARKDOWN" >> $GITHUB_OUTPUT
rm tfplan.json
echo "$SUMMARIZED_PLAN_MD"
- name: Encrypt terraform plan file
id: encrypt_plan
if: success() && inputs.upload-artifacts
env:
GPG_PASSPHRASE: ${{ secrets.gpg-passphrase }}
run: |
gpg --batch --yes --passphrase "$GPG_PASSPHRASE" -c --cipher-algo AES256 tfplan
rm tfplan
- name: Store terraform artifacts
if: success() && inputs.upload-artifacts
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: ${{ env.ARTIFACTS_KEY }}
path: |
${{ github.workspace }}/terraform
!${{ github.workspace }}/terraform/.terraform
if-no-files-found: error
include-hidden-files: true
retention-days: ${{ inputs.artifacts-retention-days }}
overwrite: true