Skip to content

Update to support Terraform cloud with Terraform 1.6 #419

Update to support Terraform cloud with Terraform 1.6

Update to support Terraform cloud with Terraform 1.6 #419

name: Test plan target and replace
on:
- pull_request
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
plan_targeting:
runs-on: ubuntu-latest
name: Plan targeting
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Plan with no changes in targets
uses: ./terraform-plan
id: plan
with:
label: test-target-replace plan_targeting
path: tests/workflows/test-target-replace
target: |
random_string.notpresent
variables: |
length = 5
- name: Verify outputs
run: |
if [[ "${{ steps.plan.outputs.changes }}" != "false" ]]; then
echo "::error:: Should not be any changes with this targeted plan"
exit 1
fi
- name: Plan targeted change
uses: ./terraform-plan
id: plan-first-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.count[0]
variables: |
length = 5
- name: Verify outputs
run: |
if [[ "${{ steps.plan-first-change.outputs.changes }}" != "true" ]]; then
echo "::error:: Targeted plan should have changes"
exit 1
fi
- name: Apply targeted change
uses: ./terraform-apply
id: apply-first-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.count[0]
variables: |
length = 5
- name: Verify outputs
run: |
if [[ "${{ steps.apply-first-change.outputs.count }}" == "" ]]; then
echo "::error:: output count not set correctly"
exit 1
fi
- name: Plan targeted change
uses: ./terraform-plan
id: plan-second-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.foreach["hello"]
variables: |
length = 6
- name: Verify outputs
run: |
if [[ "${{ steps.plan-second-change.outputs.changes }}" != "true" ]]; then
echo "::error:: Targeted plan should have changes"
exit 1
fi
- name: Apply targeted change
uses: ./terraform-apply
id: apply-second-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.foreach["hello"]
variables: |
length = 6
- name: Verify outputs
run: |
if [[ "${{ steps.apply-second-change.outputs.foreach }}" == "" ]]; then
echo "::error:: output foreach not set correctly"
exit 1
fi
if [[ "${{ steps.apply-second-change.outputs.count }}" != "${{ steps.apply-first-change.outputs.count }}" ]]; then
echo "::error:: Targeted change has affected untargeted resources"
exit 1
fi
- name: Auto Apply targeted change
uses: ./terraform-apply
id: apply-third-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.count[0]
random_string.foreach["hello"]
variables: |
length = 10
auto_approve: true
- name: Verify outputs
run: |
if [[ "${{ steps.apply-third-change.outputs.count }}" == "${{ steps.apply-second-change.outputs.count }}" ]]; then
echo "::error:: Targeted change has not affected targeted resources"
exit 1
fi
if [[ "${{ steps.apply-third-change.outputs.foreach }}" == "${{ steps.apply-second-change.outputs.foreach }}" ]]; then
echo "::error:: Targeted change has not affected targeted resources"
exit 1
fi
- name: Plan targeted replacement
uses: ./terraform-plan
id: plan-targeted-replacement
with:
path: tests/workflows/test-target-replace
target: |
random_string.foreach["hello"]
replace: |
random_string.foreach["hello"]
random_string.count[0]
variables: |
length = 10
- name: Verify outputs
run: |
if [[ "${{ steps.plan-targeted-replacement.outputs.changes }}" != "true" ]]; then
echo "::error:: Targeted replacement should have changes"
exit 1
fi
- name: Apply targeted replacement
uses: ./terraform-apply
id: apply-targeted-replacement
with:
path: tests/workflows/test-target-replace
target: |
random_string.foreach["hello"]
replace: |
random_string.foreach["hello"]
random_string.count[0]
variables: |
length = 10
- name: Verify outputs
run: |
if [[ "${{ steps.apply-targeted-replacement.outputs.count }}" != "${{ steps.apply-third-change.outputs.count }}" ]]; then
echo "::error:: Targeted replacement has affected non targeted resources"
exit 1
fi
if [[ "${{ steps.apply-targeted-replacement.outputs.foreach }}" == "${{ steps.apply-third-change.outputs.foreach }}" ]]; then
echo "::error:: Targeted replacement has not affected targeted resources"
exit 1
fi
- name: Plan replacement
uses: ./terraform-plan
id: plan-replacement
with:
path: tests/workflows/test-target-replace
replace: |
random_string.foreach["hello"]
random_string.count[0]
variables: |
length = 10
- name: Verify outputs
run: |
if [[ "${{ steps.plan-replacement.outputs.changes }}" != "true" ]]; then
echo "::error:: Replacement should have changes"
exit 1
fi
- name: Apply replacement
uses: ./terraform-apply
id: apply-replacement
with:
path: tests/workflows/test-target-replace
replace: |
random_string.foreach["hello"]
random_string.count[0]
variables: |
length = 10
- name: Verify outputs
run: |
if [[ "${{ steps.apply-replacement.outputs.count }}" == "${{ steps.apply-targeted-replacement.outputs.count }}" ]]; then
echo "::error:: Replacement has not affected targeted resources"
exit 1
fi
if [[ "${{ steps.apply-replacement.outputs.foreach }}" == "${{ steps.apply-targeted-replacement.outputs.foreach }}" ]]; then
echo "::error:: Replacement has not affected targeted resources"
exit 1
fi
remote_plan_targeting:
runs-on: ubuntu-latest
name: Remote Plan targeting
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup remote backend
run: |
cat >tests/workflows/test-target-replace/backend.tf <<EOF
terraform {
backend "remote" {
organization = "flooktech"
workspaces {
prefix = "github-actions-replace-"
}
}
required_version = "~> 1.0.4"
}
EOF
- name: Create test workspace
uses: ./terraform-new-workspace
with:
path: tests/workflows/test-target-replace
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Plan with no changes in targets
uses: ./terraform-plan
id: plan
with:
label: test-target-replace remote_plan_targeting
path: tests/workflows/test-target-replace
target: |
random_string.notpresent
variables: |
length = 5
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.plan.outputs.changes }}" != "false" ]]; then
echo "::error:: Should not be any changes with this targeted plan"
exit 1
fi
- name: Plan targeted change
uses: ./terraform-plan
id: plan-first-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.count[0]
variables: |
length = 5
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.plan-first-change.outputs.changes }}" != "true" ]]; then
echo "::error:: Targeted plan should have changes"
exit 1
fi
- name: Apply targeted change
uses: ./terraform-apply
id: apply-first-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.count[0]
variables: |
length = 5
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.apply-first-change.outputs.count }}" == "" ]]; then
echo "::error:: output count not set correctly"
exit 1
fi
- name: Plan targeted change
uses: ./terraform-plan
id: plan-second-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.foreach["hello"]
variables: |
length = 6
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.plan-second-change.outputs.changes }}" != "true" ]]; then
echo "::error:: Targeted plan should have changes"
exit 1
fi
- name: Apply targeted change
uses: ./terraform-apply
id: apply-second-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.foreach["hello"]
variables: |
length = 6
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.apply-second-change.outputs.foreach }}" == "" ]]; then
echo "::error:: output foreach not set correctly"
exit 1
fi
if [[ "${{ steps.apply-second-change.outputs.count }}" != "${{ steps.apply-first-change.outputs.count }}" ]]; then
echo "::error:: Targeted change has affected untargeted resources"
exit 1
fi
- name: Auto Apply targeted change
uses: ./terraform-apply
id: apply-third-change
with:
path: tests/workflows/test-target-replace
target: |
random_string.count[0]
random_string.foreach["hello"]
variables: |
length = 10
auto_approve: true
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.apply-third-change.outputs.count }}" == "${{ steps.apply-second-change.outputs.count }}" ]]; then
echo "::error:: Targeted change has not affected targeted resources"
exit 1
fi
if [[ "${{ steps.apply-third-change.outputs.foreach }}" == "${{ steps.apply-second-change.outputs.foreach }}" ]]; then
echo "::error:: Targeted change has not affected targeted resources"
exit 1
fi
- name: Plan targeted replacement
uses: ./terraform-plan
id: plan-targeted-replacement
with:
path: tests/workflows/test-target-replace
target: |
random_string.foreach["hello"]
replace: |
random_string.foreach["hello"]
random_string.count[0]
variables: |
length = 10
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.plan-targeted-replacement.outputs.changes }}" != "true" ]]; then
echo "::error:: Targeted replacement should have changes"
exit 1
fi
- name: Apply targeted replacement
uses: ./terraform-apply
id: apply-targeted-replacement
with:
path: tests/workflows/test-target-replace
target: |
random_string.foreach["hello"]
replace: |
random_string.foreach["hello"]
random_string.count[0]
variables: |
length = 10
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.apply-targeted-replacement.outputs.count }}" != "${{ steps.apply-third-change.outputs.count }}" ]]; then
echo "::error:: Targeted replacement has affected non targeted resources"
exit 1
fi
if [[ "${{ steps.apply-targeted-replacement.outputs.foreach }}" == "${{ steps.apply-third-change.outputs.foreach }}" ]]; then
echo "::error:: Targeted replacement has not affected targeted resources"
exit 1
fi
- name: Plan replacement
uses: ./terraform-plan
id: plan-replacement
with:
path: tests/workflows/test-target-replace
replace: |
random_string.foreach["hello"]
random_string.count[0]
variables: |
length = 10
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.plan-replacement.outputs.changes }}" != "true" ]]; then
echo "::error:: Replacement should have changes"
exit 1
fi
- name: Apply replacement
uses: ./terraform-apply
id: apply-replacement
with:
path: tests/workflows/test-target-replace
replace: |
random_string.foreach["hello"]
random_string.count[0]
variables: |
length = 10
workspace: ${{ github.head_ref }}
backend_config: token=${{ secrets.TF_API_TOKEN }}
- name: Verify outputs
run: |
if [[ "${{ steps.apply-replacement.outputs.count }}" == "${{ steps.apply-targeted-replacement.outputs.count }}" ]]; then
echo "::error:: Replacement has not affected targeted resources"
exit 1
fi
if [[ "${{ steps.apply-replacement.outputs.foreach }}" == "${{ steps.apply-targeted-replacement.outputs.foreach }}" ]]; then
echo "::error:: Replacement has not affected targeted resources"
exit 1
fi
- name: Destroy the workspace
uses: ./terraform-destroy-workspace
with:
path: tests/workflows/test-target-replace
workspace: ${{ github.head_ref }}
variables: |
length = 10
backend_config: token=${{ secrets.TF_API_TOKEN }}