Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for partial cloud config block #268

Merged
merged 13 commits into from
Jun 18, 2023
Merged
6 changes: 3 additions & 3 deletions .github/workflows/test-apply.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
187 changes: 187 additions & 0 deletions .github/workflows/test-cloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.auto_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 terraform-output outputs
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: |
length=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-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-existent-workspace.outcome }}" != "failure" ]]; then
echo "Destroy non-existant workspace"
exit 1
fi
4 changes: 2 additions & 2 deletions .github/workflows/test-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
92 changes: 91 additions & 1 deletion .github/workflows/test-version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,96 @@ 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.2.1
TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }}
TF_CLOUD_ORGANIZATION: flooktech
with:
path: tests/workflows/test-version/partial-cloud
workspace: tfc_partial_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_partial_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_partial_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.2.1" ]]; then
echo "::error:: Terraform version not set from remote workspace"
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
Expand Down Expand Up @@ -398,7 +488,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
Expand Down
4 changes: 4 additions & 0 deletions image/actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/destroy-workspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/new-workspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions image/src/github_pr_comment/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading