Skip to content

Improve parsing of tf files #282

Improve parsing of tf files

Improve parsing of tf files #282

Workflow file for this run

name: Test Terraform cloud
on:
- pull_request
permissions:
contents: read
jobs:
workspaces:
runs-on: ubuntu-24.04
name: Terraform Cloud
permissions:
contents: read
pull-requests: write
strategy:
fail-fast: false
matrix:
tf_version: ['0.13', '1.0']
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Create a new workspace with no existing workspaces
uses: ./terraform-new-workspace
with:
path: tests/workflows/test-cloud/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-1
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Create a new workspace when it doesn't exist
uses: ./terraform-new-workspace
with:
path: tests/workflows/test-cloud/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-2
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Create a new workspace when it already exists
uses: ./terraform-new-workspace
with:
path: tests/workflows/test-cloud/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-2
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Auto apply workspace
uses: ./terraform-apply
id: auto_apply
with:
path: tests/workflows/test-cloud/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-1
backend_config: token=${{ secrets.TF_API_TOKEN }}
auto_approve: true
var_file: |
tests/workflows/test-cloud/${{ matrix.tf_version }}/my_variable.tfvars
variables: |
from_variables="from_variables"
- name: Verify auto_apply terraform outputs
env:
OUTPUT_DEFAULT: ${{ steps.auto_apply.outputs.default }}
FROM_TFVARS: ${{ steps.auto_apply.outputs.from_tfvars }}
FROM_VARIABLES: ${{ steps.auto_apply.outputs.from_variables }}
TEXT_PLAN_PATH: ${{ steps.auto_apply.outputs.text_plan_path }}
JSON_PLAN_PATH: ${{ steps.auto_apply.outputs.json_plan_path }}
RUN_ID: ${{ steps.auto_apply.outputs.run_id }}
run: |
if [[ "$OUTPUT_DEFAULT" != "default" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if [[ "$FROM_TFVARS" != "from_tfvars" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if [[ "$FROM_VARIABLES" != "from_variables" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if [[ -n "$TEXT_PLAN_PATH" ]]; then
echo "::error:: text_plan_path should not be set"
exit 1
fi
if [[ -n "$JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should not be set"
exit 1
fi
if [[ "$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/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-1
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify auto_apply terraform outputs with workspace prefix
env:
OUTPUT_DEFAULT: ${{ steps.output.outputs.default }}
run: |
if [[ "$OUTPUT_DEFAULT" != "default" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
- name: Setup terraform with workspace name
env:
HEAD_REF: ${{ github.head_ref }}
run: |
mkdir fixed-workspace-name
if [[ "${{ matrix.tf_version }}" == "0.13" ]]; then
sed -e 's/prefix.*/name = "github-actions-0-13-'"$HEAD_REF"'-1"/' tests/workflows/test-cloud/${{ matrix.tf_version }}/main.tf > fixed-workspace-name/main.tf
else
sed -e 's/prefix.*/name = "github-actions-1-1-'"$HEAD_REF"'-1"/' tests/workflows/test-cloud/${{ matrix.tf_version }}/main.tf > fixed-workspace-name/main.tf
fi
- name: Get outputs
uses: ./terraform-output
id: name-output
with:
path: fixed-workspace-name
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify auto_apply terraform outputs with workspace name
env:
OUTPUT_DEFAULT: ${{ steps.name-output.outputs.default }}
run: |
if [[ "$OUTPUT_DEFAULT" != "default" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
- name: Check no changes
uses: ./terraform-check
with:
path: tests/workflows/test-cloud/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-1
backend_config: token=${{ secrets.TF_API_TOKEN }}
var_file: |
tests/workflows/test-cloud/${{ matrix.tf_version }}/my_variable.tfvars
variables: |
from_variables="from_variables"
- name: Check changes
uses: ./terraform-check
id: check
continue-on-error: true
with:
path: tests/workflows/test-cloud/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-1
backend_config: token=${{ secrets.TF_API_TOKEN }}
var_file: |
tests/workflows/test-cloud/${{ matrix.tf_version }}/my_variable.tfvars
variables: |
from_variables="Changed!"
- name: Verify changes detected
env:
CHECK_OUTCOME: ${{ steps.check.outcome }}
FAILURE_REASON: ${{ steps.check.outputs.failure-reason }}
run: |
if [[ "$CHECK_OUTCOME" != "failure" ]]; then
echo "Check didn't fail correctly"
exit 1
fi
if [[ "$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/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-1
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Plan workspace
uses: ./terraform-plan
id: plan
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
path: tests/workflows/test-cloud/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-2
backend_config: token=${{ secrets.TF_API_TOKEN }}
var_file: |
tests/workflows/test-cloud/${{ matrix.tf_version }}/my_variable.tfvars
variables: |
from_variables="from_variables"
- name: Verify plan outputs
env:
PLAN_CHANGES: ${{ steps.plan.outputs.changes }}
TEXT_PLAN_PATH: ${{ steps.plan.outputs.text_plan_path }}
JSON_PLAN_PATH: ${{ steps.plan.outputs.json_plan_path }}
RUN_ID: ${{ steps.plan.outputs.run_id }}
run: |
if [[ "$PLAN_CHANGES" != "true" ]]; then
echo "::error:: output changes not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" "$TEXT_PLAN_PATH"; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ "$RUN_ID" != "run-"* ]]; then
echo "::error:: output run_id not set correctly"
exit 1
fi
jq .output_changes.from_variables.actions[0] "$JSON_PLAN_PATH"
if [[ $(jq -r .output_changes.from_variables.actions[0] "$JSON_PLAN_PATH") != "create" ]]; then
echo "::error:: json_plan_path 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/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-2
backend_config: token=${{ secrets.TF_API_TOKEN }}
var_file: |
tests/workflows/test-cloud/${{ matrix.tf_version }}/my_variable.tfvars
variables: |
from_variables="from_variables"
- name: Verify apply terraform outputs
env:
OUTPUT_DEFAULT: ${{ steps.apply.outputs.default }}
FROM_TFVARS: ${{ steps.apply.outputs.from_tfvars }}
FROM_VARIABLES: ${{ steps.apply.outputs.from_variables }}
TEXT_PLAN_PATH: ${{ steps.apply.outputs.text_plan_path }}
JSON_PLAN_PATH: ${{ steps.apply.outputs.json_plan_path }}
RUN_ID: ${{ steps.apply.outputs.run_id }}
run: |
if [[ "$OUTPUT_DEFAULT" != "default" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if [[ "$FROM_TFVARS" != "from_tfvars" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if [[ "$FROM_VARIABLES" != "from_variables" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" "$TEXT_PLAN_PATH"; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ -n "$JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should not be set"
exit 1
fi
if [[ "$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/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-2
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Destroy non-existent workspace
uses: ./terraform-destroy-workspace
continue-on-error: true
id: destroy-non-existant-workspace
with:
path: tests/workflows/test-cloud/${{ matrix.tf_version }}
workspace: ${{ github.head_ref }}-1
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Check failed to destroy
env:
DESTROY_OUTCOME: ${{ steps.destroy-non-existant-workspace.outcome }}
run: |
if [[ "$DESTROY_OUTCOME" != "failure" ]]; then
echo "Destroy non-existant workspace"
exit 1
fi
cloud:
runs-on: ubuntu-24.04
name: Partial cloud config
permissions:
contents: read
pull-requests: write
env:
TF_CLOUD_ORGANIZATION: flooktech
TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- 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
env:
OUTPUT_LEN: ${{ steps.auto_apply.outputs.len }}
TEXT_PLAN_PATH: ${{ steps.auto_apply.outputs.text_plan_path }}
JSON_PLAN_PATH: ${{ steps.auto_apply.outputs.json_plan_path }}
RUN_ID: ${{ steps.auto_apply.outputs.run_id }}
run: |
if [[ "$OUTPUT_LEN" != "5" ]]; then
echo "::error:: output not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" "$TEXT_PLAN_PATH"; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ ! -f "$JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should be set"
exit 1
fi
if [[ "$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
env:
OUTPUT_LEN: ${{ steps.output.outputs.len }}
run: |
if [[ "$OUTPUT_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
env:
CHECK_OUTCOME: ${{ steps.check.outcome }}
FAILURE_REASON: ${{ steps.check.outputs.failure-reason }}
run: |
if [[ "$CHECK_OUTCOME" != "failure" ]]; then
echo "Check didn't fail correctly"
exit 1
fi
if [[ "$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 changes
uses: ./terraform-plan
id: plan
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
label: test-cloud cloud plan
path: tests/workflows/test-cloud/partial
workspace: ${{ github.head_ref }}-cloud-2
- name: Verify plan outputs
env:
PLAN_CHANGES: ${{ steps.plan.outputs.changes }}
TEXT_PLAN_PATH: ${{ steps.plan.outputs.text_plan_path }}
JSON_PLAN_PATH: ${{ steps.plan.outputs.json_plan_path }}
RUN_ID: ${{ steps.plan.outputs.run_id }}
run: |
if [[ "$PLAN_CHANGES" != "true" ]]; then
echo "::error:: output changes not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" "$TEXT_PLAN_PATH"; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ ! -f "$JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should be set"
exit 1
fi
if [[ "$RUN_ID" != "run-"* ]]; then
echo "::error:: output run_id not set correctly"
exit 1
fi
- name: Apply changes
uses: ./terraform-apply
id: apply
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
label: test-cloud cloud plan
path: tests/workflows/test-cloud/partial
workspace: ${{ github.head_ref }}-cloud-2
- name: Verify apply terraform outputs
env:
OUTPUT_LEN: ${{ steps.apply.outputs.len }}
TEXT_PLAN_PATH: ${{ steps.apply.outputs.text_plan_path }}
JSON_PLAN_PATH: ${{ steps.apply.outputs.json_plan_path }}
RUN_ID: ${{ steps.apply.outputs.run_id }}
run: |
if [[ "$OUTPUT_LEN" != "5" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" "$TEXT_PLAN_PATH"; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ ! -f "$JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should be set"
exit 1
fi
if [[ "$RUN_ID" != "run-"* ]]; then
echo "::error:: output run_id not set correctly"
exit 1
fi
- name: Plan no changes
uses: ./terraform-plan
id: plan-no-changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
label: test-cloud cloud plan-no-changes
path: tests/workflows/test-cloud/partial
workspace: ${{ github.head_ref }}-cloud-2
- name: Apply no changes
uses: ./terraform-apply
id: apply-no-changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
label: test-cloud cloud plan-no-changes
path: tests/workflows/test-cloud/partial
workspace: ${{ github.head_ref }}-cloud-2
- name: Verify apply with no changes
env:
PLAN_CHANGES: ${{ steps.plan-no-changes.outputs.changes }}
APPLY_OUTPUT_LEN: ${{ steps.apply-no-changes.outputs.len }}
PLAN_TEXT_PLAN_PATH: ${{ steps.plan-no-changes.outputs.text_plan_path }}
APPLY_TEXT_PLAN_PATH: ${{ steps.apply-no-changes.outputs.text_plan_path }}
PLAN_JSON_PLAN_PATH: ${{ steps.plan-no-changes.outputs.json_plan_path }}
APPLY_JSON_PLAN_PATH: ${{ steps.apply-no-changes.outputs.json_plan_path }}
PLAN_RUN_ID: ${{ steps.plan-no-changes.outputs.run_id }}
APPLY_RUN_ID: ${{ steps.apply-no-changes.outputs.run_id }}
run: |
if [[ "$PLAN_CHANGES" != "false" ]]; then
echo "::error:: changes output not set correctly"
exit 1
fi
if [[ "$APPLY_OUTPUT_LEN" != "5" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if ! grep -q "No changes. Your infrastructure matches the configuration." "$PLAN_TEXT_PLAN_PATH"; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if ! grep -q "No changes. Your infrastructure matches the configuration." "$APPLY_TEXT_PLAN_PATH"; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ ! -f "$PLAN_JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should be set"
exit 1
fi
if [[ ! -f "$APPLY_JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should be set"
exit 1
fi
if [[ "$PLAN_RUN_ID}" != "run-"* ]]; then
echo "::error:: output run_id not set correctly"
exit 1
fi
if [[ "$APPLY_RUN_ID" != "run-"* ]]; then
echo "::error:: output run_id not set correctly"
exit 1
fi
- name: Saved plan with changes
uses: ./terraform-plan
id: saved-plan-changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
label: test-cloud cloud save-plan-changes
path: tests/workflows/test-cloud/partial
workspace: ${{ github.head_ref }}-cloud-2
variables: |
length=8
- name: Saved apply with changes
uses: ./terraform-apply
id: saved-apply-changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
label: test-cloud cloud save-plan-changes
path: tests/workflows/test-cloud/partial
plan_path: ${{ steps.saved-plan-changes.outputs.plan_path }}
workspace: ${{ github.head_ref }}-cloud-2
variables: |
length=8
- name: Verify saved apply with changes
env:
SAVED_PLAN_CHANGES: ${{ steps.saved-plan-changes.outputs.changes }}
SAVED_APPLY_OUTPUT_LEN: ${{ steps.saved-apply-changes.outputs.len }}
SAVED_PLAN_TEXT_PLAN_PATH: ${{ steps.saved-plan-changes.outputs.text_plan_path }}
SAVED_PLAN_JSON_PLAN_PATH: ${{ steps.saved-plan-changes.outputs.json_plan_path }}
SAVED_PLAN_RUN_ID: ${{ steps.saved-plan-changes.outputs.run_id }}
SAVED_APPLY_RUN_ID: ${{ steps.saved-apply-changes.outputs.run_id }}
run: |
if [[ "$SAVED_PLAN_CHANGES" != "true" ]]; then
echo "::error:: changes output not set correctly"
exit 1
fi
if [[ "$SAVED_APPLY_OUTPUT_LEN" != "8" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if ! grep -q "Terraform will perform the following actions" $SAVED_PLAN_TEXT_PLAN_PATH; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ ! -f "$SAVED_PLAN_JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should be set"
exit 1
fi
if [[ "$SAVED_PLAN_RUN_ID" != "run-"* ]]; then
echo "::error:: output run_id not set correctly"
exit 1
fi
if [[ "$SAVED_APPLY_RUN_ID" != "run-"* ]]; then
echo "::error:: output run_id not set correctly"
exit 1
fi
- name: Saved plan with no changes
uses: ./terraform-plan
id: saved-plan-no-changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
label: test-cloud cloud save-plan-no-changes
path: tests/workflows/test-cloud/partial
workspace: ${{ github.head_ref }}-cloud-2
variables: |
length=8
- name: Saved apply with no changes
uses: ./terraform-apply
id: saved-apply-no-changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
label: test-cloud cloud save-plan-no-changes
path: tests/workflows/test-cloud/partial
plan_path: ${{ steps.saved-plan-no-changes.outputs.plan_path }}
workspace: ${{ github.head_ref }}-cloud-2
variables: |
length=8
- name: Verify saved apply with no changes
env:
SAVED_PLAN_NO_CHANGES: ${{ steps.saved-plan-no-changes.outputs.changes }}
SAVED_APPLY_OUTPUT_LEN: ${{ steps.saved-apply-no-changes.outputs.len }}
SAVED_PLAN_TEXT_PLAN_PATH: ${{ steps.saved-plan-no-changes.outputs.text_plan_path }}
SAVED_PLAN_JSON_PLAN_PATH: ${{ steps.saved-plan-no-changes.outputs.json_plan_path }}
SAVED_PLAN_RUN_ID: ${{ steps.saved-plan-no-changes.outputs.run_id }}
SAVED_APPLY_RUN_ID: ${{ steps.saved-apply-no-changes.outputs.run_id }}
run: |
if [[ "$SAVED_PLAN_NO_CHANGES" != "false" ]]; then
echo "::error:: changes output not set correctly"
exit 1
fi
if [[ "$SAVED_APPLY_OUTPUT_LEN" != "8" ]]; then
echo "::error:: Variables not set correctly"
exit 1
fi
if ! grep -q "No changes. Your infrastructure matches the configuration." "$SAVED_PLAN_TEXT_PLAN_PATH"; then
echo "::error:: text_plan_path not set correctly"
exit 1
fi
if [[ ! -f "$SAVED_PLAN_JSON_PLAN_PATH" ]]; then
echo "::error:: json_plan_path should be set"
exit 1
fi
if [[ "$SAVED_PLAN_RUN_ID" != "run-"* ]]; then
echo "::error:: output run_id not set correctly"
exit 1
fi
if [[ "$SAVED_APPLY_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
env:
DESTROY_OUTCOME: ${{ steps.destroy-non-existent-workspace.outcome }}
run: |
if [[ "$DESTROY_OUTCOME" != "failure" ]]; then
echo "Destroy non-existant workspace"
exit 1
fi