コンテンツへスキップ

Google Cloud での OIDC

ワークロードID連携を使うと、キーファイルを保存せずにジョブがGoogle Cloudのサービスアカウントとして動作できます。実行のたびにGitHubが署名付きトークンを発行し、Googleがそれを短期間有効な認証情報に交換します。トークンの流れは標準のGitHub Actionsと同じなので、以下のワークフローはcompiler.devのruns-onラベルを指定した通常のものです。

1. ワークロードIDプールとGitHubプロバイダーを作成します。発行者はhttps://token.actions.githubusercontent.comです。クレームを属性にマッピングし、リポジトリだけを受け入れるよう条件を追加します:

gcloud iam workload-identity-pools providers create-oidc github \
  --project=MY_PROJECT --location=global --workload-identity-pool=github-pool \
  --issuer-uri="https://token.actions.githubusercontent.com" \
  --attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repository,attribute.ref=assertion.ref" \
  --attribute-condition="assertion.repository == 'ORG/REPO'"

2. そのリポジトリにサービスアカウントの使用を許可します。リポジトリのプリンシパルセットに、サービスアカウントに対するroles/iam.workloadIdentityUserを付与します:

principalSet://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/github-pool/attribute.repository/ORG/REPO

3. ワークフローで使用します。ジョブにはid-token: writeが必要です:

permissions:
  id-token: write
  contents: read

jobs:
  deploy:
    runs-on: compiler-8cpu
    steps:
      - uses: actions/checkout@v4
      - uses: google-github-actions/auth@v2
        with:
          workload_identity_provider: projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/github-pool/providers/github
          service_account: deploy@MY_PROJECT.iam.gserviceaccount.com
      - uses: google-github-actions/setup-gcloud@v2
      - run: gcloud auth list