Skip to content

セクション G: GitHub Actions ワークフローを整備#7

Open
kantacky wants to merge 12 commits into
mainfrom
feat/g-actions
Open

セクション G: GitHub Actions ワークフローを整備#7
kantacky wants to merge 12 commits into
mainfrom
feat/g-actions

Conversation

@kantacky

@kantacky kantacky commented May 9, 2026

Copy link
Copy Markdown
Member

やったこと

Notion 計画 §7-G の GitHub Actions 整備分。way の汎用ワークフローを使わずに、サーバー側 Terraform / テストに必要な PR チェックとデプロイ自動化を一式追加する。

  • テスト
    • .github/workflows/test.yml: 本リポジトリ内で mise run testpull_request / push to main で実行 (paths フィルタは required check 想定で意図的に未設定)
    • PR 番号ベースの concurrency で連続 push 時の旧 run をキャンセル
  • Terraform plan
    • .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) を成功扱い、それ以外は明示的に fail
    • fork PR からのコメント投稿失敗を防ぐため head.repo.full_name == github.repository でガード
    • PR plan は image_tag を渡さず variables.tf の default (latest) を採用し、commit 毎の image_tag 差分ノイズを抑制
  • デプロイ
    • .github/workflows/deploy.yml: push to main または workflow_dispatchset-envbuild-and-pushterraform-apply-jobsmigrateterraform-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

確認したこと

  • 3 ファイルとも YAML 構文が壊れていないことをローカルで目視確認
  • deploy.yml の image URI が Terraform local.image と一致 (<host>/<project>/server/server:<sha>)
  • Copilot レビュー指摘 (submodules / shell injection / PR コメント upsert / Environment スコープ解決 / -target の絞り込み 等) を順次反映

メモ

  • 必要な GitHub Variables: PROJECT_ID / _AR_HOSTNAME / _DEPLOY_REGION / WORKLOAD_IDENTITY_PROVIDER / SERVICE_ACCOUNT / INSTANCE_CONNECTION_NAME / DB_NAME / DB_IAM_USER / SECRET_PROJECT_ID
  • 必要な GitHub Secrets: 現状なし (IAM 認証のみ)。Phase 3 の auth 系で追加予定
  • way 側の reusable workflow (backend-terraform-deploy.yml) への切り出しは Phase 6 で実施 (本 PR のスコープ外)
  • migrate-lint は当初追加予定だったが、本 PR では削除 (Phase 後続で再検討)
  • migration 失敗時の手動復旧手順 (Job 側を直前 image_tag に巻き戻す) は別途運用ドキュメント化予定
  • ベースブランチは §F スタックの feat/f-terraform を経由して main にマージ

kantacky added 4 commits May 9, 2026 13:16
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 で同期実行
@kantacky kantacky self-assigned this May 9, 2026
@kantacky kantacky marked this pull request as ready for review May 9, 2026 13:23
Copilot AI review requested due to automatic review settings May 9, 2026 13:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread .github/workflows/tf-plan.yml Outdated
Comment thread .github/workflows/tf-plan.yml
Comment thread .github/workflows/migrate-lint.yml Outdated
Comment thread .github/workflows/migrate-lint.yml Outdated
Comment thread .github/workflows/deploy.yml
Comment thread .github/workflows/deploy.yml Outdated
Base automatically changed from feat/f-terraform to main May 17, 2026 05:56
masaya-osuga and others added 3 commits May 17, 2026 14:56
- 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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 13 comments.

Comment thread .github/workflows/migrate-lint.yml Outdated
Comment thread .github/workflows/migrate-lint.yml Outdated
Comment thread .github/workflows/deploy.yml
Comment thread .github/workflows/deploy.yml
Comment thread .github/workflows/tf-plan.yml
Comment thread .github/workflows/deploy.yml
Comment thread .github/workflows/deploy.yml Outdated
Comment thread .github/workflows/tf-plan.yml
Comment thread .github/workflows/test.yml
Comment thread .github/workflows/test.yml Outdated
masaya-osuga and others added 2 commits May 17, 2026 15:18
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 9 comments.

Comment thread .github/workflows/deploy.yml
Comment thread .github/workflows/deploy.yml
Comment thread .github/workflows/migrate-lint.yml Outdated
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
Comment thread .github/workflows/deploy.yml
-no-color \
-var-file=envs/${{ needs.set-env.outputs.environment }}.tfvars \
-var=image_tag=${{ needs.set-env.outputs.image_tag }} \
-out=tfplan.binary
Comment thread .github/workflows/test.yml
Comment thread .github/workflows/deploy.yml
- 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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated 7 comments.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants