経緯
前回は、GitHub Actionsを使用してPR作成を自動化しましたが、terraformのplanはまだ手動実行だったので、PR実行時にterraform planの結果を自動追記したいと内容を思い実装しました。
前回の記事はこちら
https://zenn.dev/ho377/articles/2026-03-13-tech
目的
- PRの作成時にteraform planの出力をコメント追記することで、planの内容を見てレビューすることができるようになる
実装内容
前提環境
- GitHubのリポジトリがある
- GitHubのリポジトリの設定で、Workflow permissionsの「Read and write permissions」と「Allow GitHub Actions to create and approve pull requests」が有効になっている
- personal access token(PAT)がリポジトリに設定済
- AWSでOIDCの設定済
- PRがGitHub Actionで自動化されていること
成果物
- .github/workflows/auto-pr.yml(前回作成ファイル)の修正
- envを修正
- env: GH_TOKEN: ${{ secrets.MY_PAT }}にします。
MY_PATはPATの名前なのでご自身の環境に合わせて変更してください。
修正後のauto-pr.yml
name: Auto PR on Push
on:
push:
branches:
- develop
jobs:
create-pull-request:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get commit message
id: commit
run: echo "msg=$(git log -1 --pretty=%s)" >> $GITHUB_OUTPUT
- name: Create Pull Request
run: |
gh pr create \
--base main \
--head ${{ github.ref_name }} \
--title "${{ steps.commit.outputs.msg }}" \
--body-file .github/pull_request_template.md \
--label "automated-pr" || echo "PR already exists."
env:
GH_TOKEN: ${{ secrets.MY_PAT }}
- .github/workflows/plan.ymlの作成
PRのコメントにterraform planを追記するファイルです。
github/workflows/plan.yml
name: TerraformPlan
on:
pull_request:
branches:
- main
env:
TF_VERSION: 1.14.6
AWS_DEFAULT_REGION: ap-northeast-1
AWS_ROLE_ARN: xxxxxxxxxxxx #作成済みのAWSでOIDCロールのarnを入力
permissions:
id-token: write
contents: read
actions: read
pull-requests: write
jobs:
Terraform_plan_and_Comment:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: ./environments/dev
steps:
# リポジトリのチェックアウトをする。
- name: Check out repository code
uses: actions/checkout@v3
# OICDでAssumeRoleをする。
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ env.AWS_DEFAULT_REGION }}
role-to-assume: ${{ env.AWS_ROLE_ARN }}
- name: Setup Terraform
# バージョン2を使用する。
uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Exec Terraform fmt check
id: fmt
run: terraform fmt -check -recursive
# exit code 3でエラーになり終了してしまうため
# continue-on-error: true で後続の処理も続ける。
continue-on-error: true
- name: Exec Terraform init
id: init
run: terraform init
- name: Validate
run: terraform validate
- name: Exec Terraform plan
id: plan
run: terraform plan -no-color
# terraform plan の結果をコメント欄に出力する。
- name : comment
uses: actions/github-script@v6
env:
# ここのstdoutでterraform planの結果をPLANに保存している。
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
script: |
const output = `### terraform cicd
#### Terraform Format and Style \`${{ steps.fmt.outcome }}\`
#### Terraform Initialization \`${{ steps.init.outcome }}\`
#### Terraform Plan \`${{ steps.plan.outcome }}\`
#### Terraform Validation \`${{ steps.validate.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`${process.env.PLAN}\`\`\`
</details>`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
PATを使用していることで、以下のonが使えます。
:::note alert
on:pull_requestは、手動によるPR作成をトリガーとするので、PATを使用することで手動と認識させることができます。
{{secret.GITHUB_TOKEN}}のままだとBot扱いとなり、うまく動きません。
:::
on:
pull_request:
branches:
- main
以上です。
参考