Skip to content

OIDC with Google Cloud

Workload Identity Federation lets a job act as a Google Cloud service account without a stored key file. Each run asks GitHub for a signed token, and Google swaps it for short-lived credentials. The token flow is the standard GitHub Actions one, so the workflow below is the usual one with a compiler.dev runs-on label.

1. Create a workload identity pool and a GitHub provider. Issuer https://token.actions.githubusercontent.com. Map claims to attributes and add a condition so only your repository is accepted:

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. Let that repository use a service account. Grant roles/iam.workloadIdentityUser on the service account to the principal set for the repository:

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

3. Use it in your workflow. The job needs 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