Skip to content

Commit

Permalink
Merge pull request #138 from dflook/apply-plan-outputs
Browse files Browse the repository at this point in the history
Add `json_plan_path` and `text_plan_path` for dflook/terraform-apply
  • Loading branch information
dflook authored Dec 3, 2021
2 parents 3404f39 + 066c1c3 commit 9d41ad3
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 40 deletions.
174 changes: 152 additions & 22 deletions .github/workflows/test-apply.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ jobs:
echo "::error:: output my_string not set correctly"
exit 1
fi
if [[ $(jq -r .output_changes.my_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "No changes" '${{ steps.output.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
apply_error:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -53,6 +63,17 @@ jobs:
echo "::error:: failure-reason not set correctly"
exit 1
fi
if [[ -n "${{ steps.apply.outputs.json_plan_path }}" ]]; then
echo "::error:: json_plan_path should not be set"
exit 1
fi
if [[ -n "${{ steps.apply.outputs.text_plan_path }}" ]]; then
echo "::error:: text_plan_path should not be set"
exit 1
fi
apply_apply_error:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -90,6 +111,16 @@ jobs:
echo "::error:: failure-reason not set correctly"
exit 1
fi
if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "0.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" '${{ steps.apply.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
apply_no_token:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -143,6 +174,39 @@ jobs:
echo "::error:: output s not set correctly"
exit 1
fi
if [[ $(jq -r .output_changes.output_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" '${{ steps.output.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
- name: Apply
uses: ./terraform-apply
id: output
with:
path: tests/apply/changes

- name: Verify outputs
run: |
if [[ "${{ steps.output.outputs.output_string }}" != "the_string" ]]; then
echo "::error:: output s not set correctly"
exit 1
fi
if [[ $(jq -r .format_version "${{ steps.output.outputs.json_plan_path }}") != "0.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "No changes" '${{ steps.output.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
apply_variables:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -215,6 +279,16 @@ jobs:
echo "::error:: output complex_output not set correctly"
exit 1
fi
if [[ $(jq -r .output_changes.output_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" '${{ steps.output.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
backend_config_12:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -246,6 +320,16 @@ jobs:
echo "::error:: output from backend_config file not set correctly"
exit 1
fi
if [[ $(jq -r .output_changes.test.actions[0] "${{ steps.backend_config_file_12.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "No changes" '${{ steps.backend_config_file_12.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
- name: Plan
uses: ./terraform-plan
Expand All @@ -272,6 +356,16 @@ jobs:
echo "::error:: Output from backend_config not set correctly"
exit 1
fi
if [[ $(jq -r .output_changes.test.actions[0] "${{ steps.backend_config_12.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "No changes" '${{ steps.backend_config_file_12.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
backend_config_13:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -303,6 +397,16 @@ jobs:
echo "::error:: output from backend_config file not set correctly"
exit 1
fi
if [[ $(jq -r .output_changes.test.actions[0] "${{ steps.backend_config_file_13.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "No changes" '${{ steps.backend_config_file_13.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
- name: Plan
uses: ./terraform-plan
Expand All @@ -329,6 +433,16 @@ jobs:
echo "::error:: Output from backend_config not set correctly"
exit 1
fi
if [[ $(jq -r .output_changes.test.actions[0] "${{ steps.backend_config_13.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "No changes" '${{ steps.backend_config_13.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
apply_label:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -362,31 +476,17 @@ jobs:
echo "::error:: output s not set correctly"
exit 1
fi
apply_changes_already_applied:
runs-on: ubuntu-latest
name: Apply when changes are already applied
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
needs:
- apply
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Apply
uses: ./terraform-apply
id: output
with:
path: tests/apply/changes

- name: Verify outputs
run: |
if [[ "${{ steps.output.outputs.output_string }}" != "the_string" ]]; then
echo "::error:: output s not set correctly"
if [[ $(jq -r .output_changes.output_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" '${{ steps.output.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
apply_no_changes:
runs-on: ubuntu-latest
name: Apply when there are no planned changes
Expand All @@ -410,6 +510,16 @@ jobs:
echo "::error:: output my_string not set correctly"
exit 1
fi
if [[ $(jq -r .format_version "${{ steps.output.outputs.json_plan_path }}") != "0.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "No changes" '${{ steps.output.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
apply_no_plan:
runs-on: ubuntu-latest
Expand All @@ -433,6 +543,16 @@ jobs:
echo "Apply did not fail correctly"
exit 1
fi
if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "0.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" '${{ steps.apply.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
apply_user_token:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -463,6 +583,16 @@ jobs:
exit 1
fi
if [[ $(jq -r .output_changes.output_string.actions[0] "${{ steps.output.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" '${{ steps.output.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
apply_vars:
runs-on: ubuntu-latest
name: Apply approved changes with deprecated vars
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/test-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jobs:
cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json not set correctly"
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -162,7 +162,7 @@ jobs:
cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json not set correctly"
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -198,7 +198,7 @@ jobs:
cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json not set correctly"
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -234,7 +234,7 @@ jobs:
cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json not set correctly"
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -269,7 +269,7 @@ jobs:
cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json not set correctly"
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -304,7 +304,7 @@ jobs:
cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json not set correctly"
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -333,7 +333,7 @@ jobs:
run: |
cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .output_changes.s.actions[0] "${{ steps.plan.outputs.json_plan_path }}") != "create" ]]; then
echo "::error:: json not set correctly"
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/test-remote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ jobs:
echo "::error:: Variables not set correctly"
exit 1
fi
if [[ -n "${{ steps.auto_apply.outputs.text_plan_path }}" ]]; then
echo "::error:: text_plan_path should not be set"
exit 1
fi
if [[ -n "${{ steps.auto_apply.outputs.json_plan_path }}" ]]; then
echo "::error:: json_plan_path should not be set"
exit 1
fi
- name: Check no changes
uses: ./terraform-check
Expand Down Expand Up @@ -160,6 +170,16 @@ jobs:
echo "::error:: Variables not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" '${{ steps.apply.outputs.text_plan_path }}'; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ -n "${{ steps.apply.outputs.json_plan_path }}" ]]; then
echo "::error:: json_plan_path should not be set"
exit 1
fi
- name: Destroy the last workspace
uses: ./terraform-destroy-workspace
Expand Down
18 changes: 18 additions & 0 deletions image/entrypoints/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ if [[ $PLAN_EXIT -eq 1 ]]; then
exit 1
fi

if [[ -z "$PLAN_OUT" && "$INPUT_AUTO_APPROVE" == "true" ]]; then
# Since we are doing an auto approved remote apply there is no point in planning beforehand
# No text_plan_path output for this run
:
else
mkdir -p "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR"
cp "$STEP_TMP_DIR/plan.txt" "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/plan.txt"
set_output text_plan_path "$WORKSPACE_TMP_DIR/plan.txt"
fi

if [[ -n "$PLAN_OUT" ]]; then
if (cd "$INPUT_PATH" && terraform show -json "$PLAN_OUT") >"$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/plan.json" 2>"$STEP_TMP_DIR/terraform_show.stderr"; then
set_output json_plan_path "$WORKSPACE_TMP_DIR/plan.json"
else
debug_file "$STEP_TMP_DIR/terraform_show.stderr"
fi
fi

### Apply the plan

if [[ "$INPUT_AUTO_APPROVE" == "true" || $PLAN_EXIT -eq 0 ]]; then
Expand Down
Loading

0 comments on commit 9d41ad3

Please sign in to comment.