Skip to content

docs: plan-file download and tflint workflow examples #328

docs: plan-file download and tflint workflow examples

docs: plan-file download and tflint workflow examples #328

Workflow file for this run

---
name: TF Tests
on:
# push:
pull_request:
paths: [.github/workflows/tf_tests.yaml, action.yml, tests/**]
types: [opened, reopened, synchronize, closed]
jobs:
tests:
runs-on: ubuntu-24.04
permissions:
actions: read # Required to download repository artifact.
checks: write # Required to add status summary.
contents: read # Required to checkout repository.
pull-requests: write # Required to add PR comment and label.
strategy:
fail-fast: false
matrix:
test:
- pass_one
# - pass_character_limit
# - fail_data_source_error
# - fail_format_diff
# - fail_invalid_resource_type
steps:
- name: Echo context
env:
GH_JSON: ${{ toJson(github) }}
run: echo "$GH_JSON"
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- name: Setup TF
uses: hashicorp/setup-terraform@v3
- name: Init TF
id: tf
if: ${{ github.event_name == 'pull_request' }}
uses: ./
with:
command: init
arg-lock: false
working-directory: tests/${{ matrix.test }}
format: true
validate: true
- name: Setup TFLint
if: ${{ github.event_name == 'pull_request' }}
uses: terraform-linters/setup-tflint@v4
with:
tflint_wrapper: true
- name: Run TFLint
id: tflint
if: ${{ github.event_name == 'pull_request' }}
working-directory: tests/${{ matrix.test }}
run: |
tflint --init
tflint --format compact
continue-on-error: true
- name: Comment if TFLint errors
if: ${{ steps.tflint.outputs.exitcode >= 0 }}
env:
GH_TOKEN: ${{ github.token }}
run: |
# Compose TFLint output.
# tflint=$(cat <<EOF
# <details><summary>TFLint error.</summary>
# \`\`\`hcl
# 2 issue(s) found:
# Warning: main.tf:1:1: Warning - Missing version constraint for provider "random" in `required_providers` (terraform_required_providers)
# Warning: main.tf:1:1: Warning - terraform "required_version" attribute is required (terraform_required_version)
# \`\`\`
# </details>
# EOF
# )
tflint='
2 issue(s) found:
Warning: main.tf:1:1: Warning - Missing version constraint for provider "random" in `required_providers` (terraform_required_providers)
Warning: main.tf:1:1: Warning - terraform "required_version" attribute is required (terraform_required_version)
'
tflint=$(echo "$tflint" | sed 's/`/\\`/g')
tflint="
<details><summary>TFLint error.</summary>
```hcl
${tflint}
```
</details>
"
# Get body of PR comment from tf step output.
comment=$(gh api /repos/{owner}/{repo}/issues/comments/${{ steps.tf.outputs.comment-id }} --method GET --jq '.body')
# Replace placeholder with TFLint output.
comment="${comment//<!-- placeholder-2 -->/$tflint}"
# Update PR comment combined with TFLint output.
gh api /repos/{owner}/{repo}/issues/comments/${{ steps.tf.outputs.comment-id }} --method PATCH --field body="$comment"
# Exit workflow due to TFLint error.
# exit 1