Skip to content

Commit f9de01e

Browse files
authored
Merge pull request #328 from dflook/tf-cloud-1-6
Update to support Terraform cloud with Terraform 1.6
2 parents 2871e4c + d71de61 commit f9de01e

File tree

12 files changed

+290
-47
lines changed

12 files changed

+290
-47
lines changed

.github/workflows/test-cloud.yaml

Lines changed: 198 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ jobs:
314314
exit 1
315315
fi
316316
317-
if [[ -n "${{ steps.auto_apply.outputs.text_plan_path }}" ]]; then
318-
echo "::error:: text_plan_path should not be set"
317+
if ! grep -q "Terraform will perform the following actions" '${{ steps.auto_apply.outputs.text_plan_path }}'; then
318+
echo "::error:: text_plan_path not set correctly"
319319
exit 1
320320
fi
321321
322-
if [[ -n "${{ steps.auto_apply.outputs.json_plan_path }}" ]]; then
323-
echo "::error:: json_plan_path should not be set"
322+
if [[ ! -f "${{ steps.auto_apply.outputs.json_plan_path }}" ]]; then
323+
echo "::error:: json_plan_path should be set"
324324
exit 1
325325
fi
326326
@@ -378,12 +378,13 @@ jobs:
378378
path: tests/workflows/test-cloud/partial
379379
workspace: ${{ github.head_ref }}-cloud-1
380380

381-
- name: Plan workspace
381+
- name: Plan changes
382382
uses: ./terraform-plan
383383
id: plan
384384
env:
385385
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
386386
with:
387+
label: test-cloud cloud plan
387388
path: tests/workflows/test-cloud/partial
388389
workspace: ${{ github.head_ref }}-cloud-2
389390

@@ -399,18 +400,24 @@ jobs:
399400
exit 1
400401
fi
401402
403+
if [[ ! -f "${{ steps.auto_apply.outputs.json_plan_path }}" ]]; then
404+
echo "::error:: json_plan_path should be set"
405+
exit 1
406+
fi
407+
402408
echo '${{ steps.plan.outputs.run_id }}'
403409
if [[ "${{ steps.plan.outputs.run_id }}" != "run-"* ]]; then
404410
echo "::error:: output run_id not set correctly"
405411
exit 1
406412
fi
407413
408-
- name: Apply workspace
414+
- name: Apply changes
409415
uses: ./terraform-apply
410416
id: apply
411417
env:
412418
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
413419
with:
420+
label: test-cloud cloud plan
414421
path: tests/workflows/test-cloud/partial
415422
workspace: ${{ github.head_ref }}-cloud-2
416423

@@ -425,9 +432,9 @@ jobs:
425432
echo "::error:: text_plan_path not set correctly"
426433
exit 1
427434
fi
428-
429-
if [[ -n "${{ steps.apply.outputs.json_plan_path }}" ]]; then
430-
echo "::error:: json_plan_path should not be set"
435+
436+
if [[ ! -f "${{ steps.apply.outputs.json_plan_path }}" ]]; then
437+
echo "::error:: json_plan_path should be set"
431438
exit 1
432439
fi
433440
@@ -437,6 +444,188 @@ jobs:
437444
exit 1
438445
fi
439446
447+
- name: Plan no changes
448+
uses: ./terraform-plan
449+
id: plan-no-changes
450+
env:
451+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
452+
with:
453+
label: test-cloud cloud plan-no-changes
454+
path: tests/workflows/test-cloud/partial
455+
workspace: ${{ github.head_ref }}-cloud-2
456+
457+
- name: Apply no changes
458+
uses: ./terraform-apply
459+
id: apply-no-changes
460+
env:
461+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
462+
with:
463+
label: test-cloud cloud plan-no-changes
464+
path: tests/workflows/test-cloud/partial
465+
workspace: ${{ github.head_ref }}-cloud-2
466+
467+
- name: Verify apply with no changes
468+
run: |
469+
if [[ "${{ steps.plan-no-changes.outputs.changes }}" != "false" ]]; then
470+
echo "::error:: changes output not set correctly"
471+
exit 1
472+
fi
473+
474+
if [[ "${{ steps.apply-no-changes.outputs.len }}" != "5" ]]; then
475+
echo "::error:: Variables not set correctly"
476+
exit 1
477+
fi
478+
479+
if ! grep -q "No changes. Your infrastructure matches the configuration." '${{ steps.plan-no-changes.outputs.text_plan_path }}'; then
480+
echo "::error:: text_plan_path not set correctly"
481+
exit 1
482+
fi
483+
484+
if ! grep -q "No changes. Your infrastructure matches the configuration." '${{ steps.apply-no-changes.outputs.text_plan_path }}'; then
485+
echo "::error:: text_plan_path not set correctly"
486+
exit 1
487+
fi
488+
489+
if [[ ! -f "${{ steps.plan-no-changes.outputs.json_plan_path }}" ]]; then
490+
echo "::error:: json_plan_path should be set"
491+
exit 1
492+
fi
493+
494+
if [[ ! -f "${{ steps.apply-no-changes.outputs.json_plan_path }}" ]]; then
495+
echo "::error:: json_plan_path should be set"
496+
exit 1
497+
fi
498+
499+
echo '${{ steps.plan-no-changes.outputs.run_id }}'
500+
if [[ "${{ steps.plan-no-changes.outputs.run_id }}" != "run-"* ]]; then
501+
echo "::error:: output run_id not set correctly"
502+
exit 1
503+
fi
504+
505+
echo '${{ steps.apply-no-changes.outputs.run_id }}'
506+
if [[ "${{ steps.apply-no-changes.outputs.run_id }}" != "run-"* ]]; then
507+
echo "::error:: output run_id not set correctly"
508+
exit 1
509+
fi
510+
511+
- name: Saved plan with changes
512+
uses: ./terraform-plan
513+
id: saved-plan-changes
514+
env:
515+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
516+
with:
517+
label: test-cloud cloud save-plan-changes
518+
path: tests/workflows/test-cloud/partial
519+
workspace: ${{ github.head_ref }}-cloud-2
520+
variables: |
521+
length=8
522+
523+
- name: Saved apply with changes
524+
uses: ./terraform-apply
525+
id: saved-apply-changes
526+
env:
527+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
528+
with:
529+
label: test-cloud cloud save-plan-changes
530+
path: tests/workflows/test-cloud/partial
531+
plan_path: ${{ steps.saved-plan-changes.outputs.plan_path }}
532+
workspace: ${{ github.head_ref }}-cloud-2
533+
variables: |
534+
length=8
535+
536+
- name: Verify saved apply with changes
537+
run: |
538+
if [[ "${{ steps.saved-plan-changes.outputs.changes }}" != "true" ]]; then
539+
echo "::error:: changes output not set correctly"
540+
exit 1
541+
fi
542+
543+
if [[ "${{ steps.saved-apply-changes.outputs.len }}" != "8" ]]; then
544+
echo "::error:: Variables not set correctly"
545+
exit 1
546+
fi
547+
548+
if ! grep -q "Terraform will perform the following actions" '${{ steps.saved-plan-changes.outputs.text_plan_path }}'; then
549+
echo "::error:: text_plan_path not set correctly"
550+
exit 1
551+
fi
552+
553+
if [[ ! -f "${{ steps.saved-plan-changes.outputs.json_plan_path }}" ]]; then
554+
echo "::error:: json_plan_path should be set"
555+
exit 1
556+
fi
557+
558+
echo '${{ steps.saved-plan-changes.outputs.run_id }}'
559+
if [[ "${{ steps.saved-plan-changes.outputs.run_id }}" != "run-"* ]]; then
560+
echo "::error:: output run_id not set correctly"
561+
exit 1
562+
fi
563+
564+
echo '${{ steps.saved-apply-changes.outputs.run_id }}'
565+
if [[ "${{ steps.saved-apply-changes.outputs.run_id }}" != "run-"* ]]; then
566+
echo "::error:: output run_id not set correctly"
567+
exit 1
568+
fi
569+
570+
- name: Saved plan with no changes
571+
uses: ./terraform-plan
572+
id: saved-plan-no-changes
573+
env:
574+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
575+
with:
576+
label: test-cloud cloud save-plan-no-changes
577+
path: tests/workflows/test-cloud/partial
578+
workspace: ${{ github.head_ref }}-cloud-2
579+
variables: |
580+
length=8
581+
582+
- name: Saved apply with no changes
583+
uses: ./terraform-apply
584+
id: saved-apply-no-changes
585+
env:
586+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
587+
with:
588+
label: test-cloud cloud save-plan-no-changes
589+
path: tests/workflows/test-cloud/partial
590+
plan_path: ${{ steps.saved-plan-no-changes.outputs.plan_path }}
591+
workspace: ${{ github.head_ref }}-cloud-2
592+
variables: |
593+
length=8
594+
595+
- name: Verify saved apply with no changes
596+
run: |
597+
if [[ "${{ steps.saved-plan-no-changes.outputs.changes }}" != "false" ]]; then
598+
echo "::error:: changes output not set correctly"
599+
exit 1
600+
fi
601+
602+
if [[ "${{ steps.saved-apply-no-changes.outputs.len }}" != "8" ]]; then
603+
echo "::error:: Variables not set correctly"
604+
exit 1
605+
fi
606+
607+
if ! grep -q "No changes. Your infrastructure matches the configuration." '${{ steps.saved-plan-no-changes.outputs.text_plan_path }}'; then
608+
echo "::error:: text_plan_path not set correctly"
609+
exit 1
610+
fi
611+
612+
if [[ ! -f "${{ steps.saved-plan-no-changes.outputs.json_plan_path }}" ]]; then
613+
echo "::error:: json_plan_path should be set"
614+
exit 1
615+
fi
616+
617+
echo '${{ steps.saved-plan-no-changes.outputs.run_id }}'
618+
if [[ "${{ steps.saved-plan-no-changes.outputs.run_id }}" != "run-"* ]]; then
619+
echo "::error:: output run_id not set correctly"
620+
exit 1
621+
fi
622+
623+
echo '${{ steps.saved-apply-no-changes.outputs.run_id }}'
624+
if [[ "${{ steps.saved-apply-no-changes.outputs.run_id }}" != "run-"* ]]; then
625+
echo "::error:: output run_id not set correctly"
626+
exit 1
627+
fi
628+
440629
- name: Destroy the last workspace
441630
uses: ./terraform-destroy-workspace
442631
with:

.github/workflows/test-version.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ jobs:
503503

504504
- name: Check the version
505505
run: |
506-
if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.6"* ]]; then
506+
if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.7"* ]]; then
507507
echo "::error:: Latest version was not used"
508508
exit 1
509509
fi

image/actions.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@ function plan() {
456456
# shellcheck disable=SC2034
457457
PLAN_EXIT=${PIPESTATUS[0]}
458458
set -e
459+
460+
if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" || "$TERRAFORM_BACKEND_TYPE" == "cloud" ]]; then
461+
if remote-run-id "$STEP_TMP_DIR/terraform_plan.stdout" >"$STEP_TMP_DIR/remote-run-id.stdout" 2>"$STEP_TMP_DIR/remote-run-id.stderr"; then
462+
RUN_ID="$(<"$STEP_TMP_DIR/remote-run-id.stdout")"
463+
set_output run_id "$RUN_ID"
464+
else
465+
debug_log "Failed to get remote run-id"
466+
debug_file "$STEP_TMP_DIR/remote-run-id.stderr"
467+
fi
468+
fi
459469
}
460470

461471
function destroy() {

image/entrypoints/apply.sh

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ set-plan-args
1111
PLAN_OUT="$STEP_TMP_DIR/plan.out"
1212

1313
function update_comment() {
14-
if ! github_pr_comment "$@" 2>"$STEP_TMP_DIR/github_pr_comment.stderr"; then
15-
debug_file "$STEP_TMP_DIR/github_pr_comment.stderr"
16-
else
17-
debug_file "$STEP_TMP_DIR/github_pr_comment.stderr"
14+
if [[ -v TERRAFORM_ACTIONS_GITHUB_TOKEN ]]; then
15+
16+
if ! github_pr_comment "$@" 2>"$STEP_TMP_DIR/github_pr_comment.stderr"; then
17+
debug_file "$STEP_TMP_DIR/github_pr_comment.stderr"
18+
else
19+
debug_file "$STEP_TMP_DIR/github_pr_comment.stderr"
20+
fi
21+
1822
fi
1923
}
2024

21-
if [[ -v TERRAFORM_ACTIONS_GITHUB_TOKEN ]]; then
22-
update_comment begin-apply
23-
fi
25+
update_comment begin-apply
2426

2527
exec 3>&1
2628

@@ -29,12 +31,14 @@ function apply() {
2931

3032
set +e
3133
if [[ -n "$PLAN_OUT" ]]; then
34+
3235
# shellcheck disable=SC2086
33-
debug_log $TOOL_COMMAND_NAME apply -input=false -no-color -auto-approve -lock-timeout=300s $PARALLEL_ARG $PLAN_OUT
36+
debug_log $TOOL_COMMAND_NAME apply -input=false -no-color -lock-timeout=300s $PARALLEL_ARG $PLAN_OUT
3437
# shellcheck disable=SC2086
35-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME apply -input=false -no-color -auto-approve -lock-timeout=300s $PARALLEL_ARG $PLAN_OUT) \
38+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME apply -input=false -no-color -lock-timeout=300s $PARALLEL_ARG $PLAN_OUT) \
3639
2>"$STEP_TMP_DIR/terraform_apply.stderr" \
37-
| $TFMASK
40+
| $TFMASK \
41+
| tee "$STEP_TMP_DIR/terraform_apply.stdout"
3842
APPLY_EXIT=${PIPESTATUS[0]}
3943
>&2 cat "$STEP_TMP_DIR/terraform_apply.stderr"
4044
else
@@ -51,17 +55,25 @@ function apply() {
5155
APPLY_EXIT=${PIPESTATUS[0]}
5256
>&2 cat "$STEP_TMP_DIR/terraform_apply.stderr"
5357

54-
if remote-run-id "$STEP_TMP_DIR/terraform_apply.stdout" >"$STEP_TMP_DIR/remote-run-id.stdout" 2>"$STEP_TMP_DIR/remote-run-id.stderr"; then
58+
fi
59+
set -e
60+
61+
if [[ "$TERRAFORM_BACKEND_TYPE" == "cloud" || "$TERRAFORM_BACKEND_TYPE" == "remote" ]]; then
62+
if remote-run-id "$STEP_TMP_DIR/terraform_apply.stdout" "$STEP_TMP_DIR/terraform_apply.stderr" >"$STEP_TMP_DIR/remote-run-id.stdout" 2>"$STEP_TMP_DIR/remote-run-id.stderr"; then
5563
RUN_ID="$(<"$STEP_TMP_DIR/remote-run-id.stdout")"
5664
set_output run_id "$RUN_ID"
5765
else
5866
debug_log "Failed to get remote run-id"
5967
debug_file "$STEP_TMP_DIR/remote-run-id.stderr"
6068
fi
6169
fi
62-
set -e
6370

64-
if [[ $APPLY_EXIT -eq 0 ]]; then
71+
if [[ "$TERRAFORM_BACKEND_TYPE" == "cloud" && $APPLY_EXIT -ne 0 ]] && grep -q "Error: Saved plan has no changes" "$STEP_TMP_DIR/terraform_apply.stderr"; then
72+
# Not really an error then is it?
73+
APPLY_EXIT=0
74+
output
75+
update_comment cloud-no-changes-to-apply "$STEP_TMP_DIR/terraform_output.json"
76+
elif [[ $APPLY_EXIT -eq 0 ]]; then
6577
output
6678
update_comment apply-complete "$STEP_TMP_DIR/terraform_output.json"
6779
else
@@ -134,7 +146,13 @@ fi
134146

135147
### Apply the plan
136148

137-
if [[ "$INPUT_AUTO_APPROVE" == "true" || $PLAN_EXIT -eq 0 ]]; then
149+
if [[ "$TERRAFORM_BACKEND_TYPE" == "cloud" && $PLAN_EXIT -eq 0 ]]; then
150+
# Terraform cloud will just error if we try to apply a plan with no changes
151+
echo "No changes to apply"
152+
output
153+
update_comment cloud-no-changes-to-apply "$STEP_TMP_DIR/terraform_output.json"
154+
155+
elif [[ "$INPUT_AUTO_APPROVE" == "true" || $PLAN_EXIT -eq 0 ]]; then
138156
echo "Automatically approving plan"
139157
apply
140158

image/entrypoints/plan.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ fi
2727

2828
cat "$STEP_TMP_DIR/terraform_plan.stderr"
2929

30-
if [[ -z "$PLAN_OUT" ]]; then
31-
if remote-run-id "$STEP_TMP_DIR/terraform_plan.stdout" >"$STEP_TMP_DIR/remote-run-id.stdout" 2>"$STEP_TMP_DIR/remote-run-id.stderr"; then
32-
RUN_ID="$(<"$STEP_TMP_DIR/remote-run-id.stdout")"
33-
set_output run_id "$RUN_ID"
34-
else
35-
debug_log "Failed to get remote run-id"
36-
debug_file "$STEP_TMP_DIR/remote-run-id.stderr"
37-
fi
38-
fi
39-
4030
if [[ "$GITHUB_EVENT_NAME" == "pull_request" || "$GITHUB_EVENT_NAME" == "issue_comment" || "$GITHUB_EVENT_NAME" == "pull_request_review_comment" || "$GITHUB_EVENT_NAME" == "pull_request_target" || "$GITHUB_EVENT_NAME" == "pull_request_review" || "$GITHUB_EVENT_NAME" == "repository_dispatch" ]]; then
4131
if [[ "$INPUT_ADD_GITHUB_COMMENT" == "true" || "$INPUT_ADD_GITHUB_COMMENT" == "changes-only" || "$INPUT_ADD_GITHUB_COMMENT" == "always-new" ]]; then
4232

image/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
install_requires=[
2323
'requests',
24+
'requests-cache',
2425
'python-hcl2',
2526
'canonicaljson'
2627
]

0 commit comments

Comments
 (0)