-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
425 lines (403 loc) · 15.9 KB
/
action.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
name: Run Terraform
description: Runs various Terraform commands using the Terraform CLI.
inputs:
github-token:
description: The GitHub token to use
default: ${{ github.token }}
required: false
tf-cli:
description: The actual Terraform cli to use
default: terraform
required: false
directory:
description: The directory containing the Terraform configuration
required: false
default: .
workspace:
description: The Terraform workspace to use
required: false
workspace-select:
description: |
Whether to run terraform workspace select. Requires workspace input and the TF_WORKSPACE env var to not be set.
required: false
default: 'true'
workspace-create-if-missing:
description: Whether to create the workspace if it does not exist. Requires workspace-select input to be true.
required: false
default: 'true'
init:
description: Whether to run terraform init
required: false
default: 'true'
init-args:
description: Additional arguments to pass to terraform init
required: false
tfbackend-content:
description: The content of the backend file to use.
required: false
fmt:
description: Whether to run terraform fmt
required: false
default: 'true'
validate:
description: Whether to run terraform validate
required: false
default: 'true'
action:
description: The main terraform action to run ("plan", "apply", "test", or "output")
required: false
action-args:
description: Additional arguments to pass to terraform plan, apply, test, or output
required: false
destroy:
description: Whether to destroy the infrastructure
required: false
default: 'false'
output:
description: Whether to run terraform output separate from the main action (for logging purposes)
required: false
default: 'false'
output-args:
description: Additional arguments to pass to terraform output (for the output input)
required: false
metadata:
description: >
Whether to run terraform output in JSON format and produce a flattened "metadata" output that can be used as json
tfvars.
required: false
default: 'false'
tfvars-content:
description: The content of the variable file to use.
required: false
tfvars-json-content:
description: The content of the variable file to use in JSON format.
required: false
plan-file-name:
description: The name to the plan file to use. If not provided, a plan file will not be used.
required: false
encrypted-plan-password:
description: The password to use to encrypt the plan artifact. If not provided, the plan will not be encrypted.
required: false
plan-retention-days:
description: >
Duration after which the plan artifact will expire in days.
0 means using the default value defined in the repository settings.
Actual values can range from 1 to 90 (public repositories) or 400 (private repositories).
required: false
default: '0'
comment:
description: Whether to comment on the PR with the results
required: false
default: 'true'
comment-header:
description: The header to use in the PR comment
required: false
comment-identifier:
description: The identifier to use in the PR comment
required: false
outputs:
fmt-outcome:
description: The outcome of the terraform fmt command
value: ${{ steps.fmt.outputs.outcome }}
fmt-stdout:
description: The stdout of the terraform fmt command
value: ${{ steps.fmt.outputs.stdout }}
fmt-stderr:
description: The stderr of the terraform fmt command
value: ${{ steps.fmt.outputs.stderr }}
init-outcome:
description: The outcome of the terraform init command
value: ${{ steps.init.outputs.outcome }}
init-stdout:
description: The stdout of the terraform init command
value: ${{ steps.init.outputs.stdout }}
init-stderr:
description: The stderr of the terraform init command
value: ${{ steps.init.outputs.stderr }}
workspace-outcome:
description: The outcome of the terraform workspace select command
value: ${{ steps.workspace.outputs.outcome }}
workspace-stdout:
description: The stdout of the terraform workspace select command
value: ${{ steps.workspace.outputs.stdout }}
workspace-stderr:
description: The stderr of the terraform workspace select command
value: ${{ steps.workspace.outputs.stderr }}
validate-outcome:
description: The outcome of the terraform validate command
value: ${{ steps.validate.outputs.outcome }}
validate-stdout:
description: The stdout of the terraform validate command
value: ${{ steps.validate.outputs.stdout }}
validate-stderr:
description: The stderr of the terraform validate command
value: ${{ steps.validate.outputs.stderr }}
test-outcome:
description: The outcome of the terraform test command
value: ${{ steps.test.outputs.outcome }}
test-stdout:
description: The stdout of the terraform test command
value: ${{ steps.test.outputs.stdout }}
test-stderr:
description: The stderr of the terraform test command
value: ${{ steps.test.outputs.stderr }}
plan-outcome:
description: The outcome of the terraform plan command
value: ${{ steps.plan.outputs.outcome }}
plan-stdout:
description: The stdout of the terraform plan command
value: ${{ steps.plan.outputs.stdout }}
plan-stderr:
description: The stderr of the terraform plan command
value: ${{ steps.plan.outputs.stderr }}
apply-outcome:
description: The outcome of the terraform apply command
value: ${{ steps.apply.outputs.outcome }}
apply-stdout:
description: The stdout of the terraform apply command
value: ${{ steps.apply.outputs.stdout }}
apply-stderr:
description: The stderr of the terraform apply command
value: ${{ steps.apply.outputs.stderr }}
output-outcome:
description: The outcome of the terraform output command
value: ${{ steps.output.outputs.outcome }}
output-stdout:
description: The stdout of the terraform output command
value: ${{ steps.output.outputs.stdout }}
output-stderr:
description: The stderr of the terraform output command
value: ${{ steps.output.outputs.stderr }}
metadata:
description: >
The metadata (output vars in JSON format) from the terraform state
value: ${{ steps.metadata.outputs.metadata }}
runs:
using: composite
steps:
- name: Create tfbackend file
if: inputs.tfbackend-content != ''
shell: bash
run: echo '${{ inputs.tfbackend-content }}' > ${{ github.action_path }}/action.tfbackend
- name: Create tfvars variable files
env:
tfvarsContent: ${{ inputs.tfvars-content }}
tfvarsJsonContent: ${{ inputs.tfvars-json-content }}
shell: bash
run: |
if [ -n "$tfvarsContent" ]; then
echo "$tfvarsContent" > ${{ inputs.directory }}/action-variables.auto.tfvars
fi
if [ -n "$tfvarsJsonContent" ]; then
echo "$tfvarsJsonContent" > ${{ inputs.directory }}/action-variables.auto.tfvars.json
fi
- name: ${{ inputs.tf-cli }} fmt
id: fmt
if: fromJSON(inputs.fmt)
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} fmt -check -diff \
${{ github.event_name == 'pull_request' && '-no-color' || '' }}
- name: ${{ inputs.tf-cli }} init
id: init
if: fromJSON(inputs.init)
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} init -input=false \
${{ github.event_name == 'pull_request' && '-no-color' || '' }} \
${{ inputs.tfbackend-content != '' && format('-backend-config={0}/action.tfbackend', github.action_path) || '' }} \
${{ inputs.init-args }}
- name: ${{ inputs.tf-cli }} workspace select
id: workspace
if: fromJSON(inputs.workspace-select) && inputs.workspace && !env.TF_WORKSPACE
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} workspace select \
${{ fromJSON(inputs.workspace-create-if-missing) && '-or-create' || '' }} \
${{ inputs.workspace }} \
${{ github.event_name == 'pull_request' && '-no-color' || '' }}
- name: ${{ inputs.tf-cli }} validate
id: validate
if: fromJSON(inputs.validate)
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} validate \
${{ github.event_name == 'pull_request' && '-no-color' || '' }}
- name: ${{ inputs.tf-cli }} test
id: test
if: inputs.action == 'test'
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} test \
${{ github.event_name == 'pull_request' && '-no-color' || '' }} \
${{ inputs.action-args }}
- name: ${{ inputs.tf-cli }} plan
id: plan
if: inputs.action == 'plan'
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} plan -input=false \
${{ github.event_name == 'pull_request' && '-no-color' || '' }} \
${{ fromJSON(inputs.destroy) && '-destroy' || '' }} \
${{ inputs.plan-file-name && format('-out={0}', inputs.plan-file-name) || '' }} \
${{ inputs.action-args }}
- name: Encrypt Plan
if: inputs.action == 'plan' && inputs.plan-file-name && inputs.encrypted-plan-password
shell: bash
working-directory: ${{ inputs.directory }}
run: |
echo "Compressing and encrypting plan file"
tar cf - ${{ inputs.plan-file-name }} \
| 7z a -si -t7z -mx=0 -mhe=on -p"${{ inputs.encrypted-plan-password }}" artifact.tar.7z
- name: Upload Plan
if: inputs.action == 'plan' && inputs.plan-file-name
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.plan-file-name }}
path: ${{ inputs.directory }}/${{ inputs.encrypted-plan-password && 'artifact.tar.7z' || inputs.plan-file-name }}
if-no-files-found: error
compression-level: 0
retention-days: ${{ inputs.plan-retention-days }}
overwrite: true
- name: Create Temporary Directory for Encrypted Plan
if: inputs.action == 'apply' && inputs.plan-file-name && inputs.encrypted-plan-password
shell: bash
run: |
echo "Creating temporary destination directory for encrypted plan"
temp_dir=$(mktemp -d /tmp/XXXXXXXXX)
echo "temp_dir=$temp_dir" >> "$GITHUB_ENV"
- name: Download Plan
if: inputs.action == 'apply' && inputs.plan-file-name
uses: actions/download-artifact@v4
with:
name: ${{ inputs.plan-file-name }}
path: ${{ env.temp_dir || inputs.directory }}
- name: Decrypt Plan
if: inputs.action == 'apply' && inputs.plan-file-name && inputs.encrypted-plan-password
shell: bash
run: |
echo "Decrypting and extracting plan file"
7z x -so "${{ env.temp_dir }}/artifact.tar.7z" -p"${{ inputs.encrypted-plan-password }}" \
| tar xf - -C "${{ inputs.directory }}"
- name: ${{ inputs.tf-cli }} apply
id: apply
if: inputs.action == 'apply'
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} apply -input=false -auto-approve \
${{ github.event_name == 'pull_request' && '-no-color' || '' }} \
${{ fromJSON(inputs.destroy) && '-destroy' || '' }} \
${{ inputs.action-args }} \
${{ inputs.plan-file-name }}
- name: ${{ inputs.tf-cli }} output
id: output
if: inputs.action == 'output'
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} output \
${{ github.event_name == 'pull_request' && '-no-color' || '' }} \
${{ inputs.action-args }}
- name: Build PR Comment
id: comment
if: always() && fromJSON(inputs.comment) && github.event_name == 'pull_request'
uses: actions/github-script@v7
env:
# Capture the stdout/stderr in env vars to get them from process in the script to avoid unescaped quote issues
header: ${{ inputs.comment-header }}
fmtStdout: ${{ steps.fmt.outputs.stdout }}
fmtStderr: ${{ steps.fmt.outputs.stderr }}
initStdout: ${{ steps.init.outputs.stdout }}
initStderr: ${{ steps.init.outputs.stderr }}
workspaceStdout: ${{ steps.workspace.outputs.stdout }}
workspaceStderr: ${{ steps.workspace.outputs.stderr }}
validateStdout: ${{ steps.validate.outputs.stdout }}
validateStderr: ${{ steps.validate.outputs.stderr }}
testStdout: ${{ steps.test.outputs.stdout }}
testStderr: ${{ steps.test.outputs.stderr }}
planStdout: ${{ steps.plan.outputs.stdout }}
planStderr: ${{ steps.plan.outputs.stderr }}
outputStdout: ${{ steps.output.outputs.stdout }}
outputStderr: ${{ steps.output.outputs.stderr }}
with:
github-token: ${{ inputs.github-token }}
result-encoding: string
script: |
const content = {
header: `${process.env.header}`,
fmt: {
enabled: ${{ inputs.fmt }},
outcome: "${{ steps.fmt.outcome }}",
stdout: `${process.env.fmtStdout}`,
stderr: `${process.env.fmtStderr}`
},
init: {
enabled: ${{ inputs.init }},
outcome: "${{ steps.init.outcome }}",
stdout: `${process.env.initStdout}`,
stderr: `${process.env.initStderr}`
},
workspace: {
enabled: ${{ fromJSON(inputs.workspace-select) && !!inputs.workspace && !env.TF_WORKSPACE }},
outcome: "${{ steps.workspace.outcome }}",
stdout: `${process.env.workspaceStdout}`,
stderr: `${process.env.workspaceStderr}`
},
validate: {
enabled: ${{ inputs.validate }},
outcome: "${{ steps.validate.outcome }}",
stdout: `${process.env.validateStdout}`,
stderr: `${process.env.validateStderr}`
},
test: {
enabled: ${{ inputs.action == 'test' }},
outcome: "${{ steps.test.outcome }}",
stdout: `${process.env.testStdout}`,
stderr: `${process.env.testStderr}`
},
plan: {
enabled: ${{ inputs.action == 'plan' }},
outcome: "${{ steps.plan.outcome }}",
stdout: `${process.env.planStdout}`,
stderr: `${process.env.planStderr}`
},
output: {
enabled: ${{ inputs.action == 'output' }},
outcome: "${{ steps.output.outcome }}",
stdout: `${process.env.outputStdout}`,
stderr: `${process.env.outputStderr}`
}
}
const terraformComment = require('${{ github.action_path }}/terraform_comment.js')
return terraformComment(context, content)
- name: PR Comment
uses: 7Factor/action-pr-comment@v1
if: always() && fromJSON(inputs.comment) && github.event_name == 'pull_request'
with:
github-token: ${{ inputs.github-token }}
comment-identifier: ${{ inputs.comment-identifier }}
comment-body: ${{ steps.comment.outputs.result }}
- name: ${{ inputs.tf-cli }} output
if: fromJSON(inputs.output)
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} output \
${{ inputs.output-args }}
- name: ${{ inputs.tf-cli }} output
id: metadata
if: fromJSON(inputs.metadata)
working-directory: ${{ inputs.directory }}
shell: bash
run: |
${{ inputs.tf-cli }} output -no-color -json \
> ${{ github.action_path }}/output
set -e
echo "metadata=$(cat ${{ github.action_path }}/output | jq -c 'with_entries(.value |= .value)')" >> "$GITHUB_OUTPUT"