CI/CD and automation · Intermediate · 18 min
Deploy to Google Cloud from GitHub Actions with Workload Identity Federation
By Padding · Updated 18 Nov 2026
GitHub Actions · google-github-actions/auth v3 · gcloud 594.0.1
You will replace a stored service account key with Workload Identity Federation, so that only one GitHub repository can borrow a narrow deploy identity for a few minutes and ship a Cloud Run service.
On this page
Most guides for deploying to Google Cloud from CI start the same way: create a service account, download its JSON key, paste the key into a GitHub secret. That key is a password that never expires. Anyone who gets it, through a leaked log, a compromised dependency in a workflow, or an old laptop with a copy, can act as that service account from anywhere until somebody notices and deletes it. Google's own advice is to avoid user-managed keys, and organisations created since May 2024 block key creation by default.
Workload Identity Federation removes the key. On every run, GitHub issues the job a signed OIDC token that says which repository, branch and workflow it came from. Google's Security Token Service checks that token against a provider you configure, and if your conditions match, hands back a short-lived Google access token. Nothing long-lived is stored anywhere.
In this tutorial you will build that chain for one repository and use it to deploy Google's sample container to Cloud Run as a private service called hello-wif:
- a workload identity pool with a GitHub provider that only admits tokens from your repository,
- a deploy service account that can deploy Cloud Run revisions and nothing else,
- an IAM binding that lets only your repository impersonate that account,
- a workflow that authenticates with
google-github-actions/auth@v3and deploys withgoogle-github-actions/deploy-cloudrun@v3.
This can cost money. The pool and the service accounts are free to create, but Cloud Run bills for requests and the resources they use, beyond a free allowance whose size changes. Check cloud.google.com/run/pricing before you start. The last step deletes everything this tutorial creates.
Step 1 · Find the keys you are replacing
Start by setting the values the rest of the tutorial uses, in the terminal where you will run every gcloud and gh command. Replace your-project-id and your-org/your-repo with your own. Pick a Cloud Run region close to your users: africa-south1 (Johannesburg) serves southern Africa, europe-west1 is a common choice for West Africa and Europe.
PROJECT_ID=your-project-id # replace this
REGION=europe-west1 # replace this if you want another region
GITHUB_REPO=your-org/your-repo # replace this
gcloud config set project "$PROJECT_ID"
PROJECT_NUMBER="$(gcloud projects describe "$PROJECT_ID" --format='value(projectNumber)')"
echo "$PROJECT_NUMBER"The project number is a long run of digits. Workload identity resources are addressed by number, not by project ID, so you will need it later.
If this repository already deploys with a key, find it. Look for a secret with a name like GCP_CREDENTIALS or GOOGLE_CREDENTIALS, and for a workflow step that passes credentials_json to the auth action:
gh secret list --repo "$GITHUB_REPO"
grep -rn "credentials_json" .github/workflows/Then list the user-managed keys on the service account that secret belongs to. Replace OLD_SA_EMAIL with that account's email address:
gcloud iam service-accounts keys list --iam-account=OLD_SA_EMAIL --managed-by=userCheck it worked
You have a number in PROJECT_NUMBER, and you have written down any key ID and secret name the old setup used. Do not delete them yet: the old pipeline keeps working until the new one has deployed once. If the repository never had a key, there is nothing to note and you start clean.
Step 2 · Create the pool and a GitHub provider
Enable the APIs that federation and the deploy use. sts.googleapis.com exchanges the GitHub token, and iamcredentials.googleapis.com turns the result into a service account token:
gcloud services enable iam.googleapis.com cloudresourcemanager.googleapis.com \
iamcredentials.googleapis.com sts.googleapis.com run.googleapis.comLook up your repository's numeric IDs. Names can be reused: if you rename or delete an organisation or repository, someone else can register the old name and inherit any trust granted to it. The numeric IDs are never reused, so the condition pins those:
REPO_ID="$(gh api "repos/$GITHUB_REPO" --jq '.id')"
OWNER_ID="$(gh api "repos/$GITHUB_REPO" --jq '.owner.id')"
echo "repository $REPO_ID, owner $OWNER_ID"Create a pool, which groups identities from outside Google Cloud, and a provider in it that trusts GitHub's token issuer:
gcloud iam workload-identity-pools create github \
--location=global \
--display-name="GitHub Actions"
gcloud iam workload-identity-pools providers create-oidc deploy-repo \
--location=global \
--workload-identity-pool=github \
--display-name="GitHub deploy repository" \
--issuer-uri="https://token.actions.githubusercontent.com" \
--attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repository,attribute.repository_id=assertion.repository_id,attribute.repository_owner_id=assertion.repository_owner_id,attribute.ref=assertion.ref" \
--attribute-condition="assertion.repository_owner_id == '${OWNER_ID}' && assertion.repository_id == '${REPO_ID}'"Two flags do the security work:
--attribute-mappingcopies claims from GitHub's token into attributes Google can match on.google.subjectis the identity that appears in audit logs, andattribute.repository_idis what the IAM binding in a later step refers to.--attribute-conditionis a check every token must pass before it is let in at all. GitHub signs tokens for every repository on github.com with the same issuer, so without a condition any public repository could present a valid token to your provider. Thegcloudreference says it plainly: with no condition, all valid credentials are accepted. Never create a GitHub provider without one.
Check it worked
Print the provider's full name and condition:
gcloud iam workload-identity-pools providers describe deploy-repo \
--location=global --workload-identity-pool=github \
--format='value(name,attributeCondition,state)'You see something like projects/123456789012/locations/global/workloadIdentityPools/github/providers/deploy-repo, then your condition with your two IDs in it, then ACTIVE. Keep the full name: it is the workload_identity_provider value the workflow needs.
Step 3 · Create a deploy account that can only deploy
Make two service accounts. github-deployer is the identity the workflow borrows. hello-wif-runtime is the identity the running service uses. Keeping them apart means the running container cannot deploy, and the pipeline cannot read whatever the service can read:
gcloud iam service-accounts create github-deployer \
--display-name="Deploys hello-wif from GitHub"
gcloud iam service-accounts create hello-wif-runtime \
--display-name="Runs hello-wif"
DEPLOYER="github-deployer@${PROJECT_ID}.iam.gserviceaccount.com"
RUNTIME="hello-wif-runtime@${PROJECT_ID}.iam.gserviceaccount.com"Give the deployer the two roles Cloud Run documents for deploying. Cloud Run Developer lets it create services and revisions. Service Account User, granted on the runtime account only, lets it attach that one account to the service:
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
--member="serviceAccount:$DEPLOYER" \
--role=roles/run.developer \
--condition=None
gcloud iam service-accounts add-iam-policy-binding "$RUNTIME" \
--member="serviceAccount:$DEPLOYER" \
--role=roles/iam.serviceAccountUserNotice what is missing. The deployer is not an Editor or Owner, cannot create keys, cannot touch other service accounts, and cannot change who may call the service: roles/run.developer has no permission to set IAM policy on a service, so a compromised workflow cannot make hello-wif public. The runtime account has no roles at all, which is right for the sample container. Give it only what your real service needs. /tutorials/grant-least-privilege-access-in-google-cloud-iam goes further.
Check it worked
gcloud iam service-accounts get-iam-policy "$RUNTIME"The policy has one binding: roles/iam.serviceAccountUser with the member serviceAccount:github-deployer@.... If you see the Compute Engine default account or a user here, remove that binding before going on.
Step 4 · Let only your repository use the deploy account
Now connect the two halves. Grant Workload Identity User on the deployer to the set of identities from the pool whose repository_id attribute is your repository's ID:
gcloud iam service-accounts add-iam-policy-binding "$DEPLOYER" \
--role=roles/iam.workloadIdentityUser \
--member="principalSet://iam.googleapis.com/projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/github/attribute.repository_id/${REPO_ID}"You now have two locks. The provider's condition rejects every token that is not from your repository. The binding means that even if you later loosen the condition, for example to admit every repository in your organisation, only this repository can become github-deployer.
To allow deploys from main only, add the branch to the condition. A pull request branch, or a workflow someone pushes on a feature branch, then cannot obtain the deployer at all:
gcloud iam workload-identity-pools providers update-oidc deploy-repo \
--location=global --workload-identity-pool=github \
--attribute-condition="assertion.repository_owner_id == '${OWNER_ID}' && assertion.repository_id == '${REPO_ID}' && assertion.ref == 'refs/heads/main'"IAM changes typically take about 2 minutes to apply and can take 7 minutes or longer, so a run straight after this step may still be refused.
Check it worked
gcloud iam service-accounts get-iam-policy "$DEPLOYER"You see roles/iam.workloadIdentityUser with one member that starts principalSet://iam.googleapis.com/projects/ and ends /attribute.repository_id/ followed by your repository ID. A member ending in /* would admit the whole pool; if you see one, remove it.
Step 5 · Write the workflow
None of the values the workflow needs is secret: a provider name and a service account email only work for tokens that pass your condition. Store them as repository variables, so they stay out of the YAML but are visible in logs when you debug:
gh variable set GCP_PROJECT_ID --repo "$GITHUB_REPO" --body "$PROJECT_ID"
gh variable set GCP_REGION --repo "$GITHUB_REPO" --body "$REGION"
gh variable set GCP_WIF_PROVIDER --repo "$GITHUB_REPO" \
--body "projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/github/providers/deploy-repo"
gh variable set GCP_DEPLOYER_SA --repo "$GITHUB_REPO" --body "$DEPLOYER"
gh variable set GCP_RUNTIME_SA --repo "$GITHUB_REPO" --body "$RUNTIME"In your repository's working copy, create .github/workflows/deploy-cloud-run.yml:
mkdir -p .github/workflows
cat > .github/workflows/deploy-cloud-run.yml <<'YAML'
name: Deploy to Cloud Run
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
concurrency:
group: deploy-cloud-run
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v7
- id: auth
name: Authenticate to Google Cloud
uses: google-github-actions/auth@v3
with:
project_id: ${{ vars.GCP_PROJECT_ID }}
workload_identity_provider: ${{ vars.GCP_WIF_PROVIDER }}
service_account: ${{ vars.GCP_DEPLOYER_SA }}
- id: deploy
name: Deploy hello-wif
uses: google-github-actions/deploy-cloudrun@v3
with:
service: hello-wif
image: us-docker.pkg.dev/cloudrun/container/hello
region: ${{ vars.GCP_REGION }}
project_id: ${{ vars.GCP_PROJECT_ID }}
flags: --service-account=${{ vars.GCP_RUNTIME_SA }}
- name: Show the service address
env:
SERVICE_URL: ${{ steps.deploy.outputs.url }}
run: echo "Deployed to $SERVICE_URL"
YAMLWhat each part is for:
id-token: writelets the job ask GitHub for an OIDC token. It is granted on the deploy job only, so other jobs you add later do not get it by default.contents: readkeeps the workflow's own GitHub token read-only.- The auth step sends GitHub's token to Google, receives a short-lived token for
github-deployer, and sets it up for the steps that follow. There is nocredentials_jsoninput and no secret. deploy-cloudrundeploys a published image here, so the example has no build step. For your own app, build and push an image to Artifact Registry first, and give the deployer write access to that one repository.--service-accountmakes the service run ashello-wif-runtimeinstead of the Compute Engine default account, which in older projects often holds Editor.- The URL reaches the shell through
env:, never pasted into therun:text, which is the habit that stops script injection.
google-github-actions actions are maintained by Google, but they are still outside code running with your deploy identity. For a stricter setup, pin each one to the full commit hash of the release you reviewed instead of the v3 tag, since a tag can be moved.
Check the file with actionlint (github.com/rhysd/actionlint, version 1.7.12; /tutorials/github-actions-first-workflow shows how to install it), and confirm no key slipped in:
actionlint .github/workflows/deploy-cloud-run.yml
echo "actionlint exit code: $?"
if grep -q "credentials_json" .github/workflows/deploy-cloud-run.yml; then echo "found a key input"; false; else echo "no key input in the workflow"; fiCheck it worked
actionlint prints nothing, then actionlint exit code: 0, then no key input in the workflow.
Step 6 · Push and watch the deploy
Commit the workflow on main and push:
git add .github/workflows/deploy-cloud-run.yml
git commit -m "Deploy to Cloud Run with Workload Identity Federation"
git push origin mainGitHub takes a few seconds to register the push and start a run, so wait about ten seconds, then look up the newest run of this workflow and watch it by its ID:
gh run list --repo "$GITHUB_REPO" --workflow=deploy-cloud-run.yml --branch main --limit 1
RUN_ID="$(gh run list --repo "$GITHUB_REPO" --workflow=deploy-cloud-run.yml --branch main --limit 1 --json databaseId --jq '.[0].databaseId')"
gh run watch "$RUN_ID" --repo "$GITHUB_REPO" --exit-statusIf the list is empty or shows an older run, the push has not been picked up yet: wait a few more seconds and run the three commands again. On a slow or prepaid connection, gh run watch keeps polling every few seconds until the run ends; you can close it and open the Actions tab later instead, since the run continues on GitHub.
Check it worked
The run turns green and the last step prints Deployed to https://hello-wif-...run.app. Open that address in a browser: you get 403 Forbidden, because the service is private and the deployer cannot change that. Call it as yourself instead, with an identity token from your own gcloud login:
SERVICE_URL="$(gcloud run services describe hello-wif --region "$REGION" --format='value(status.url)')"
curl -sS -H "Authorization: Bearer $(gcloud auth print-identity-token)" "$SERVICE_URL" | head -n 5
gcloud run services describe hello-wif --region "$REGION" --format='value(spec.template.spec.serviceAccountName)'curl prints the start of the sample's HTML page, and the last command prints hello-wif-runtime@....
If the auth step fails, read its error. A message that mentions the attribute condition (something like The given credential is rejected by the attribute condition) means the condition does not match the run: check the IDs, and the branch if you added one. Permission 'iam.serviceAccounts.getAccessToken' denied means the Workload Identity User binding is missing or still propagating. iam.serviceAccounts.actAs errors in the deploy step mean the Service Account User binding on the runtime account is missing.
Step 7 · Remove the old key
With a green deploy through federation, remove the long-lived credentials. Deleting a key breaks anything else still using it, so first search your other repositories and servers for that key ID. Then delete the GitHub secret, replacing GCP_CREDENTIALS with the name you noted in the first step:
gh secret delete GCP_CREDENTIALS --repo "$GITHUB_REPO"Delete the key itself, replacing KEY_ID and OLD_SA_EMAIL with the values from the first step:
gcloud iam service-accounts keys delete KEY_ID --iam-account=OLD_SA_EMAILIf your project belongs to an organisation, ask its administrator whether the Disable service account key creation policy is enforced. For organisations created on or after 3 May 2024 it is on by default; for older ones, turning it on stops new keys from appearing next time someone follows an old guide.
Check it worked
gcloud iam service-accounts keys list --iam-account=OLD_SA_EMAIL --managed-by=user
gh secret list --repo "$GITHUB_REPO"The key list is empty and the secret is gone. Push a small commit, or start the workflow from the Actions tab: it still deploys, now with nothing stored.
Step 8 · Clean up
If you made this setup only to learn, delete what it created. This removes the Cloud Run service, the deployer's project role, both service accounts and the pool with its provider. Remove the project binding before deleting the account: deleting a service account does not delete its role bindings, which stay on the project as a deleted:serviceAccount:...?uid=... member for up to 60 days.
gcloud run services delete hello-wif --region "$REGION" --quiet
gcloud projects remove-iam-policy-binding "$PROJECT_ID" \
--member="serviceAccount:$DEPLOYER" \
--role=roles/run.developer \
--condition=None
gcloud iam service-accounts delete "$DEPLOYER" --quiet
gcloud iam service-accounts delete "$RUNTIME" --quiet
gcloud iam workload-identity-pools delete github --location=global --quiet
for name in GCP_PROJECT_ID GCP_REGION GCP_WIF_PROVIDER GCP_DEPLOYER_SA GCP_RUNTIME_SA; do
gh variable delete "$name" --repo "$GITHUB_REPO"
doneA deleted pool can be restored for 30 days, and its name stays reserved until then, so a new pool needs a different ID, such as github-2. Delete or keep the workflow file in your repository as you prefer.
Check it worked
gcloud run services list --region "$REGION"
gcloud iam service-accounts list --filter="email:github-deployer OR email:hello-wif-runtime"
gcloud iam workload-identity-pools list --location=global
gcloud projects get-iam-policy "$PROJECT_ID" --flatten="bindings[].members" --filter="bindings.members:github-deployer" --format="value(bindings.role)"hello-wif and the two accounts are not listed, the github pool is either gone or shown as DELETED, and the last command prints nothing. If it still prints roles/run.developer because you deleted the account first, remove that deleted:serviceAccount: member by passing it exactly as get-iam-policy shows it to gcloud projects remove-iam-policy-binding.
Next steps
- Narrow what the runtime and deploy accounts can reach: /tutorials/grant-least-privilege-access-in-google-cloud-iam
- Build and deploy your own service instead of the sample: /tutorials/deploy-a-container-to-google-cloud-run
- Compare with a key that can run only one command on a server: /tutorials/deploy-to-a-server-over-ssh-with-github-actions
Related tutorials
- Grant least-privilege access in Google Cloud with IAM roles and service accountsYou will give one Cloud Run service its own service account that can read exactly one Secret Manager secret, test that access by impersonation instead of a key, add a grant that expires on its own, and review who can reach the secret.
- How to Deploy a Container to Google Cloud RunYou will package a small Node.js 26 web service with a Dockerfile, deploy it to Google Cloud Run with one gcloud command, cap how far it can scale, roll back to an earlier revision, and delete every resource the deploy created.
- How to Deploy to Your Server over SSH from GitHub ActionsYou will set up a deploy key that can only do one thing on your Ubuntu 26.04 server, store it as an encrypted GitHub environment secret, and write a workflow that uploads each release over SSH and switches to it with a single atomic step.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials