はじめに
今回はsuzuki-shunsuke/github-action-tflint
を用いてterraformのlinter(tflint)をActionsに導入する方法を紹介したいと思います。

reviewdog/action-tflintについて
有名なterraformのlinter用actionとしてreviewdog/action-tflint
があります。
github.com
ただ今回紹介するsuzuki-shunsuke/github-action-tflint
の違いの一つとして、GitHub Token
を細かく制御できるかどうかがあります。
# reviewdog/action-tfilnt inputs: github_token: description: 'GITHUB_TOKEN' required: true default: ${{ github.token }}
# suzuki-shunsuke/github-action-tflint inputs: github_token: description: 'GitHub Access Token' required: false default: ${{ github.token }} github_token_for_tflint_init: description: GitHub Access Token for tflint --init. If this input isn't set, github_token is used required: false github_token_for_fix: description: GitHub Access Token for tflint --fix. If this input isn't set, github_token is used required: false
この影響としてEnterprise Serverとかだとreviewdog/action-tfilnt
を動かせないこともあるので注意してください。
概要
terraform
のlinterとしてTFLint
があります。またそれをsetup
するActionとしてsetup-tflint
というものも公式が用意してくれています。
github.com
github.com
ただ実用を見据えると簡単にGitHub上で分かりやすく表示してほしいよねということで、今回紹介するsuzuki-shunsuke/github-action-tflint
はreviewdog
やgithub-comment
を用いて以下のような見た目で通知を行ってくれます。
Run tflint and notify the result with reviewdog and github-comment. This GitHub Actions does not install tflint and reviewdog, so you have to install them in advance. It allows to install tools outside this action. We recommend aqua to install them.
GitHub - suzuki-shunsuke/github-action-tflint: GitHub Actions for tflint

導入方法
作者さんが作られたaqua
というものを活用することで、簡単にセットアップをすることができます。readmeのMotivationでもaquaを言及していますね。
aquaproj.github.io
レポジトリルートにaqua.yaml
を用意してあげて、依存先のパッケージを記述してあげます。
--- # aqua - Declarative CLI Version Manager # https://aquaproj.github.io/ # checksum: # enabled: true # require_checksum: true # supported_envs: # - all registries: - type: standard ref: v4.331.0 # renovate: depName=aquaproj/aqua-registry packages: - name: suzuki-shunsuke/github-comment@v6.3.2 - name: terraform-linters/tflint@v0.55.1 - name: reviewdog/reviewdog@v0.20.3
準備が終われば、あとはワークフローを書いていけばOKです。
# .github/workflows/terraform-lint.tf name: terraform lint on: pull_request: paths: - "**.tf**" jobs: terraformlint: runs-on: ubuntu-latest permissions: pull-requests: write # for github_token contents: read # for github_token_for_tflint_init (change to write for github_token_for_fix) steps: - uses: actions/checkout@v4 - uses: aquaproj/aqua-installer@e2d0136abcf70b7a2f6f505720640750557c4b33 # v3.1.1 with: aqua_version: v2.46.0 - uses: suzuki-shunsuke/github-action-tflint@a9d1f3376c8bbd6aadf4891dfe32f5e246c9c52f # v1.2.1 with: working_directory: terraform github_comment: true