Replace a JSON service account key in GitHub secrets with Workload Identity Federation: create the pool and provider, restrict it to your repository, grant a deploy service account, and deploy to Cloud Run.

A common way to deploy from GitHub Actions to Google Cloud has long been a service account JSON key pasted into a repository secret. It works, and it leaves a credential that never expires, works from any machine, and can be used by anyone who sees a leaked log line. Many organizations now block key creation outright with the iam.disableServiceAccountKeyCreation policy.
Workload Identity Federation replaces the key with GitHub's own OIDC token. Each workflow run gets a token signed by GitHub, Google checks it against the conditions you set, and hands back short-lived credentials. This guide sets it up for a Cloud Run deploy.
Google's Workload Identity Federation best practices cover the GitHub case directly, including why the attribute condition matters.
Run these once per project. The attribute condition restricts the pool to your repository and the production branch. GitHub issues tokens for every organization from the same issuer URL, so without a condition, any repository on GitHub could present a valid token.
Create a service account for deploys and bind it to that repository. The provider condition already requires main, and the binding repeats the repository so a looser condition cannot widen access.
The member path uses the project number, not the project ID. The last binding lets the deploy account attach the Cloud Run service's runtime identity, api-runtime, which is a separate service account per service. Add roles/artifactregistry.writer on the repository if the workflow pushes images.
The job needs id-token: write so GitHub mints an OIDC token, and the auth action exchanges it.
name: deploy
on:
push:
branches: [main]
permissions:
contents: read
id-token: write
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # v3.0.0
with:
workload_identity_provider: projects/123456789/locations/global/workloadIdentityPools/github/providers/my-repo
service_account: gh-deploy@my-project.iam.gserviceaccount.com
- uses: google-github-actions/setup-gcloud@aa5489c8933f4cc7a4f7d45035b3b1440c9c10db # v3.0.1
- run: |
gcloud run deploy api \
--image=us-docker.pkg.dev/my-project/app/api:${{ github.sha }} \
--region=us-central1 \
--service-account=api-runtime@my-project.iam.gserviceaccount.com
The provider above already requires refs/heads/main. For staging deploys from another branch, create a separate workload identity pool and bind a separate service account to it; do not add a looser provider to this pool, because the repository-only production binding would also match its tokens. Leaving out service_account switches the action to Direct Workload Identity Federation, where IAM roles are granted to the pool principal itself; that skips a resource but not every Google API accepts principal identities, so the service account path above is the safe default.
When it fails, the error message names the step. "Failed to generate Google Cloud federated token" means the attribute condition rejected the token; a permission error after that means the workloadIdentityUser binding or the deploy roles are missing.
Best fit: any workflow that deploys to GCP today with a JSON key. The setup is a few commands, and you delete the key afterwards.
Encore brings infrastructure from code, and its deploys do not run in your GitHub Actions at all. You link the app to GitHub, and a push to the configured branch triggers a build and deploy by the Encore Platform. On GCP, the recommended connection method is the Encore Platform's own Google-managed service account, which you grant access to your project or organization: no key is shared, and removing the IAM binding revokes access.
What gets deployed is described by the code. A service with a database and a topic needs no deploy script, Cloud Run flags or IAM bindings in the repository:
package api
import (
"context"
"encore.dev/pubsub"
"encore.dev/storage/sqldb"
)
var db = sqldb.NewDatabase("api", sqldb.DatabaseConfig{Migrations: "./migrations"})
type SignupEvent struct {
UserID string `json:"user_id"`
}
var Signups = pubsub.NewTopic[*SignupEvent]("signups", pubsub.TopicConfig{
DeliveryGuarantee: pubsub.AtLeastOnce,
})
type SignupParams struct {
Email string `json:"email"`
}
//encore:api public method=POST path=/signup
func Signup(ctx context.Context, p *SignupParams) error {
var id string
if err := db.QueryRow(ctx, `
INSERT INTO users (email) VALUES ($1) RETURNING id`, p.Email).Scan(&id); err != nil {
return err
}
_, err := Signups.Publish(ctx, &SignupEvent{UserID: id})
return err
}
On deploy, Encore runs the service on Cloud Run or GKE, provisions Cloud SQL for the database and GCP Pub/Sub for the topic, and provisions a service account for the service. Your GitHub Actions can keep running linters and encore test with no Google credentials in them.
For teams letting coding agents open pull requests, that removes the most dangerous secret from the repository's CI: an agent or a compromised dependency in a workflow has no GCP credential to steal.
Best fit: teams on GCP who want push-to-deploy into their own project with no cloud credentials stored in GitHub.
If GitHub Actions deploys to GCP, replace the JSON key with Workload Identity Federation today: one pool, one provider with an organization condition, one deploy service account bound to one repository. If you deploy with Encore, GitHub only needs the repository link, and the Encore Platform's own service account does the provisioning and deploys in your project.
Use Workload Identity Federation. Create a workload identity pool and an OIDC provider for https://token.actions.githubusercontent.com, restrict it with an attribute condition on your organization or repository, grant the pool roles/iam.workloadIdentityUser on a deploy service account, and use google-github-actions/auth with workload_identity_provider and service_account in a job that has id-token: write.
A JSON key is a long-lived credential that works from anywhere until someone deletes it. Anyone who can read it, including a compromised workflow or a leaked log, can act as the service account. Federation issues tokens that expire in minutes and only to workflows that match your conditions.
Yes. Google strongly recommends one. GitHub uses a single token issuer for every organization, so without a condition such as assertion.repository_owner == 'your-org', a workflow in someone else's repository could be admitted to your pool. Map the claim in the provider's attribute mapping before you use it in the condition.
No. With Encore, pushing to the linked GitHub repository triggers the deploy, and the Encore Platform provisions and deploys to your GCP project using its own Google-managed service account that you grant access to. No key is stored in GitHub, and you can revoke access by removing the IAM binding.
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.