Skip to content

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

docs: plan-file download and tflint workflow examples

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

Workflow file for this run

---
name: TF Tests
on:
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: opentofu/setup-opentofu@12f4debbf681675350b6cd1f0ff8ecfbda62027b # v1.0.4
with:
tofu_version: 1.8.5
tofu_wrapper: false
- 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
tool: tofu
- 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: ${{ github.event_name == 'pull_request' && steps.tflint.outputs.exitcode != 0 }}
env:
GH_TOKEN: ${{ github.token }}
run: |
# Compose TFLint output.
tflint="
<details><summary>TFLint error.</summary>
\`\`\`hcl
${{ steps.tflint.outputs.stderr || steps.tflint.outputs.stdout }}
\`\`\`
</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