Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
401 lines (363 loc) · 14.5 KB
/
Copy pathaction.yml
File metadata and controls
401 lines (363 loc) · 14.5 KB
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
name: 'iac-cicd'
description: 'Zero-config OpenTofu/Terraform plan-on-PR, apply-on-merge, with state in Cloudflare R2 or AWS S3.'
author: 'ZeroConfig.io'
branding:
icon: 'cloud-lightning'
color: 'orange'
inputs:
tool:
description: "IaC tool to run: 'opentofu' or 'terraform'."
required: false
default: 'opentofu'
tool-version:
description: "Version of the tool to install, e.g. '1.10.0'. 'latest' resolves to the newest release."
required: false
default: 'latest'
working-directory:
description: 'Directory containing the root module.'
required: false
default: '.'
command:
description: "Override auto-detection ('plan' or 'apply'). Default: apply on push events, plan otherwise."
required: false
default: ''
backend:
description: "State backend flavor: 'r2' (Cloudflare R2) or 's3' (AWS S3)."
required: false
default: 'r2'
bucket:
description: 'Bucket used for state storage.'
required: true
access-key-id:
description: 'S3-compatible API access key ID.'
required: false
default: ''
secret-access-key:
description: 'S3-compatible API secret access key.'
required: false
default: ''
aws-role-arn:
description: "IAM role ARN to assume via OIDC. Only meaningful when backend is 's3'. Presence selects OIDC auth over static access-key-id/secret-access-key."
required: false
default: ''
aws-role-session-name:
description: 'Session name used when assuming aws-role-arn via OIDC.'
required: false
default: 'iac-cicd'
account-id:
description: "Cloudflare account ID that owns the bucket. Required when backend is 'r2', unless endpoint is set explicitly."
required: false
default: ''
region:
description: "Bucket region. Required when backend is 's3'. Defaults to 'auto' when backend is 'r2'."
required: false
default: ''
endpoint:
description: "Override the S3-compatible endpoint URL. Auto-derived from account-id when backend is 'r2'; left unset (real AWS S3 endpoints) when backend is 's3', unless you're pointing at a third S3-compatible provider."
required: false
default: ''
state-key:
description: "Object key for the state file. Default: '<owner>/<repo>[/<working-directory>]/terraform.tfstate'."
required: false
default: ''
extra-args:
description: 'Extra CLI flags appended to the plan/apply command.'
required: false
default: ''
comment-on-pr:
description: 'Post/update a sticky PR comment with the plan output.'
required: false
default: 'true'
policy-command:
description: 'Command run against the JSON plan before allowing merge/apply. Receives the plan JSON path via the IAC_PLAN_JSON env var. Nonzero exit fails the check. Empty (default) disables the policy gate.'
required: false
default: ''
github-token:
description: 'Token used to post the PR comment. Defaults to the workflow job token.'
required: false
default: ''
outputs:
exitcode:
description: 'Exit code of the plan/apply command (0 = no changes, 2 = plan has changes).'
value: ${{ steps.run.outputs.exitcode }}
has-changes:
description: "'true' if the plan detected changes."
value: ${{ steps.run.outputs.has-changes }}
runs:
using: 'composite'
steps:
- name: Validate inputs and resolve backend
id: validate
shell: bash
run: |
set -euo pipefail
case "${{ inputs.tool }}" in
opentofu|terraform) ;;
*)
echo "::error::tool must be 'opentofu' or 'terraform', got '${{ inputs.tool }}'"
exit 1
;;
esac
backend="${{ inputs.backend }}"
case "$backend" in
r2|s3) ;;
*)
echo "::error::backend must be 'r2' or 's3', got '$backend'"
exit 1
;;
esac
endpoint="${{ inputs.endpoint }}"
region="${{ inputs.region }}"
access_key_id="${{ inputs.access-key-id }}"
secret_access_key="${{ inputs.secret-access-key }}"
aws_role_arn="${{ inputs.aws-role-arn }}"
if [ "$backend" = "r2" ]; then
if [ -n "$aws_role_arn" ]; then
echo "::error::aws-role-arn is only meaningful when backend is 's3', r2 has no OIDC equivalent"
exit 1
fi
if [ -z "$endpoint" ]; then
if [ -z "${{ inputs.account-id }}" ]; then
echo "::error::account-id is required when backend is 'r2', unless endpoint is set explicitly"
exit 1
fi
endpoint="https://${{ inputs.account-id }}.r2.cloudflarestorage.com"
fi
[ -n "$region" ] || region="auto"
use_path_style=true
skip_checks=true
encrypt=false
if [ -z "$access_key_id" ] || [ -z "$secret_access_key" ]; then
echo "::error::access-key-id and secret-access-key are both required when backend is 'r2'"
exit 1
fi
auth_mode=static
else
if [ -z "$region" ]; then
echo "::error::region is required when backend is 's3'"
exit 1
fi
use_path_style=false
skip_checks=false
encrypt=true
if [ -n "$aws_role_arn" ]; then
if [ -n "$access_key_id" ] || [ -n "$secret_access_key" ]; then
echo "::error::set either aws-role-arn or access-key-id/secret-access-key for backend 's3', not both"
exit 1
fi
auth_mode=oidc
else
if [ -z "$access_key_id" ] || [ -z "$secret_access_key" ]; then
echo "::error::access-key-id and secret-access-key are both required for backend 's3' unless aws-role-arn is set for OIDC"
exit 1
fi
auth_mode=static
fi
fi
echo "endpoint=$endpoint" >> "$GITHUB_OUTPUT"
echo "region=$region" >> "$GITHUB_OUTPUT"
echo "use-path-style=$use_path_style" >> "$GITHUB_OUTPUT"
echo "skip-checks=$skip_checks" >> "$GITHUB_OUTPUT"
echo "encrypt=$encrypt" >> "$GITHUB_OUTPUT"
echo "auth-mode=$auth_mode" >> "$GITHUB_OUTPUT"
- name: Set up OpenTofu
if: inputs.tool == 'opentofu'
uses: opentofu/setup-opentofu@a1320f892987e89d278cc92dc5adc984fb93aca4 # v2.0.2
with:
tofu_version: ${{ inputs.tool-version }}
tofu_wrapper: false
- name: Set up Terraform
if: inputs.tool == 'terraform'
uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1
with:
terraform_version: ${{ inputs.tool-version }}
terraform_wrapper: false
- name: Resolve command and state key
id: resolve
shell: bash
run: |
set -euo pipefail
cmd="${{ inputs.command }}"
if [ -z "$cmd" ]; then
if [ "${{ github.event_name }}" = "push" ]; then
cmd="apply"
else
cmd="plan"
fi
fi
if [ "$cmd" != "plan" ] && [ "$cmd" != "apply" ]; then
echo "::error::command must be 'plan' or 'apply', got '$cmd'"
exit 1
fi
key="${{ inputs.state-key }}"
if [ -z "$key" ]; then
key="${{ github.repository }}"
wd="${{ inputs.working-directory }}"
if [ -n "$wd" ] && [ "$wd" != "." ]; then
key="$key/$wd"
fi
key="$key/terraform.tfstate"
fi
bin="terraform"
if [ "${{ inputs.tool }}" = "opentofu" ]; then
bin="tofu"
fi
echo "command=$cmd" >> "$GITHUB_OUTPUT"
echo "state-key=$key" >> "$GITHUB_OUTPUT"
echo "bin=$bin" >> "$GITHUB_OUTPUT"
- name: Write backend config
shell: bash
run: |
set -euo pipefail
{
echo "bucket = \"${{ inputs.bucket }}\""
echo "key = \"${{ steps.resolve.outputs.state-key }}\""
echo "region = \"${{ steps.validate.outputs.region }}\""
if [ -n "${{ steps.validate.outputs.endpoint }}" ]; then
echo "endpoints = { s3 = \"${{ steps.validate.outputs.endpoint }}\" }"
fi
echo "use_lockfile = true"
echo "use_path_style = ${{ steps.validate.outputs.use-path-style }}"
echo "skip_credentials_validation = ${{ steps.validate.outputs.skip-checks }}"
echo "skip_region_validation = ${{ steps.validate.outputs.skip-checks }}"
echo "skip_requesting_account_id = ${{ steps.validate.outputs.skip-checks }}"
echo "skip_s3_checksum = ${{ steps.validate.outputs.skip-checks }}"
echo "skip_metadata_api_check = ${{ steps.validate.outputs.skip-checks }}"
echo "encrypt = ${{ steps.validate.outputs.encrypt }}"
} > "$RUNNER_TEMP/iac-cicd-backend.hcl"
- name: Configure credentials (OIDC)
if: steps.validate.outputs.auth-mode == 'oidc'
uses: aws-actions/configure-aws-credentials@cbe3b392738ccf3f987d68400dafcf4b0624a56c # v6.2.4
with:
role-to-assume: ${{ inputs.aws-role-arn }}
role-session-name: ${{ inputs.aws-role-session-name }}
aws-region: ${{ steps.validate.outputs.region }}
- name: Configure credentials (static)
if: steps.validate.outputs.auth-mode == 'static'
shell: bash
run: |
set -euo pipefail
echo "AWS_ACCESS_KEY_ID=${{ inputs.access-key-id }}" >> "$GITHUB_ENV"
echo "AWS_SECRET_ACCESS_KEY=${{ inputs.secret-access-key }}" >> "$GITHUB_ENV"
- name: Init
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
set -euo pipefail
"${{ steps.resolve.outputs.bin }}" init -input=false \
-backend-config="$RUNNER_TEMP/iac-cicd-backend.hcl"
- name: Plan
if: steps.resolve.outputs.command == 'plan'
id: plan
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
# keep in sync with the Apply step's plan+show sequence below
set -euo pipefail
set +e
"${{ steps.resolve.outputs.bin }}" plan -input=false -no-color -detailed-exitcode \
-out="$RUNNER_TEMP/iac-cicd-tfplan.bin" \
${{ inputs.extra-args }} | tee "$RUNNER_TEMP/iac-cicd-plan.txt"
code="${PIPESTATUS[0]}"
set -e
echo "exitcode=$code" >> "$GITHUB_OUTPUT"
if [ "$code" = "1" ]; then
echo "::error::plan failed, see log above"
exit 1
fi
if [ "$code" = "2" ]; then
echo "has-changes=true" >> "$GITHUB_OUTPUT"
else
echo "has-changes=false" >> "$GITHUB_OUTPUT"
fi
"${{ steps.resolve.outputs.bin }}" show -json "$RUNNER_TEMP/iac-cicd-tfplan.bin" \
> "$RUNNER_TEMP/iac-cicd-tfplan.json"
- name: Policy check (plan)
if: steps.resolve.outputs.command == 'plan' && inputs.policy-command != ''
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
IAC_PLAN_JSON: ${{ runner.temp }}/iac-cicd-tfplan.json
run: |
set -euo pipefail
if ! (unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN; ${{ inputs.policy-command }}); then
echo "::error::policy check failed"
exit 1
fi
- name: Comment plan on PR
if: |
steps.resolve.outputs.command == 'plan' &&
inputs.comment-on-pr == 'true' &&
github.event_name == 'pull_request'
shell: bash
env:
GH_TOKEN: ${{ inputs.github-token || github.token }}
run: |
set -euo pipefail
marker="<!-- iac-cicd-plan:${{ inputs.working-directory }} -->"
body_file="$RUNNER_TEMP/iac-cicd-comment.md"
plan_file="$RUNNER_TEMP/iac-cicd-plan.txt"
plan_size=$(wc -c < "$plan_file")
cap=55000
{
echo "$marker"
echo "### IaC plan (\`${{ inputs.tool }}\`, \`${{ inputs.working-directory }}\`)"
echo '```diff'
head -c "$cap" "$plan_file"
echo '```'
if [ "$plan_size" -gt "$cap" ]; then
echo ""
echo "_Plan output truncated ($plan_size bytes). Full log: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}_"
fi
} > "$body_file"
repo="${{ github.repository }}"
pr="${{ github.event.pull_request.number }}"
existing=$(gh api "repos/$repo/issues/$pr/comments" --paginate \
| jq -r --arg marker "$marker" '.[] | select(.body | startswith($marker)) | .id' \
| head -n1)
if [ -n "$existing" ]; then
gh api -X PATCH "repos/$repo/issues/comments/$existing" -F body=@"$body_file"
else
gh api -X POST "repos/$repo/issues/$pr/comments" -F body=@"$body_file"
fi
- name: Apply
if: steps.resolve.outputs.command == 'apply'
id: apply
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
# keep in sync with the Plan step's plan+show sequence above
set -euo pipefail
set +e
"${{ steps.resolve.outputs.bin }}" plan -out="$RUNNER_TEMP/iac-cicd-tfplan.bin" -input=false -no-color \
${{ inputs.extra-args }}
code="$?"
set -e
if [ "$code" != "0" ]; then
echo "::error::plan failed, see log above"
exit 1
fi
"${{ steps.resolve.outputs.bin }}" show -json "$RUNNER_TEMP/iac-cicd-tfplan.bin" \
> "$RUNNER_TEMP/iac-cicd-tfplan.json"
if [ -n "${{ inputs.policy-command }}" ]; then
export IAC_PLAN_JSON="$RUNNER_TEMP/iac-cicd-tfplan.json"
if ! (unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN; ${{ inputs.policy-command }}); then
echo "::error::policy check failed"
exit 1
fi
fi
"${{ steps.resolve.outputs.bin }}" apply -input=false -auto-approve "$RUNNER_TEMP/iac-cicd-tfplan.bin"
echo "exitcode=0" >> "$GITHUB_OUTPUT"
echo "has-changes=true" >> "$GITHUB_OUTPUT"
- name: Set outputs
id: run
shell: bash
run: |
set -euo pipefail
if [ "${{ steps.resolve.outputs.command }}" = "plan" ]; then
echo "exitcode=${{ steps.plan.outputs.exitcode }}" >> "$GITHUB_OUTPUT"
echo "has-changes=${{ steps.plan.outputs.has-changes }}" >> "$GITHUB_OUTPUT"
else
echo "exitcode=${{ steps.apply.outputs.exitcode }}" >> "$GITHUB_OUTPUT"
echo "has-changes=${{ steps.apply.outputs.has-changes }}" >> "$GITHUB_OUTPUT"
fi