From 9b1d7827c4e0fce052948903e7db6a54c5861c4d Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Fri, 16 Jun 2023 19:45:38 +0100 Subject: [PATCH 01/13] Add a test for partial cloud config block --- .github/workflows/test-version.yaml | 45 +++++++++++++++++++ .../test-version/partial-cloud/main.tf | 4 ++ 2 files changed, 49 insertions(+) create mode 100644 tests/workflows/test-version/partial-cloud/main.tf diff --git a/.github/workflows/test-version.yaml b/.github/workflows/test-version.yaml index 128180a7..9982d3ea 100644 --- a/.github/workflows/test-version.yaml +++ b/.github/workflows/test-version.yaml @@ -226,6 +226,51 @@ jobs: exit 1 fi + tfc_partial_cloud_workspace: + runs-on: ubuntu-latest + name: Partial TFC Cloud Configuration + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Create workspace + uses: ./terraform-new-workspace + env: + TERRAFORM_VERSION: 1.1.2 + TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }} + TF_CLOUD_ORGANIZATION: flooktech + with: + path: tests/workflows/test-version/partial-cloud + workspace: tfc_cloud_workspace-1 + + - name: Test terraform-version + uses: ./terraform-version + id: terraform-version + env: + TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }} + TF_CLOUD_ORGANIZATION: flooktech + with: + path: tests/workflows/test-version/partial-cloud + workspace: tfc_cloud_workspace-1 + + - name: Destroy workspace + uses: ./terraform-destroy-workspace + env: + TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }} + TF_CLOUD_ORGANIZATION: flooktech + with: + path: tests/workflows/test-version/partial-cloud + workspace: tfc_cloud_workspace-1 + + - name: Print the version + run: | + echo "The terraform version was ${{ steps.terraform-version.outputs.terraform }}" + + if [[ "${{ steps.terraform-version.outputs.terraform }}" != "1.1.2" ]]; then + echo "::error:: Terraform version not set from remote workspace" + exit 1 + fi + local_state: runs-on: ubuntu-latest name: Local State file diff --git a/tests/workflows/test-version/partial-cloud/main.tf b/tests/workflows/test-version/partial-cloud/main.tf new file mode 100644 index 00000000..f42678f8 --- /dev/null +++ b/tests/workflows/test-version/partial-cloud/main.tf @@ -0,0 +1,4 @@ +terraform { + cloud { + } +} From 84576059c59c90ff7e35630e683a92c3d20be4a3 Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Fri, 16 Jun 2023 19:57:35 +0100 Subject: [PATCH 02/13] Update version tests for 1.5 --- .github/workflows/test-apply.yaml | 6 +++--- .github/workflows/test-plan.yaml | 4 ++-- .github/workflows/test-version.yaml | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test-apply.yaml b/.github/workflows/test-apply.yaml index 1c1d0c22..af5706b0 100644 --- a/.github/workflows/test-apply.yaml +++ b/.github/workflows/test-apply.yaml @@ -123,7 +123,7 @@ jobs: fi cat "${{ steps.apply.outputs.json_plan_path }}" - if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "1.1" ]]; then + if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "1.2" ]]; then echo "::error:: json_plan_path not set correctly" exit 1 fi @@ -227,7 +227,7 @@ jobs: exit 1 fi - if [[ $(jq -r .format_version "${{ steps.second-apply.outputs.json_plan_path }}") != "1.1" ]]; then + if [[ $(jq -r .format_version "${{ steps.second-apply.outputs.json_plan_path }}") != "1.2" ]]; then echo "::error:: json_plan_path not set correctly" exit 1 fi @@ -625,7 +625,7 @@ jobs: fi cat "${{ steps.apply.outputs.json_plan_path }}" - if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "1.1" ]]; then + if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "1.2" ]]; then echo "::error:: json_plan_path not set correctly" exit 1 fi diff --git a/.github/workflows/test-plan.yaml b/.github/workflows/test-plan.yaml index 82a967de..56777883 100644 --- a/.github/workflows/test-plan.yaml +++ b/.github/workflows/test-plan.yaml @@ -29,7 +29,7 @@ jobs: fi cat '${{ steps.plan.outputs.json_plan_path }}' - if [[ $(jq -r .format_version "${{ steps.plan.outputs.json_plan_path }}") != "1.1" ]]; then + if [[ $(jq -r .format_version "${{ steps.plan.outputs.json_plan_path }}") != "1.2" ]]; then echo "::error:: json_plan_path not set correctly" exit 1 fi @@ -69,7 +69,7 @@ jobs: - name: Verify outputs run: | cat '${{ steps.plan.outputs.json_plan_path }}' - if [[ $(jq -r .format_version "${{ steps.plan.outputs.json_plan_path }}") != "1.1" ]]; then + if [[ $(jq -r .format_version "${{ steps.plan.outputs.json_plan_path }}") != "1.2" ]]; then echo "::error:: json_plan_path not set correctly" exit 1 fi diff --git a/.github/workflows/test-version.yaml b/.github/workflows/test-version.yaml index 9982d3ea..b47a3668 100644 --- a/.github/workflows/test-version.yaml +++ b/.github/workflows/test-version.yaml @@ -443,7 +443,7 @@ jobs: - name: Check the version run: | - if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.4"* ]]; then + if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.5"* ]]; then echo "::error:: Latest version was not used" exit 1 fi From 536530079db7063edd3e2490135b0b519a692051 Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Fri, 16 Jun 2023 20:01:47 +0100 Subject: [PATCH 03/13] Support TF_CLOUD_HOSTNAME and TF_ORGANIZATION --- image/src/terraform/module.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/image/src/terraform/module.py b/image/src/terraform/module.py index 05c82d84..8143fcc4 100644 --- a/image/src/terraform/module.py +++ b/image/src/terraform/module.py @@ -216,8 +216,10 @@ def get_cloud_config(module: TerraformModule, cli_config_path: Path) -> Optional if 'hostname' in cloud: backend_config['hostname'] = cloud['hostname'] + elif 'TF_CLOUD_HOSTNAME' in os.environ: + backend_config['hostname'] = os.environ['TF_CLOUD_HOSTNAME'] - backend_config['organization'] = cloud.get('organization') + backend_config['organization'] = cloud.get('organization', os.environ.get('TF_ORGANIZATION')) backend_config['token'] = cloud.get('token') if cloud.get('workspaces', []): From 473491d1c895e82911adff7820e2cdde9ca2dd9c Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Fri, 16 Jun 2023 20:03:21 +0100 Subject: [PATCH 04/13] Support TF_CLOUD_HOSTNAME and TF_CLOUD_ORGANIZATION --- image/src/terraform/module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image/src/terraform/module.py b/image/src/terraform/module.py index 8143fcc4..ecdc30d3 100644 --- a/image/src/terraform/module.py +++ b/image/src/terraform/module.py @@ -219,7 +219,7 @@ def get_cloud_config(module: TerraformModule, cli_config_path: Path) -> Optional elif 'TF_CLOUD_HOSTNAME' in os.environ: backend_config['hostname'] = os.environ['TF_CLOUD_HOSTNAME'] - backend_config['organization'] = cloud.get('organization', os.environ.get('TF_ORGANIZATION')) + backend_config['organization'] = cloud.get('organization', os.environ.get('TF_CLOUD_ORGANIZATION')) backend_config['token'] = cloud.get('token') if cloud.get('workspaces', []): From 86e3b0d44ca030b3d654fb9e27aa8f82d8eb6ba1 Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Fri, 16 Jun 2023 21:34:11 +0100 Subject: [PATCH 05/13] Support TF_WORKSPACE when not specified in cloud config block --- image/src/terraform/module.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/image/src/terraform/module.py b/image/src/terraform/module.py index ecdc30d3..af6503d8 100644 --- a/image/src/terraform/module.py +++ b/image/src/terraform/module.py @@ -222,8 +222,10 @@ def get_cloud_config(module: TerraformModule, cli_config_path: Path) -> Optional backend_config['organization'] = cloud.get('organization', os.environ.get('TF_CLOUD_ORGANIZATION')) backend_config['token'] = cloud.get('token') - if cloud.get('workspaces', []): + if 'workspaces' in cloud: backend_config['workspaces'] = cloud['workspaces'][0] + elif 'INPUT_WORKSPACE' in os.environ: + backend_config['workspaces'] = BackendConfigWorkspaces(name=os.environ['INPUT_WORKSPACE']) if not found: return None From 4e5682a495767ce8f17118df41f1c90ddd3064fd Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Fri, 16 Jun 2023 21:43:59 +0100 Subject: [PATCH 06/13] Bump tf to a version that supports cloud config from env vars --- .github/workflows/test-version.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-version.yaml b/.github/workflows/test-version.yaml index b47a3668..9fe3ff40 100644 --- a/.github/workflows/test-version.yaml +++ b/.github/workflows/test-version.yaml @@ -236,7 +236,7 @@ jobs: - name: Create workspace uses: ./terraform-new-workspace env: - TERRAFORM_VERSION: 1.1.2 + TERRAFORM_VERSION: 1.2.1 TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }} TF_CLOUD_ORGANIZATION: flooktech with: @@ -266,7 +266,7 @@ jobs: run: | echo "The terraform version was ${{ steps.terraform-version.outputs.terraform }}" - if [[ "${{ steps.terraform-version.outputs.terraform }}" != "1.1.2" ]]; then + if [[ "${{ steps.terraform-version.outputs.terraform }}" != "1.2.1" ]]; then echo "::error:: Terraform version not set from remote workspace" exit 1 fi From 5989db66d1860e900993ce5a90038ae23a1d69d6 Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Fri, 16 Jun 2023 22:02:08 +0100 Subject: [PATCH 07/13] Use separate workspace --- .github/workflows/test-version.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test-version.yaml b/.github/workflows/test-version.yaml index 9fe3ff40..4fa4eb9a 100644 --- a/.github/workflows/test-version.yaml +++ b/.github/workflows/test-version.yaml @@ -241,7 +241,7 @@ jobs: TF_CLOUD_ORGANIZATION: flooktech with: path: tests/workflows/test-version/partial-cloud - workspace: tfc_cloud_workspace-1 + workspace: tfc_partial_cloud_workspace-1 - name: Test terraform-version uses: ./terraform-version @@ -251,7 +251,7 @@ jobs: TF_CLOUD_ORGANIZATION: flooktech with: path: tests/workflows/test-version/partial-cloud - workspace: tfc_cloud_workspace-1 + workspace: tfc_partial_cloud_workspace-1 - name: Destroy workspace uses: ./terraform-destroy-workspace @@ -260,7 +260,7 @@ jobs: TF_CLOUD_ORGANIZATION: flooktech with: path: tests/workflows/test-version/partial-cloud - workspace: tfc_cloud_workspace-1 + workspace: tfc_partial_cloud_workspace-1 - name: Print the version run: | From 8a511434888513155359bae4a78f75afd3558467 Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Sun, 18 Jun 2023 11:09:42 +0100 Subject: [PATCH 08/13] Recognise cloud backend as distinct from remote backend --- image/entrypoints/destroy-workspace.sh | 2 +- image/entrypoints/new-workspace.sh | 2 +- image/entrypoints/validate.sh | 2 +- image/src/github_pr_comment/__main__.py | 2 ++ image/src/terraform/module.py | 2 +- image/src/terraform_version/__main__.py | 2 +- 6 files changed, 7 insertions(+), 5 deletions(-) diff --git a/image/entrypoints/destroy-workspace.sh b/image/entrypoints/destroy-workspace.sh index b46e9522..59d76bc7 100755 --- a/image/entrypoints/destroy-workspace.sh +++ b/image/entrypoints/destroy-workspace.sh @@ -29,7 +29,7 @@ if [[ $DESTROY_EXIT -eq 1 ]]; then exit 1 fi -if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" ]]; then +if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" || "$TERRAFORM_BACKEND_TYPE" == "cloud" ]]; then terraform-cloud-workspace delete "$INPUT_WORKSPACE" else # We can't delete an active workspace, so re-initialize with a 'default' workspace (which may not exist) diff --git a/image/entrypoints/new-workspace.sh b/image/entrypoints/new-workspace.sh index 8ab4982e..b4cbc6b5 100755 --- a/image/entrypoints/new-workspace.sh +++ b/image/entrypoints/new-workspace.sh @@ -6,7 +6,7 @@ source /usr/local/actions.sh debug setup -if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" ]]; then +if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" || "$TERRAFORM_BACKEND_TYPE" == "cloud" ]]; then TERRAFORM_VERSION="$TERRAFORM_VER_MAJOR.$TERRAFORM_VER_MINOR.$TERRAFORM_VER_PATCH" terraform-cloud-workspace new "$INPUT_WORKSPACE" exit 0 fi diff --git a/image/entrypoints/validate.sh b/image/entrypoints/validate.sh index 96c54360..8a81c3fc 100755 --- a/image/entrypoints/validate.sh +++ b/image/entrypoints/validate.sh @@ -13,7 +13,7 @@ setup # terraform.workspace will be evaluated during a validate, but it is not initialized properly. # Pass through the workspace input, except for remote backend where it should be 'default' -if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" ]]; then +if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" || "$TERRAFORM_BACKEND_TYPE" == "cloud" ]]; then TF_WORKSPACE="default" else TF_WORKSPACE="$INPUT_WORKSPACE" diff --git a/image/src/github_pr_comment/__main__.py b/image/src/github_pr_comment/__main__.py index 3befb2b7..6ecff5c2 100644 --- a/image/src/github_pr_comment/__main__.py +++ b/image/src/github_pr_comment/__main__.py @@ -297,6 +297,8 @@ def get_comment(action_inputs: PlanPrInputs, backend_fingerprint: bytes, backup_ } if backend_type := os.environ.get('TERRAFORM_BACKEND_TYPE'): + if backend_type == 'cloud': + backend_type = 'remote' headers['backend_type'] = backend_type headers['label'] = os.environ.get('INPUT_LABEL') or None diff --git a/image/src/terraform/module.py b/image/src/terraform/module.py index af6503d8..1a51756c 100644 --- a/image/src/terraform/module.py +++ b/image/src/terraform/module.py @@ -252,7 +252,7 @@ def get_backend_type(module: TerraformModule) -> Optional[str]: for terraform in module.get('terraform', []): if 'cloud' in terraform: - return 'remote' + return 'cloud' return 'local' diff --git a/image/src/terraform_version/__main__.py b/image/src/terraform_version/__main__.py index 1630e564..6fa185eb 100644 --- a/image/src/terraform_version/__main__.py +++ b/image/src/terraform_version/__main__.py @@ -79,7 +79,7 @@ def determine_version(inputs: InitInputs, cli_config_path: Path, actions_env: Ac # arm64 support was introduced in 0.13.5 versions = list(apply_constraints(versions, [Constraint('>=0.13.5')])) - if backend_type not in ['remote', 'local']: + if backend_type not in ['remote', 'cloud', 'local']: if version := try_guess_state_version(inputs, module, versions): sys.stdout.write('Using the same terraform version that wrote the existing remote state file\n') return version From b4337157bde509387a4d1a5d8b405a24d0ed5d24 Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Sun, 18 Jun 2023 12:28:50 +0100 Subject: [PATCH 09/13] Ingore failure to select workspace with cloud backend --- image/actions.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/image/actions.sh b/image/actions.sh index 6ad4b54e..63965869 100644 --- a/image/actions.sh +++ b/image/actions.sh @@ -249,6 +249,10 @@ function select-workspace() { if [[ $WORKSPACE_EXIT -ne 0 ]] && grep -q "workspaces not supported" "$STEP_TMP_DIR/workspace_select" && [[ $INPUT_WORKSPACE == "default" ]]; then echo "The full name of a remote workspace is set by the terraform configuration, selecting a different one is not supported" WORKSPACE_EXIT=0 + elif [[ $WORKSPACE_EXIT -ne 0 && "$TERRAFORM_BACKEND_TYPE" == "cloud" ]]; then + # workspace select doesn't work with partial cloud config, we'll just have to try it and see + export TF_WORKSPACE="$INPUT_WORKSPACE" + WORKSPACE_EXIT=0 else cat "$STEP_TMP_DIR/workspace_select" fi From db25df973a9bc87f68973cb68aaea51edcfde8ac Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Sun, 18 Jun 2023 13:42:01 +0100 Subject: [PATCH 10/13] Add test for version detection of partial config with tags --- .github/workflows/test-version.yaml | 45 +++++++++++++++++++ .../test-version/partial-cloud-tags/main.tf | 7 +++ 2 files changed, 52 insertions(+) create mode 100644 tests/workflows/test-version/partial-cloud-tags/main.tf diff --git a/.github/workflows/test-version.yaml b/.github/workflows/test-version.yaml index 4fa4eb9a..24ad21a2 100644 --- a/.github/workflows/test-version.yaml +++ b/.github/workflows/test-version.yaml @@ -271,6 +271,51 @@ jobs: exit 1 fi + tfc_partial_cloud_tags_workspace: + runs-on: ubuntu-latest + name: Partial TFC Cloud Configuration with tags + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Create workspace + uses: ./terraform-new-workspace + env: + TERRAFORM_VERSION: 1.2.1 + TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }} + TF_CLOUD_ORGANIZATION: flooktech + with: + path: tests/workflows/test-version/partial-cloud-tags + workspace: tfc_partial_cloud_tags_workspace-1 + + - name: Test terraform-version + uses: ./terraform-version + id: terraform-version + env: + TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }} + TF_CLOUD_ORGANIZATION: flooktech + with: + path: tests/workflows/test-version/partial-cloud-tags + workspace: tfc_partial_cloud_tags_workspace-1 + + - name: Destroy workspace + uses: ./terraform-destroy-workspace + env: + TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }} + TF_CLOUD_ORGANIZATION: flooktech + with: + path: tests/workflows/test-version/partial-cloud-tags + workspace: tfc_partial_cloud_tags_workspace-1 + + - name: Print the version + run: | + echo "The terraform version was ${{ steps.terraform-version.outputs.terraform }}" + + if [[ "${{ steps.terraform-version.outputs.terraform }}" != "1.2.1" ]]; then + echo "::error:: Terraform version not set from remote workspace" + exit 1 + fi + local_state: runs-on: ubuntu-latest name: Local State file diff --git a/tests/workflows/test-version/partial-cloud-tags/main.tf b/tests/workflows/test-version/partial-cloud-tags/main.tf new file mode 100644 index 00000000..12f2ec86 --- /dev/null +++ b/tests/workflows/test-version/partial-cloud-tags/main.tf @@ -0,0 +1,7 @@ +terraform { + cloud { + workspaces { + tags = ["animal", "mineral"] + } + } +} From 88c3bd6c3f5da0f4d7b841917f3b9f49a8b32bee Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Sun, 18 Jun 2023 13:42:45 +0100 Subject: [PATCH 11/13] Add workspace lifecycle test for partial cloud config --- .github/workflows/test-cloud.yaml | 187 +++++++++++++++++++++ tests/workflows/test-cloud/partial/main.tf | 21 +++ 2 files changed, 208 insertions(+) create mode 100644 tests/workflows/test-cloud/partial/main.tf diff --git a/.github/workflows/test-cloud.yaml b/.github/workflows/test-cloud.yaml index 69ae67f2..bfbb3eeb 100644 --- a/.github/workflows/test-cloud.yaml +++ b/.github/workflows/test-cloud.yaml @@ -270,3 +270,190 @@ jobs: echo "Destroy non-existant workspace" exit 1 fi + + cloud: + runs-on: ubuntu-latest + name: Partial cloud config + env: + TF_CLOUD_ORGANIZATION: flooktech + TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Create a new workspace with no existing workspaces + uses: ./terraform-new-workspace + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-1 + + - name: Create a new workspace when it doesn't exist + uses: ./terraform-new-workspace + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-2 + + - name: Create a new workspace when it already exists + uses: ./terraform-new-workspace + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-2 + + - name: Auto apply workspace + uses: ./terraform-apply + id: auto_apply + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-1 + auto_approve: true + + - name: Verify auto_apply terraform outputs + run: | + if [[ "${{ steps.apply.outputs.len }}" != "5" ]]; then + echo "::error:: output 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 + + echo '${{ steps.auto_apply.outputs.run_id }}' + if [[ "${{ steps.auto_apply.outputs.run_id }}" != "run-"* ]]; then + echo "::error:: output run_id not set correctly" + exit 1 + fi + + - name: Get outputs + uses: ./terraform-output + id: output + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-1 + + - name: Verify auto_apply terraform outputs with workspace prefix + run: | + if [[ "${{ steps.output.outputs.len }}" != "5" ]]; then + echo "::error:: output not set correctly" + exit 1 + fi + + - name: Check no changes + uses: ./terraform-check + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-1 + + - name: Check changes + uses: ./terraform-check + id: check + continue-on-error: true + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-1 + variables: | + len=6 + + - name: Verify changes detected + run: | + if [[ "${{ steps.check.outcome }}" != "failure" ]]; then + echo "Check didn't fail correctly" + exit 1 + fi + + if [[ "${{ steps.check.outputs.failure-reason }}" != "changes-to-apply" ]]; then + echo "failure-reason not set correctly" + exit 1 + fi + + - name: Destroy workspace + uses: ./terraform-destroy-workspace + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-1 + + - name: Plan workspace + uses: ./terraform-plan + id: plan + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-2 + + - name: Verify plan outputs + run: | + if [[ "${{ steps.plan.outputs.changes }}" != "true" ]]; then + echo "::error:: output changes not set correctly" + exit 1 + fi + + if ! grep -q "Terraform will perform the following actions" '${{ steps.plan.outputs.text_plan_path }}'; then + echo "::error:: text_plan_path not set correctly" + exit 1 + fi + + echo '${{ steps.plan.outputs.run_id }}' + if [[ "${{ steps.plan.outputs.run_id }}" != "run-"* ]]; then + echo "::error:: output run_id not set correctly" + exit 1 + fi + + - name: Apply workspace + uses: ./terraform-apply + id: apply + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-2 + + - name: Verify apply terraform outputs + run: | + if [[ "${{ steps.apply.outputs.len }}" != "5" ]]; then + 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 + + echo '${{ steps.apply.outputs.run_id }}' + if [[ "${{ steps.apply.outputs.run_id }}" != "run-"* ]]; then + echo "::error:: output run_id not set correctly" + exit 1 + fi + + - name: Destroy the last workspace + uses: ./terraform-destroy-workspace + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-2 + + - name: Destroy non-existent workspace + uses: ./terraform-destroy-workspace + continue-on-error: true + id: destroy-non-existant-workspace + with: + path: tests/workflows/test-cloud/partial + workspace: ${{ github.head_ref }}-cloud-1 + + - name: Check failed to destroy + run: | + if [[ "${{ steps.destroy-non-existant-workspace.outcome }}" != "failure" ]]; then + echo "Destroy non-existant workspace" + exit 1 + fi diff --git a/tests/workflows/test-cloud/partial/main.tf b/tests/workflows/test-cloud/partial/main.tf new file mode 100644 index 00000000..80582a6e --- /dev/null +++ b/tests/workflows/test-cloud/partial/main.tf @@ -0,0 +1,21 @@ +terraform { + cloud { + } + required_version = "~> 1.5.0" +} + +resource "random_id" "the_id" { + byte_length = var.length +} + +variable "length" { + default = 5 +} + +output "id" { + value = random_id.the_id.hex +} + +output "len" { + value = var.length +} From aa601d9069699c20994a648064d8d8ea4ec91f0f Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Sun, 18 Jun 2023 13:59:21 +0100 Subject: [PATCH 12/13] Fix step name --- .github/workflows/test-cloud.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-cloud.yaml b/.github/workflows/test-cloud.yaml index bfbb3eeb..3933bb77 100644 --- a/.github/workflows/test-cloud.yaml +++ b/.github/workflows/test-cloud.yaml @@ -309,7 +309,7 @@ jobs: - name: Verify auto_apply terraform outputs run: | - if [[ "${{ steps.apply.outputs.len }}" != "5" ]]; then + if [[ "${{ steps.auto_apply.outputs.len }}" != "5" ]]; then echo "::error:: output not set correctly" exit 1 fi @@ -337,7 +337,7 @@ jobs: path: tests/workflows/test-cloud/partial workspace: ${{ github.head_ref }}-cloud-1 - - name: Verify auto_apply terraform outputs with workspace prefix + - name: Verify terraform-output outputs run: | if [[ "${{ steps.output.outputs.len }}" != "5" ]]; then echo "::error:: output not set correctly" @@ -446,14 +446,14 @@ jobs: - name: Destroy non-existent workspace uses: ./terraform-destroy-workspace continue-on-error: true - id: destroy-non-existant-workspace + id: destroy-non-existent-workspace with: path: tests/workflows/test-cloud/partial workspace: ${{ github.head_ref }}-cloud-1 - name: Check failed to destroy run: | - if [[ "${{ steps.destroy-non-existant-workspace.outcome }}" != "failure" ]]; then + if [[ "${{ steps.destroy-non-existent-workspace.outcome }}" != "failure" ]]; then echo "Destroy non-existant workspace" exit 1 fi From 73e42ff33fedcf1d584bdf35e386861b9526c402 Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Sun, 18 Jun 2023 14:35:43 +0100 Subject: [PATCH 13/13] Fix variable name --- .github/workflows/test-cloud.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-cloud.yaml b/.github/workflows/test-cloud.yaml index 3933bb77..c375062c 100644 --- a/.github/workflows/test-cloud.yaml +++ b/.github/workflows/test-cloud.yaml @@ -358,7 +358,7 @@ jobs: path: tests/workflows/test-cloud/partial workspace: ${{ github.head_ref }}-cloud-1 variables: | - len=6 + length=6 - name: Verify changes detected run: |