Skip to content

TFLintのインストール時にGithub Actionsのキャッシュを使用 #275

TFLintのインストール時にGithub Actionsのキャッシュを使用

TFLintのインストール時にGithub Actionsのキャッシュを使用 #275

Workflow file for this run

name: test
# PR作成時、コミットプッシュ時にワークフローを実行
on:
pull_request:
branches:
- main
types: [opened, synchronize]
# ワークフローの同時実行数を1にするための設定
# 複数のPRからterraform applyやterraform testが同時実行されないようにする
# groupの値をstg.yamlやprod.yamlでも同じにすると、全環境で1度に1つのワークフローしか実行できなくなるので注意
# (ただしstagingのapply中にこのワークフローを実行すると、plan時にロックが取得できずにエラーが出るのでその辺りの取り扱いはポリシー次第)
# cancel-in-progress:falseにすると既に実行済みのワークフローの実行が完了するまで、後続のワークフローは待機するようになる
# cancel-in-progress:trueにすると既に実行済みのワークフローをキャンセルして、後続のワークフローを開始する
concurrency:
group: test
cancel-in-progress: false
# すべてのアクセス許可を無効に設定
permissions: {}
env:
TFLINT_VERSION: 0.50.3
CONFTEST_VERSION: 0.51.0
jobs:
validation:
runs-on: ubuntu-22.04
steps:
# プロジェクトのチェックアウト(ジョブ毎に実行必須)
- name: checkout project
# @vXのようなバージョン指定にすると、最新バージョンでセキュリティ的に問題があるアクションを意図せず使用してしまう可能性がある
# よって@vX.Y.Zにすればリスクは軽減するが、それでもバージョンは自由に作り直せるのでより厳密にコミットハッシュを指定
# v4.1.1 ref. https://github.com/actions/checkout/releases/tag/v4.1.1
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# # テストコード実行対象モジュールリストを定義する.github/data/test_matrix.yamlとterraform/modulesディレクトリが
# # 同期されている状態かチェックするスクリプトを実行
# - name: check test_matrix.yaml
# run: |
# ./scripts/check_test_matrix.sh
- name: cache tflint CLI
id: tflint-cache
# v4.0.2 ref. https://github.com/actions/cache/releases/tag/v4.0.2
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
with:
path: /usr/local/bin/tflint
key: tflint-${{ env.TFLINT_VERSION }}
- name: install tflint if the cache doesn't exist
if: steps.tflint-cache.outputs.cache-hit != 'true'
run: |
# v0.50.3 ref. https://github.com/terraform-linters/tflint/releases/tag/v0.50.3
wget -O /tmp/install_tflint.sh https://raw.githubusercontent.com/terraform-linters/tflint/e34bda7f398c139d7bc528261ee96998c10e79c8/install_linux.sh
echo "------------------------------------------------"
cat /tmp/install_tflint.sh
echo "------------------------------------------------"
sh /tmp/install_tflint.sh
- name: test tflint
run: |
tflint --version
# # environmentsやmodules配下のHCLファイルが格納されているディレクトリ全てでtflintコマンドを実行
# - name: run tflint
# run: |
# ./scripts/tflint.sh
# # terraform CLIをインストール
# - name: install terraform CLI
# uses: ./.github/actions/setup
# with:
# auth_gcloud: "false"
# # environmentsやmodules配下のHCLファイルが格納されているディレクトリ全てでterraform fmt -checkコマンドを実行
# - name: run terraform fmt
# run: |
# ./scripts/tffmt.sh
# # environmentsやmodules配下のHCLファイルが格納されているディレクトリ全てでterraform validateコマンドを実行
# - name: run terraform validate
# run: |
# ./scripts/tfvalidate.sh
- name: cache conftest CLI
id: conftest-cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
with:
path: /usr/local/bin/conftest
key: conftest-${{ env.CONFTEST_VERSION }}
- name: install conftest if the cache doesn't exist
if: steps.conftest-cache.outputs.cache-hit != 'true'
run: |
ARCH=$(arch)
SYSTEM=$(uname)
wget "https://github.com/open-policy-agent/conftest/releases/download/v${CONFTEST_VERSION}/conftest_${{ env.CONFTEST_VERSION }}_${SYSTEM}_${ARCH}.tar.gz"
tar xzf conftest_${{ env.CONFTEST_VERSION }}_${SYSTEM}_${ARCH}.tar.gz
sudo mv conftest /usr/local/bin
# Conftestによるポリシーテストを実行
- name: run conftest
run: |
./scripts/conftest.sh
# test-prep:
# needs: validation
# runs-on: ubuntu-22.04
# # gcloudの認証で使用するWorkload Identity Providerで必要なパーミッション
# permissions:
# id-token: write
# contents: read
# steps:
# - name: checkout project
# uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# # terraform CLIのインストールおよびgcloudの認証を実行
# - name: setup terraform and gcloud CLI
# uses: ./.github/actions/setup
# with:
# workload_identity_provider: ${{ secrets.TEST_WORKLOAD_IDENTITY_PROVIDER }}
# service_account: ${{ secrets.TEST_SERVICE_ACCOUNT }}
# # テストコードの依存関係解決に使われるtier1ディレクトリのHCLをプロビジョニング
# - name: run apply command to tier1 in test
# run: |
# ./terraform/environments/test/tier1/apply.sh
# env:
# TF_VAR_service: ${{ secrets.TEST_SERVICE }}
# TF_VAR_env: test
# test:
# needs: test-prep
# runs-on: ubuntu-22.04
# permissions:
# id-token: write
# contents: read
# # テストコードはジョブレベルで並列実行
# # 各ジョブで実行されるモジュールのリストはtest_matrix.yamlにて手動で設定している
# # (テスト実行時間をジョブ間で均等に振り分けるため)
# strategy:
# fail-fast: false
# matrix:
# shard: [modules1, modules2]
# steps:
# - name: checkout project
# uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# - name: setup terraform and gcloud CLI
# uses: ./.github/actions/setup
# with:
# workload_identity_provider: ${{ secrets.TEST_WORKLOAD_IDENTITY_PROVIDER }}
# service_account: ${{ secrets.TEST_SERVICE_ACCOUNT }}
# # 各ジョブが担当するモジュールのテストコードを実行
# - name: run tests
# run: |
# while IFS= read MODULE; do
# ./terraform/modules/$MODULE/test.sh
# done <<< "$(yq -r .${{ matrix.shard }}[] ./.github/data/test_matrix.yaml)"
# env:
# TF_VAR_service: ${{ secrets.TEST_SERVICE }}
# TF_VAR_env: test
# plan:
# needs: test
# runs-on: ubuntu-22.04
# # 'pull-requests: write'はアクションからPRにコメントを書き込む際に必要
# permissions:
# id-token: write
# contents: read
# pull-requests: write
# steps:
# - name: checkout project
# uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# - name: setup terraform and gcloud CLI
# uses: ./.github/actions/setup
# with:
# workload_identity_provider: ${{ secrets.STG_WORKLOAD_IDENTITY_PROVIDER }}
# service_account: ${{ secrets.STG_SERVICE_ACCOUNT }}
# # staging環境に対してterraform planコマンドを実行して、実行結果サマリをPRコメントに出力
# - name: run terraform plan in stg
# uses: ./.github/actions/tfcmd
# with:
# service: ${{ secrets.STG_SERVICE }}
# env: stg
# command: plan
# gh_token: ${{ secrets.GITHUB_TOKEN }}
# slack:
# if: always()
# runs-on: ubuntu-22.04
# needs: plan
# permissions:
# contents: read
# steps:
# - name: checkout project
# uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# # ワークフローの実行結果をSlackの指定したチャネルに通知
# - name: send the workflow result to the slack channel
# uses: ./.github/actions/slack
# with:
# conclusion: ${{ needs.plan.result }}
# channel_id: ${{ vars.SLACK_CHANNEL_ID }}
# webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}