Skip to content

Commit 9d41ad3

Browse files
authored
Merge pull request #138 from dflook/apply-plan-outputs
Add `json_plan_path` and `text_plan_path` for dflook/terraform-apply
2 parents 3404f39 + 066c1c3 commit 9d41ad3

File tree

7 files changed

+230
-40
lines changed

7 files changed

+230
-40
lines changed

.github/workflows/test-apply.yaml

Lines changed: 152 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ jobs:
2626
echo "::error:: output my_string not set correctly"
2727
exit 1
2828
fi
29+
30+
if [[ $(jq -r .output_changes.my_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
31+
echo "::error:: json_plan_path not set correctly"
32+
exit 1
33+
fi
34+
35+
if ! grep -q "No changes" '${{ steps.output.outputs.text_plan_path }}'; then
36+
echo "::error:: text_plan_path not set correctly"
37+
exit 1
38+
fi
2939
3040
apply_error:
3141
runs-on: ubuntu-latest
@@ -53,6 +63,17 @@ jobs:
5363
echo "::error:: failure-reason not set correctly"
5464
exit 1
5565
fi
66+
67+
if [[ -n "${{ steps.apply.outputs.json_plan_path }}" ]]; then
68+
echo "::error:: json_plan_path should not be set"
69+
exit 1
70+
fi
71+
72+
if [[ -n "${{ steps.apply.outputs.text_plan_path }}" ]]; then
73+
echo "::error:: text_plan_path should not be set"
74+
exit 1
75+
fi
76+
5677
5778
apply_apply_error:
5879
runs-on: ubuntu-latest
@@ -90,6 +111,16 @@ jobs:
90111
echo "::error:: failure-reason not set correctly"
91112
exit 1
92113
fi
114+
115+
if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "0.2" ]]; then
116+
echo "::error:: json_plan_path not set correctly"
117+
exit 1
118+
fi
119+
120+
if ! grep -q "Terraform will perform the following actions" '${{ steps.apply.outputs.text_plan_path }}'; then
121+
echo "::error:: text_plan_path not set correctly"
122+
exit 1
123+
fi
93124
94125
apply_no_token:
95126
runs-on: ubuntu-latest
@@ -143,6 +174,39 @@ jobs:
143174
echo "::error:: output s not set correctly"
144175
exit 1
145176
fi
177+
178+
if [[ $(jq -r .output_changes.output_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
179+
echo "::error:: json_plan_path not set correctly"
180+
exit 1
181+
fi
182+
183+
if ! grep -q "Terraform will perform the following actions" '${{ steps.output.outputs.text_plan_path }}'; then
184+
echo "::error:: text_plan_path not set correctly"
185+
exit 1
186+
fi
187+
188+
- name: Apply
189+
uses: ./terraform-apply
190+
id: output
191+
with:
192+
path: tests/apply/changes
193+
194+
- name: Verify outputs
195+
run: |
196+
if [[ "${{ steps.output.outputs.output_string }}" != "the_string" ]]; then
197+
echo "::error:: output s not set correctly"
198+
exit 1
199+
fi
200+
201+
if [[ $(jq -r .format_version "${{ steps.output.outputs.json_plan_path }}") != "0.2" ]]; then
202+
echo "::error:: json_plan_path not set correctly"
203+
exit 1
204+
fi
205+
206+
if ! grep -q "No changes" '${{ steps.output.outputs.text_plan_path }}'; then
207+
echo "::error:: text_plan_path not set correctly"
208+
exit 1
209+
fi
146210
147211
apply_variables:
148212
runs-on: ubuntu-latest
@@ -215,6 +279,16 @@ jobs:
215279
echo "::error:: output complex_output not set correctly"
216280
exit 1
217281
fi
282+
283+
if [[ $(jq -r .output_changes.output_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
284+
echo "::error:: json_plan_path not set correctly"
285+
exit 1
286+
fi
287+
288+
if ! grep -q "Terraform will perform the following actions" '${{ steps.output.outputs.text_plan_path }}'; then
289+
echo "::error:: text_plan_path not set correctly"
290+
exit 1
291+
fi
218292
219293
backend_config_12:
220294
runs-on: ubuntu-latest
@@ -246,6 +320,16 @@ jobs:
246320
echo "::error:: output from backend_config file not set correctly"
247321
exit 1
248322
fi
323+
324+
if [[ $(jq -r .output_changes.test.actions[0] "${{ steps.backend_config_file_12.outputs.json_plan_path }}") != "create" ]]; then
325+
echo "::error:: json_plan_path not set correctly"
326+
exit 1
327+
fi
328+
329+
if ! grep -q "No changes" '${{ steps.backend_config_file_12.outputs.text_plan_path }}'; then
330+
echo "::error:: text_plan_path not set correctly"
331+
exit 1
332+
fi
249333
250334
- name: Plan
251335
uses: ./terraform-plan
@@ -272,6 +356,16 @@ jobs:
272356
echo "::error:: Output from backend_config not set correctly"
273357
exit 1
274358
fi
359+
360+
if [[ $(jq -r .output_changes.test.actions[0] "${{ steps.backend_config_12.outputs.json_plan_path }}") != "create" ]]; then
361+
echo "::error:: json_plan_path not set correctly"
362+
exit 1
363+
fi
364+
365+
if ! grep -q "No changes" '${{ steps.backend_config_file_12.outputs.text_plan_path }}'; then
366+
echo "::error:: text_plan_path not set correctly"
367+
exit 1
368+
fi
275369
276370
backend_config_13:
277371
runs-on: ubuntu-latest
@@ -303,6 +397,16 @@ jobs:
303397
echo "::error:: output from backend_config file not set correctly"
304398
exit 1
305399
fi
400+
401+
if [[ $(jq -r .output_changes.test.actions[0] "${{ steps.backend_config_file_13.outputs.json_plan_path }}") != "create" ]]; then
402+
echo "::error:: json_plan_path not set correctly"
403+
exit 1
404+
fi
405+
406+
if ! grep -q "No changes" '${{ steps.backend_config_file_13.outputs.text_plan_path }}'; then
407+
echo "::error:: text_plan_path not set correctly"
408+
exit 1
409+
fi
306410
307411
- name: Plan
308412
uses: ./terraform-plan
@@ -329,6 +433,16 @@ jobs:
329433
echo "::error:: Output from backend_config not set correctly"
330434
exit 1
331435
fi
436+
437+
if [[ $(jq -r .output_changes.test.actions[0] "${{ steps.backend_config_13.outputs.json_plan_path }}") != "create" ]]; then
438+
echo "::error:: json_plan_path not set correctly"
439+
exit 1
440+
fi
441+
442+
if ! grep -q "No changes" '${{ steps.backend_config_13.outputs.text_plan_path }}'; then
443+
echo "::error:: text_plan_path not set correctly"
444+
exit 1
445+
fi
332446
333447
apply_label:
334448
runs-on: ubuntu-latest
@@ -362,31 +476,17 @@ jobs:
362476
echo "::error:: output s not set correctly"
363477
exit 1
364478
fi
365-
366-
apply_changes_already_applied:
367-
runs-on: ubuntu-latest
368-
name: Apply when changes are already applied
369-
env:
370-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
371-
needs:
372-
- apply
373-
steps:
374-
- name: Checkout
375-
uses: actions/checkout@v2
376-
377-
- name: Apply
378-
uses: ./terraform-apply
379-
id: output
380-
with:
381-
path: tests/apply/changes
382-
383-
- name: Verify outputs
384-
run: |
385-
if [[ "${{ steps.output.outputs.output_string }}" != "the_string" ]]; then
386-
echo "::error:: output s not set correctly"
479+
480+
if [[ $(jq -r .output_changes.output_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
481+
echo "::error:: json_plan_path not set correctly"
387482
exit 1
388483
fi
389484
485+
if ! grep -q "Terraform will perform the following actions" '${{ steps.output.outputs.text_plan_path }}'; then
486+
echo "::error:: text_plan_path not set correctly"
487+
exit 1
488+
fi
489+
390490
apply_no_changes:
391491
runs-on: ubuntu-latest
392492
name: Apply when there are no planned changes
@@ -410,6 +510,16 @@ jobs:
410510
echo "::error:: output my_string not set correctly"
411511
exit 1
412512
fi
513+
514+
if [[ $(jq -r .format_version "${{ steps.output.outputs.json_plan_path }}") != "0.2" ]]; then
515+
echo "::error:: json_plan_path not set correctly"
516+
exit 1
517+
fi
518+
519+
if ! grep -q "No changes" '${{ steps.output.outputs.text_plan_path }}'; then
520+
echo "::error:: text_plan_path not set correctly"
521+
exit 1
522+
fi
413523
414524
apply_no_plan:
415525
runs-on: ubuntu-latest
@@ -433,6 +543,16 @@ jobs:
433543
echo "Apply did not fail correctly"
434544
exit 1
435545
fi
546+
547+
if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "0.2" ]]; then
548+
echo "::error:: json_plan_path not set correctly"
549+
exit 1
550+
fi
551+
552+
if ! grep -q "Terraform will perform the following actions" '${{ steps.apply.outputs.text_plan_path }}'; then
553+
echo "::error:: text_plan_path not set correctly"
554+
exit 1
555+
fi
436556
437557
apply_user_token:
438558
runs-on: ubuntu-latest
@@ -463,6 +583,16 @@ jobs:
463583
exit 1
464584
fi
465585
586+
if [[ $(jq -r .output_changes.output_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
587+
echo "::error:: json_plan_path not set correctly"
588+
exit 1
589+
fi
590+
591+
if ! grep -q "Terraform will perform the following actions" '${{ steps.output.outputs.text_plan_path }}'; then
592+
echo "::error:: text_plan_path not set correctly"
593+
exit 1
594+
fi
595+
466596
apply_vars:
467597
runs-on: ubuntu-latest
468598
name: Apply approved changes with deprecated vars

.github/workflows/test-plan.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ jobs:
127127
128128
cat '${{ steps.plan.outputs.json_plan_path }}'
129129
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
130-
echo "::error:: json not set correctly"
130+
echo "::error:: json_plan_path not set correctly"
131131
exit 1
132132
fi
133133
@@ -162,7 +162,7 @@ jobs:
162162
163163
cat '${{ steps.plan.outputs.json_plan_path }}'
164164
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
165-
echo "::error:: json not set correctly"
165+
echo "::error:: json_plan_path not set correctly"
166166
exit 1
167167
fi
168168
@@ -198,7 +198,7 @@ jobs:
198198
199199
cat '${{ steps.plan.outputs.json_plan_path }}'
200200
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
201-
echo "::error:: json not set correctly"
201+
echo "::error:: json_plan_path not set correctly"
202202
exit 1
203203
fi
204204
@@ -234,7 +234,7 @@ jobs:
234234
235235
cat '${{ steps.plan.outputs.json_plan_path }}'
236236
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
237-
echo "::error:: json not set correctly"
237+
echo "::error:: json_plan_path not set correctly"
238238
exit 1
239239
fi
240240
@@ -269,7 +269,7 @@ jobs:
269269
270270
cat '${{ steps.plan.outputs.json_plan_path }}'
271271
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
272-
echo "::error:: json not set correctly"
272+
echo "::error:: json_plan_path not set correctly"
273273
exit 1
274274
fi
275275
@@ -304,7 +304,7 @@ jobs:
304304
305305
cat '${{ steps.plan.outputs.json_plan_path }}'
306306
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
307-
echo "::error:: json not set correctly"
307+
echo "::error:: json_plan_path not set correctly"
308308
exit 1
309309
fi
310310
@@ -333,7 +333,7 @@ jobs:
333333
run: |
334334
cat '${{ steps.plan.outputs.json_plan_path }}'
335335
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
336-
echo "::error:: json not set correctly"
336+
echo "::error:: json_plan_path not set correctly"
337337
exit 1
338338
fi
339339

.github/workflows/test-remote.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ jobs:
6060
echo "::error:: Variables not set correctly"
6161
exit 1
6262
fi
63+
64+
if [[ -n "${{ steps.auto_apply.outputs.text_plan_path }}" ]]; then
65+
echo "::error:: text_plan_path should not be set"
66+
exit 1
67+
fi
68+
69+
if [[ -n "${{ steps.auto_apply.outputs.json_plan_path }}" ]]; then
70+
echo "::error:: json_plan_path should not be set"
71+
exit 1
72+
fi
6373
6474
- name: Check no changes
6575
uses: ./terraform-check
@@ -160,6 +170,16 @@ jobs:
160170
echo "::error:: Variables not set correctly"
161171
exit 1
162172
fi
173+
174+
if ! grep -q "Terraform will perform the following actions" '${{ steps.apply.outputs.text_plan_path }}'; then
175+
echo "::error:: text_plan_path not set correctly"
176+
exit 1
177+
fi
178+
179+
if [[ -n "${{ steps.apply.outputs.json_plan_path }}" ]]; then
180+
echo "::error:: json_plan_path should not be set"
181+
exit 1
182+
fi
163183
164184
- name: Destroy the last workspace
165185
uses: ./terraform-destroy-workspace

image/entrypoints/apply.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ if [[ $PLAN_EXIT -eq 1 ]]; then
7373
exit 1
7474
fi
7575

76+
if [[ -z "$PLAN_OUT" && "$INPUT_AUTO_APPROVE" == "true" ]]; then
77+
# Since we are doing an auto approved remote apply there is no point in planning beforehand
78+
# No text_plan_path output for this run
79+
:
80+
else
81+
mkdir -p "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR"
82+
cp "$STEP_TMP_DIR/plan.txt" "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/plan.txt"
83+
set_output text_plan_path "$WORKSPACE_TMP_DIR/plan.txt"
84+
fi
85+
86+
if [[ -n "$PLAN_OUT" ]]; then
87+
if (cd "$INPUT_PATH" && terraform show -json "$PLAN_OUT") >"$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/plan.json" 2>"$STEP_TMP_DIR/terraform_show.stderr"; then
88+
set_output json_plan_path "$WORKSPACE_TMP_DIR/plan.json"
89+
else
90+
debug_file "$STEP_TMP_DIR/terraform_show.stderr"
91+
fi
92+
fi
93+
7694
### Apply the plan
7795

7896
if [[ "$INPUT_AUTO_APPROVE" == "true" || $PLAN_EXIT -eq 0 ]]; then

0 commit comments

Comments
 (0)