Skip to content

check drift between terraform and actual #20

check drift between terraform and actual

check drift between terraform and actual #20

name: check drift between terraform and actual
on:
workflow_dispatch:
schedule:
- cron: '0 17 * * 0'
permissions:
id-token: write
contents: read
env:
TF_VAR_REMOTE_STATE_BUCKET: ${{ secrets.TF_VAR_REMOTE_STATE_BUCKET }}
TF_VAR_BACKEND_CONFIG_KEY: ${{ secrets.TF_VAR_BACKEND_CONFIG_KEY }}
TF_VAR_AWS_REGION: ${{ secrets.TF_VAR_AWS_REGION }}
jobs:
target-directory:
runs-on: ubuntu-latest
outputs:
target-directories: ${{ steps.target-directory.outputs.value }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: '1'
- name: Get target directory name
id: target-directory
working-directory: "infrastructure"
run: |
TARGET_DIRECTORY=$(find . -type f -name "main.tf" | sed -e 's/\/main.tf//g' -e 's/^\.\//"/g' -e 's/$/"/g' | tr '\n' ',' | sed 's/^/[/;s/,$/]/')
echo "value=$TARGET_DIRECTORY" >> "$GITHUB_OUTPUT"
check-drift:
runs-on: ubuntu-latest
needs: target-directory
strategy:
matrix:
directory: ${{ fromJson(needs.target-directory.outputs.target-directories) }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: '1'
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/oidc-github-actions-role
aws-region: us-east-1
- name: Get Terraform version
id: terraform_version
run: |
TERRAFORM_VERSION=$(grep terraform .tool-versions | cut -d' ' -f2)
echo "terraform_version=$TERRAFORM_VERSION" >> "$GITHUB_OUTPUT"
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ steps.terraform_version.outputs.terraform_version }}
- name: Generate config file
id: generate-config-file
working-directory: "infrastructure"
run: |
`cat <<EOF > config.tfbackend
bucket = "${TF_VAR_REMOTE_STATE_BUCKET}"
key = "${TF_VAR_BACKEND_CONFIG_KEY}"
region = "${TF_VAR_AWS_REGION}"
EOF`
CONFIG_TFBACKEND_FILE=$(pwd)/config.tfbackend
echo "config_tfbackend_file=$CONFIG_TFBACKEND_FILE" >> "$GITHUB_OUTPUT"
- name: Init
id: terraform-init
working-directory: "infrastructure/${{ matrix.directory }}"
run: |
terraform init -backend-config ${{ steps.generate-config-file.outputs. config_tfbackend_file }}
- name: Terraform Validate
id: validate
working-directory: "infrastructure/${{ matrix.directory }}"
run: terraform validate -no-color
- name: terraform-plan
id: plan
working-directory: "infrastructure/${{ matrix.directory }}"
run: terraform plan -no-color -detailed-exitcode
continue-on-error: true