Nov 23, 20265 min read

Deploy to Google Cloud from GitHub Actions with Workload Identity Federation

How to replace service account JSON keys in GitHub secrets with a workload identity pool, an OIDC provider that checks the repository, and a service account the workflow impersonates.

Illustration for Deploy to Google Cloud from GitHub Actions with Workload Identity Federation

A service account JSON key in a GitHub secret is a password that never expires, and it can be copied out of a log or a compromised action. Workload Identity Federation removes it: GitHub signs a token for every run, Google exchanges it for short-lived credentials, and nothing long-lived is stored.

The setup has four parts: a pool, a provider that trusts GitHub, an IAM binding to a service account, and the workflow.

What do you need to configure?

PieceValueWhy it matters
Workload identity poolOne pool, for example githubGroups external identities
OIDC providerIssuer https://token.actions.githubusercontent.comTells Google to trust GitHub tokens
Attribute conditionMatch your repository or ownerBlocks other GitHub repositories
Service account bindingroles/iam.workloadIdentityUser for the repo's principalLets the workflow impersonate
Workflowid-token: write and the auth actionRequests the token and exchanges it

How do you create the pool and provider?

Google documents federation for deployment pipelines, including GitHub Actions. With gcloud:

~
$ gcloud iam workload-identity-pools create github --location=global \ --display-name="GitHub Actions"$ gcloud iam workload-identity-pools providers create-oidc github-oidc \ --location=global --workload-identity-pool=github \ --issuer-uri="https://token.actions.githubusercontent.com" \ --attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repository,attribute.repository_id=assertion.repository_id,attribute.ref=assertion.ref" \ --attribute-condition="assertion.repository_owner_id == '1234567' && assertion.ref == 'refs/heads/main'"

The attribute condition is what keeps other GitHub accounts out of the provider, and the ref check keeps branch and pull request workflows from getting deploy credentials. Google recommends the numeric repository_owner_id and repository_id claims over names. Names can be reclaimed by someone else if you delete a repository or organization, while the numeric IDs cannot.

How do you let the repository use a service account?

Grant the pool's identity for your repository the right to impersonate the deploy service account, using the project number rather than the project ID in the member string:

~
$ gcloud iam service-accounts add-iam-policy-binding deploy@my-project.iam.gserviceaccount.com \ --role="roles/iam.workloadIdentityUser" \ --member="principalSet://iam.googleapis.com/projects/123456789/locations/global/workloadIdentityPools/github/attribute.repository_id/987654321"

The service account then carries the deploy permissions, for example Artifact Registry writer and Cloud Run developer, and nothing more. Review it like any other IAM binding because it defines what a compromised workflow could do.

What does the workflow look like?

name: deploy on: push: branches: [main] permissions: id-token: write # lets the job request the OIDC token contents: read jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - id: auth uses: google-github-actions/auth@v3 with: workload_identity_provider: projects/123456789/locations/global/workloadIdentityPools/github/providers/github-oidc service_account: deploy@my-project.iam.gserviceaccount.com - uses: google-github-actions/setup-gcloud@v3 - run: gcloud run services list --region europe-west1

The auth action handles the token exchange and writes credentials that gcloud and the client libraries pick up. If the provider or service account value is wrong, the step fails before any other command runs. Pin each action to a full commit SHA rather than a tag, since every step after the auth step runs with the deploy credentials.

How do you debug the usual errors?

The error The given credential is rejected by the attribute condition means the token's claims did not satisfy the provider's condition, so print the claims and compare them with the condition. A failure to impersonate the service account means the binding is missing, uses the project ID instead of the project number, or names a different attribute than the one you mapped. A job that fails to get a token at all is missing id-token: write. Pool and provider changes can also take a few minutes to propagate. The federation overview lists the supported providers and the claims they provide.

How does Encore avoid keys in GitHub?

Encore

Encore brings infrastructure from code, and for GCP the own cloud docs describe the recommended setup: you add an IAM binding in your own organization or project for the Google-managed service account the Encore platform provides for each app, and you choose its roles, down to a single project. You share no long-lived key with Encore, and access ends when you remove the binding. Link the repository through the GitHub integration, and a push to the configured branch deploys to your project.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.
Encore provisions the infrastructure your application needs in your own AWS or GCP account.
Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create media-api --example=hello-world$ cd media-api$ git push encore
// media/media.go package media import ( "encore.dev/storage/objects" "encore.dev/storage/sqldb" ) // Declared in code. A deploy provisions the Cloud Storage bucket and the // Cloud SQL database in the connected project. var Uploads = objects.NewBucket("uploads", objects.BucketConfig{}) var db = sqldb.NewDatabase("media", sqldb.DatabaseConfig{ Migrations: "./migrations", })

The workflow has no federation setup because the platform deploys the code, and the Cloud Storage buckets and databases it declares are created in your project as the GCP infrastructure docs describe. A coding agent adding a bucket edits the Go file and runs it locally, with no service account or IAM binding to write separately.

Best fit: Teams that deploy to their own Google Cloud project and want to avoid maintaining a pool, a provider and bindings for CI.

Frequently asked questions

How does Workload Identity Federation work with GitHub Actions?

The workflow gets an OIDC token from GitHub, Google's security token service exchanges it for a short-lived federated token according to the pool and provider settings, and the workflow can then impersonate a service account. No service account JSON key is stored in GitHub.

Why do you need an attribute condition?

Without one, any GitHub repository could present a token to the provider. A condition on the repository or, better, the repository ID restricts the provider to your repositories, and the principalSet in the IAM binding narrows access further.

What permissions does the service account binding need?

The identity from the pool needs roles/iam.workloadIdentityUser on the target service account. The service account itself holds the deploy roles, such as permission to push images and update a Cloud Run service.

How does Encore deploy to Google Cloud without keys in GitHub?

With Encore, you add an IAM binding in your own GCP organization or project that grants the roles you choose to the service account the Encore platform provides for your app, and link the GitHub repository. Removing the binding ends access. A push to the configured branch deploys, and the workflow stores no cloud keys.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders