セクション G: GitHub Actions ワークフローを整備#7
Open
kantacky wants to merge 12 commits into
Open
Conversation
way が提供する backend-test.yml を pull_request / push to main で 再利用するエントリポイントを置く (Notion 計画 §7-G)。 academic-api / batch-jobs と同じ手順でテストが回り、way 側で mise install + task test が走る。受け皿だった .github/workflows/.gitkeep は不要になったため削除する。
migrations/ または internal/shared/model/ または atlas.hcl が 触られた PR に対し、Notion 計画 §3 / §5 で確定した `atlas migrate lint --env local --latest 1` を必須実行する。 - mise-action でツールチェーンを揃える - atlas-provider-gorm を go run で呼ぶため go mod download を先に行う - service docker:dind は dev DB (docker://postgres/16/dev) 起動用
infra/ または .github/workflows/tf-plan.yml が触られた PR で `terraform plan -var-file=envs/stg.tfvars` を実行し (Notion 計画 §5)、 差分を actions/github-script から PR コメントに貼り付ける。 - Workload Identity 認証で GCP に繋ぐ - terraform fmt -check と terraform validate を先に流す - exit_code 0 / 2 (no change / changes detected) は成功扱い、 それ以外は明示的に fail させる
Notion 計画 §5 に沿って set-env → build-and-push → terraform-apply → migrate の 4 ステージ構成で、単一イメージ・マルチバイナリの モノレポを Cloud Run Service / Job に展開する。 - set-env: workflow_dispatch の入力 environment_name または stg を採用し、 単一イメージ URI と image_tag (commit SHA) を出力 - build-and-push: Dockerfile 1 本のみ build & push (academic-api / *-job / migrate-job の 4 バイナリを同梱) - terraform-apply: mise install で Terraform を揃え、environment 承認 ゲートを通過してから -var-file=envs/<env>.tfvars で apply - migrate: prod は migrate-job、それ以外は migrate-job-<env> を gcloud run jobs execute --wait で同期実行
There was a problem hiding this comment.
Pull request overview
本PRは、サーバーリポジトリに GitHub Actions ワークフローを追加し、テスト・DBマイグレーション lint・Terraform plan・デプロイを CI/CD として整備するものです(way の reusable workflow 再利用+サーバー固有の Terraform/Atlas/Cloud Run Jobs 実行を追加)。
Changes:
pull_requestでの Terraform plan(fmt/validate/plan と PR コメント投稿)を追加- マイグレーション関連変更時の
atlas migrate lintを必須化 push to main/workflow_dispatchによるビルド→Terraform apply→マイグレーション実行のデプロイパイプラインを追加
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
.github/workflows/test.yml |
way の reusable workflow を呼び出すテスト実行用ワークフローを追加 |
.github/workflows/migrate-lint.yml |
Atlas migrate lint を PR で必須実行するワークフローを追加 |
.github/workflows/tf-plan.yml |
PR で Terraform plan を実行し、結果を PR コメントするワークフローを追加 |
.github/workflows/deploy.yml |
main push / 手動実行のデプロイ(build/push→apply→migrate)を追加 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- tf-plan: github-script への plan 出力直挿しを env+fs 経由に変えて script injection を防ぐ - deploy/tf-plan: terraform init に -backend-config を渡し、partial configuration な backend を正しく解決する - deploy: 各 terraform 実行を plan -out → apply <plan> の二段にして、 確定した変更だけが当たるようにする - deploy: -target=google_cloud_run_v2_job.jobs で Cloud Run Jobs を 先行 apply → migrate 実行 → 残りを apply に並べ替え、新コードに対する migration を Cloud Run Service 切替より前に走らせる - 全ジョブに timeout-minutes を設定 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- 不要な submodule checkout を全 workflow から削除 (way/ は CI 不要かつ SSH URL で認証失敗の懸念) - tf-plan の terraform plan に -detailed-exitcode を追加し exit_code 判定を実挙動と整合 - migrate-lint の未使用 docker:dind service を削除 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- migrate-lint: --env local → --env gorm (atlas.hcl に local env が存在せず失敗するため) - deploy: set-env に environment: を付与し env-scoped Variables を解決 - deploy: set-env の shell を env: 経由に変更し injection リスクを排除 - tf-plan: PR plan コメントを marker で upsert (毎 push 累積を回避) - tf-plan: tail -c → tail -n でマルチバイト切断を回避し details で折り畳み - tf-plan: -var=image_tag を削除し default の "latest" で plan ノイズを抑制 - tf-plan: fork PR ガードを追加 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Comment on lines
+68
to
+78
| const body = [ | ||
| marker, | ||
| "### Terraform Plan (envs/stg.tfvars)", | ||
| "", | ||
| "<details><summary>plan output (tail)</summary>", | ||
| "", | ||
| "```", | ||
| summary, | ||
| "```", | ||
| "", | ||
| "</details>", |
Comment on lines
+26
to
+39
| - uses: actions/checkout@v6 | ||
| with: | ||
| fetch-depth: 0 | ||
| - uses: jdx/mise-action@v3 | ||
| - uses: google-github-actions/auth@v3 | ||
| with: | ||
| workload_identity_provider: ${{ vars.WORKLOAD_IDENTITY_PROVIDER }} | ||
| service_account: ${{ vars.SERVICE_ACCOUNT }} | ||
| - name: Terraform init | ||
| run: terraform init -input=false -backend-config=envs/stg.backend.hcl | ||
| - name: Terraform fmt check | ||
| run: terraform fmt -check -recursive | ||
| - name: Terraform validate | ||
| run: terraform validate |
| -no-color \ | ||
| -var-file=envs/${{ needs.set-env.outputs.environment }}.tfvars \ | ||
| -var=image_tag=${{ needs.set-env.outputs.image_tag }} \ | ||
| -out=tfplan.binary |
- migrate-lint: atlas 1.2.0 では migrate lint が Pro 限定で常時失敗するため削除 - test: way の reusable workflow から self-contained 化し、task test を mise run test に変更 (本リポジトリでは Taskfile を使わず mise tasks 運用のため) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Comment on lines
+114
to
+124
| - name: Terraform plan (Cloud Run Jobs only) | ||
| run: | | ||
| terraform plan \ | ||
| -input=false \ | ||
| -no-color \ | ||
| -target='google_cloud_run_v2_job.jobs' \ | ||
| -var-file=envs/${{ needs.set-env.outputs.environment }}.tfvars \ | ||
| -var=image_tag=${{ needs.set-env.outputs.image_tag }} \ | ||
| -out=tfplan-jobs.binary | ||
| - name: Terraform apply (Cloud Run Jobs only) | ||
| run: terraform apply -input=false -auto-approve tfplan-jobs.binary |
| cancel-in-progress: true | ||
| jobs: | ||
| test: | ||
| if: github.event.pull_request.draft == false |
Comment on lines
+62
to
+86
| build-and-push: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 20 | ||
| permissions: | ||
| contents: read | ||
| id-token: write | ||
| needs: | ||
| - set-env | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
| with: | ||
| fetch-depth: 0 | ||
| - uses: google-github-actions/auth@v3 | ||
| with: | ||
| workload_identity_provider: ${{ vars.WORKLOAD_IDENTITY_PROVIDER }} | ||
| service_account: ${{ vars.SERVICE_ACCOUNT }} | ||
| - uses: google-github-actions/setup-gcloud@v3 | ||
| with: | ||
| project_id: ${{ vars.PROJECT_ID }} | ||
| - name: Configure Docker | ||
| run: gcloud auth configure-docker ${{ vars._AR_HOSTNAME }} | ||
| - name: Build server image (multi-binary) | ||
| run: docker build -t ${{ needs.set-env.outputs.image }} -f Dockerfile . | ||
| - name: Push server image | ||
| run: docker push ${{ needs.set-env.outputs.image }} |
| -no-color \ | ||
| -detailed-exitcode \ | ||
| -var-file=envs/stg.tfvars \ | ||
| -out=tfplan.binary > tfplan.txt |
| steps: | ||
| - uses: actions/checkout@v6 | ||
| with: | ||
| fetch-depth: 0 |
| -no-color \ | ||
| -var-file=envs/${{ needs.set-env.outputs.environment }}.tfvars \ | ||
| -var=image_tag=${{ needs.set-env.outputs.image_tag }} \ | ||
| -out=tfplan.binary |
| - prod | ||
| concurrency: | ||
| group: ${{ github.repository }}-${{ github.workflow }}-${{ github.event.inputs.environment_name || 'dev' }} | ||
| cancel-in-progress: false |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
やったこと
Notion 計画 §7-G の GitHub Actions 整備分。way の汎用ワークフローを使わずに、サーバー側 Terraform / テストに必要な PR チェックとデプロイ自動化を一式追加する。
.github/workflows/test.yml: 本リポジトリ内でmise run testをpull_request/push to mainで実行 (paths フィルタは required check 想定で意図的に未設定).github/workflows/tf-plan.yml:infra/または本ワークフロー自身が触られた PR でterraform fmt -check/terraform validate/terraform plan -var-file=envs/stg.tfvarsを実行し、差分をactions/github-scriptから PR コメントに upsert-detailed-exitcodeで exit code 0 (no change) と 2 (changes detected) を成功扱い、それ以外は明示的に failhead.repo.full_name == github.repositoryでガードimage_tagを渡さずvariables.tfの default (latest) を採用し、commit 毎の image_tag 差分ノイズを抑制.github/workflows/deploy.yml:push to mainまたはworkflow_dispatchでset-env→build-and-push→terraform-apply-jobs→migrate→terraform-applyの 5 ステージset-env: 入力environment_nameまたはdev(push to main 時のデフォルト) を採用し、単一イメージ URI とimage_tag(commit SHA) を出力。Environment スコープのvars._AR_HOSTNAME/vars.PROJECT_IDを解決するためenvironment:を指定build-and-push: Dockerfile 1 本のみ build & push (academic-api / *-job / migrate-job の 4 バイナリを単一イメージに同梱)。Environment スコープ変数解決のためenvironment:指定terraform-apply-jobs:-target='google_cloud_run_v2_job.jobs["migrate-job"]'で migrate-job のみを新イメージに先行更新 (他 Job の scheduled 実行が新コードで先走るのを防ぐ)migrate: prod はmigrate-job、それ以外はmigrate-job-<env>をgcloud run jobs execute --waitで同期実行 (失敗時はワークフロー失敗)terraform-apply: migration 成功後に Service / Scheduler / 他 Job を含む残差分を apply確認したこと
deploy.ymlの image URI が Terraformlocal.imageと一致 (<host>/<project>/server/server:<sha>)メモ
PROJECT_ID/_AR_HOSTNAME/_DEPLOY_REGION/WORKLOAD_IDENTITY_PROVIDER/SERVICE_ACCOUNT/INSTANCE_CONNECTION_NAME/DB_NAME/DB_IAM_USER/SECRET_PROJECT_IDbackend-terraform-deploy.yml) への切り出しは Phase 6 で実施 (本 PR のスコープ外)feat/f-terraformを経由して main にマージ