Trivy導入 #366
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
# v0.50.3 ref. https://github.com/terraform-linters/tflint/releases/tag/v0.50.3 | |
TFLINT_VERSION: e34bda7f398c139d7bc528261ee96998c10e79c8 | |
OPA_VERSION: 0.64.1 | |
CONFTEST_VERSION: 0.51.0 | |
REGAL_VERSION: 0.21.3 | |
TRIVY_VERSION: 0.51.1 | |
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 | |
# opa CLIをインストール | |
- name: install opa CLI | |
# v2.2.0 ref. https://github.com/open-policy-agent/setup-opa/releases/tag/v2.2.0 | |
uses: open-policy-agent/setup-opa@34a30e8a924d1b03ce2cf7abe97250bbb1f332b5 | |
with: | |
version: ${{ env.OPA_VERSION }} | |
# Regoファイルのフォーマットをチェック | |
- name: run opa fmt | |
run: | | |
./scripts/opafmt.sh | |
# regal CLIをインストール | |
- name: install regal CLI | |
# v1.0.0 ref. https://github.com/StyraInc/setup-regal/releases/tag/v1.0.0 | |
uses: StyraInc/setup-regal@33a142b1189004e0f14bf42b15972c67eecce776 | |
with: | |
version: ${{ env.REGAL_VERSION }} | |
# Regoファイルに対してリンタを実行 | |
- name: run regal lint | |
run: | | |
./scripts/regal.sh | |
# 既にインストール済みのConftestがキャッシュに存在する場合restore | |
- name: cache conftest CLI | |
id: conftest-cache | |
# v4.0.2 ref. https://github.com/actions/cache/releases/tag/v4.0.2 | |
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 | |
with: | |
path: /usr/local/bin/conftest | |
key: conftest-${{ env.CONFTEST_VERSION }} | |
# キャッシュにConftestが存在しない場合インストール | |
- name: install conftest if the cache doesn't exist | |
if: steps.conftest-cache.outputs.cache-hit != 'true' | |
run: | | |
CONFTEST_FILE=conftest_${{ env.CONFTEST_VERSION }}_$(uname)_$(arch).tar.gz | |
wget "https://github.com/open-policy-agent/conftest/releases/download/v${CONFTEST_VERSION}/${CONFTEST_FILE}" | |
tar xzf ${CONFTEST_FILE} | |
sudo mv conftest /usr/local/bin | |
# Conftestを使用してプロジェクト内の全設定ファイルの定義がポリシーに適合しているかチェック | |
- name: run conftest | |
run: | | |
./scripts/conftest.sh | |
# 既にインストール済みのTFLintがキャッシュに存在する場合restore | |
- name: cache tflint CLI | |
id: tflint-cache | |
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 | |
with: | |
path: | | |
/usr/local/bin/tflint | |
~/.tflint.d/ | |
key: tflint-${{ env.TFLINT_VERSION }}-${{ hashFiles('terraform/.tflint.hcl') }} | |
# キャッシュにTFLintが存在しない場合インストール | |
- name: install tflint if the cache doesn't exist | |
if: steps.tflint-cache.outputs.cache-hit != 'true' | |
run: | | |
# このスクリプトが万が一にもGCPにアクセス出来ないようvalidationとtest-prepでジョブを分けている | |
curl -s https://raw.githubusercontent.com/terraform-linters/tflint/${{ env.TFLINT_VERSION }}/install_linux.sh | sudo bash | |
cd terraform && tflint --init | |
# environmentsやmodules配下のHCLファイルが格納されているディレクトリ全てでtflintコマンドを実行 | |
- name: run tflint | |
run: | | |
./scripts/tflint.sh | |
# 既にインストール済みのTtrivyがキャッシュに存在する場合restore | |
- name: cache trivy CLI | |
id: trivy-cache | |
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 | |
with: | |
path: | | |
/usr/local/bin/trivy | |
key: trivy-${{ env.TRIVY_VERSION }} | |
# キャッシュにTrivyが存在しない場合インストール | |
- name: install trivy if the cache doesn't exist | |
run: | | |
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin v${{ env.TRIVY_VERSION }} | |
# Trivyによるセキュリティスキャンを実行 | |
- name: run trivy | |
run: | | |
./scripts/trivy.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 | |
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 }} |