From 12dd737c8afb1ee3dc58474f1e0a64833dd0dc60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Hakan=20Gen=C3=A7o=C4=9Flu?= Date: Fri, 23 Aug 2024 11:16:36 +0300 Subject: [PATCH] chore: setup workflow for terraform ci (#256) --- .github/workflows/terraform-ci.yaml | 64 +++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 .github/workflows/terraform-ci.yaml diff --git a/.github/workflows/terraform-ci.yaml b/.github/workflows/terraform-ci.yaml new file mode 100644 index 0000000..6603279 --- /dev/null +++ b/.github/workflows/terraform-ci.yaml @@ -0,0 +1,64 @@ +name: "PR Title Lint" + +permissions: + pull-requests: write + +on: + push: + branches: + - main + paths: + - modules/**/*.tf + pull_request: + branches: + - main + paths: + - modules/**/*.tf + +jobs: + terraform-checks: + name: Validate Terraform format + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup Terraform + uses: hashicorp/setup-terraform@v2 + with: + terraform_version: 1.5.7 # Specify your desired Terraform version + + - name: Setup TFLint + uses: terraform-linters/setup-tflint@v3 + with: + tflint_version: v0.53.0 # Specify your desired TFLint version + + - name: Get changed modules + id: changed-modules + uses: tj-actions/changed-files@v35 + with: + files: modules/**/*.tf + + - name: Terraform fmt + run: terraform fmt -check -recursive -diff modules/ + continue-on-error: true + + - name: Check Terraform formatting + run: | + if [ ${{ steps.fmt.outcome }} == "failure" ]; then + echo "Terraform files are not properly formatted. Please run 'terraform fmt' to fix." + exit 1 + fi + + - name: Run TFLint + run: | + for file in ${{ steps.changed-modules.outputs.all_changed_files }}; do + module_dir=$(dirname "$file") + if [ ! -f "$module_dir/.tflint.hcl" ]; then + echo "module_dir=$module_dir" >> $GITHUB_OUTPUT + echo "No .tflint.hcl found in $module_dir. Skipping." + continue + fi + echo "Running TFLint in $module_dir" + tflint -f compact "$module_dir" + done