★★ 中級

05. GCP でチーム開発(Cloud Build / Cloud Deploy)

GCP は Cloud Build をハブにした CI/CD が定番。GKE / Cloud Run / Functions などのコンテナ系サービスへのデプロイと相性がよく、Workload Identity Federation で GitHub Actions とも安全に連携できます。

GCP の CI/CD サービス全体図

GCP の CI/CD:Cloud Source Repos / Cloud Build / Artifact Registry / Cloud Deploy
コード保管は GitHub、ビルドは Cloud Build、成果物は Artifact Registry、デプロイは Cloud Deploy。WIF で GitHub Actions からキーレス連携。

Cloud Source Repositories

Cloud Source Repositories (CSR) は GCP 内の Git ホスティング。GitHub と双方向ミラーできます。

Cloud Build(CI/CD)

Cloud Build は GCP のマネージドビルドサービス。cloudbuild.yaml に手順を書き、トリガで起動します。

# cloudbuild.yaml
steps:
  # Node.js 依存解決とテスト
  - name: 'gcr.io/cloud-builders/npm'
    args: ['ci']
  - name: 'gcr.io/cloud-builders/npm'
    args: ['test']

  # Docker イメージビルド
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - 'build'
      - '-t'
      - 'asia-northeast1-docker.pkg.dev/$PROJECT_ID/myrepo/app:$SHORT_SHA'
      - '.'

  # Artifact Registry に push
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - 'push'
      - 'asia-northeast1-docker.pkg.dev/$PROJECT_ID/myrepo/app:$SHORT_SHA'

  # Cloud Run にデプロイ
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: 'gcloud'
    args:
      - 'run'
      - 'deploy'
      - 'myapp'
      - '--image=asia-northeast1-docker.pkg.dev/$PROJECT_ID/myrepo/app:$SHORT_SHA'
      - '--region=asia-northeast1'

images:
  - 'asia-northeast1-docker.pkg.dev/$PROJECT_ID/myrepo/app:$SHORT_SHA'

GitHub / Cloud Source Repos / Bitbucket のいずれのリポジトリでも、push や PR をトリガにできます。

Artifact Registry

Artifact Registry は Docker イメージ / Maven / npm / Python / Apt / Yum 等を一元管理する次世代のレジストリ。旧 Container Registry の後継です。

Cloud Deploy

Cloud Deploy は GKE / Cloud Run へのプログレッシブデリバリーをマネージドで提供。dev → stg → prod の昇格を「リリースパイプライン」として宣言します。

# clouddeploy.yaml
apiVersion: deploy.cloud.google.com/v1
kind: DeliveryPipeline
metadata:
  name: myapp-pipeline
description: dev → stg → prod
serialPipeline:
  stages:
    - targetId: dev
      profiles: [dev]
    - targetId: stg
      profiles: [stg]
    - targetId: prod
      profiles: [prod]
      strategy:
        canary:
          runtimeConfig:
            cloudRun:
              automaticTrafficControl: true
          canaryDeployment:
            percentages: [10, 50]
            verify: true
---
apiVersion: deploy.cloud.google.com/v1
kind: Target
metadata:
  name: prod
description: production environment
requireApproval: true   # 手動承認必須
run:
  location: projects/PROJECT_ID/locations/asia-northeast1

Workload Identity Federation(WIF)

GitHub Actions から GCP リソースを操作するなら、サービスアカウントキーは作らず、Workload Identity Federation 一択です。

# Terraform で WIF を構築
resource "google_iam_workload_identity_pool" "github" {
  workload_identity_pool_id = "github-pool"
  display_name              = "GitHub Actions Pool"
}

resource "google_iam_workload_identity_pool_provider" "github" {
  workload_identity_pool_id          = google_iam_workload_identity_pool.github.workload_identity_pool_id
  workload_identity_pool_provider_id = "github-provider"
  display_name                       = "GitHub Actions"

  oidc {
    issuer_uri = "https://token.actions.githubusercontent.com"
  }

  attribute_mapping = {
    "google.subject"       = "assertion.sub"
    "attribute.repository" = "assertion.repository"
    "attribute.ref"        = "assertion.ref"
  }

  # 特定のリポジトリ + ブランチに限定
  attribute_condition = "attribute.repository == \"myorg/myapp\" && attribute.ref == \"refs/heads/main\""
}

# Actions が impersonate するサービスアカウント
resource "google_service_account" "deploy" {
  account_id   = "github-deploy"
  display_name = "GitHub Actions Deploy SA"
}

resource "google_service_account_iam_member" "wif_binding" {
  service_account_id = google_service_account.deploy.name
  role               = "roles/iam.workloadIdentityUser"
  member             = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.github.name}/attribute.repository/myorg/myapp"
}
# .github/workflows/deploy.yml の側
permissions:
  id-token: write
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: google-github-actions/auth@v2
        with:
          workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/github-pool/providers/github-provider'
          service_account: 'github-deploy@my-project.iam.gserviceaccount.com'
      - run: terraform apply -auto-approve

構成パターンの選び方

状況推奨構成
GitHub + GKE / Cloud RunGitHub Actions + WIF + Terraform でデプロイ
GCP 内で完結したいCloud Build トリガ + Artifact Registry + Cloud Deploy
段階的リリース(Canary)が必要Cloud Deploy(管理画面でロールアウト視覚化)
マルチクラウドGitHub Actions + Terraform(クラウド非依存)