Skip to content

chore(ci): validate terraform modules in CI #48

chore(ci): validate terraform modules in CI

chore(ci): validate terraform modules in CI #48

Workflow file for this run

name: CI
on:
pull_request:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
pull-requests: read
jobs:
fmt:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3
- name: Format
run: terraform fmt -recursive -check
discover:
runs-on: ubuntu-latest
outputs:
TERRAFORM_MODULES: ${{ steps.find-modules.outputs.TERRAFORM_MODULES }}
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
- name: Find Terraform Module Groups
id: find-modules
run: |
find modules examples -maxdepth 2 -mindepth 2 -type d | sort -u | sed 's/^\.\///' | jq -R -s -c 'split("\n")[:-1]' > /tmp/modules.json
modules_raw=$(cat /tmp/modules.json)
echo "TERRAFORM_MODULES=$modules_raw" >> $GITHUB_OUTPUT
- name: Display Terraform Modules
run: |
echo "Terraform Modules:"
echo "${{ steps.find-modules.outputs.TERRAFORM_MODULES }}"
validate-modules:
runs-on: ubuntu-latest
needs: discover
strategy:
fail-fast: false
matrix:
module: ${{ fromJson(needs.discover.outputs.TERRAFORM_MODULES) }}
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3
- name: Discover Modules
run: |
dirs=$(find "${{ matrix.module }}" -type f -name "*.tf" -exec dirname {} \; | sort | uniq)
echo "MODULE_DIRS=$dirs" >> $GITHUB_ENV
- name: Initialize
working-directory: ${{ matrix.module }}
run: |
for dir in $MODULE_DIRS; do
terraform init $dir
done
- name: Validate
working-directory: ${{ matrix.module }}
env:
# examples/data-processing/database-migration-service causes crash in terraform validate on GHA
excluded_directories: |
modules/networking/vpc-flow-logs
run: |
excluded_dirs=($excluded_directories)
for dir in $MODULE_DIRS; do
if [[ ! " ${excluded_dirs[@]} " =~ " $dir " ]]; then
terraform validate -json -check-variables=false $dir
fi
done