Last year we found a GCP service-account JSON key in a CI variable that had been created four years earlier by someone who had since left. It had Owner on a production project. The same audit turned up an Azure client secret that expired in the middle of a release, which is how we learned three pipelines depended on it. Both problems share one fix: stop storing cloud credentials in CI at all.

GitHub Actions and GitLab can both mint a signed OIDC token for each job. GCP and Azure can both trust that token and exchange it for short-lived credentials. This guide sets up all four combinations and, more importantly, shows the claim conditions that make sure only the right repository, branch and environment get access. The AWS side of the same pattern is covered in the post on GitHub OIDC and AssumeRoleWithWebIdentity.

How the exchange works

Every combination follows the same five steps:

  1. The CI job requests an OIDC token from its platform. The token is a JWT signed by the platform's issuer.

  2. The token carries claims about the job: repository or project path, branch or tag, environment, workflow, who triggered it.

  3. The job sends the token to the cloud's security token service.

  4. The cloud checks the signature against the issuer's public keys, checks the audience, and evaluates your conditions on the claims.

  5. If everything matches, the cloud returns a credential valid for minutes to an hour.

Nothing long-lived is stored in CI. The trust lives in cloud configuration that you can review in code.

GitHub Actions

GitLab.com

Issuer

https://token.actions.githubusercontent.com

https://gitlab.com (self-managed: your instance URL)

How a job asks

permissions: id-token: write

id_tokens: keyword with an aud

Subject format

repo:ORG/REPO:ref:refs/heads/main or repo:ORG/REPO:environment:prod

project_path:GROUP/PROJECT:ref_type:branch:ref:main

Useful extra claims

repository, repository_owner, ref, environment, job_workflow_ref

project_path, namespace_path, ref, ref_protected, environment

GCP: Workload Identity Federation

On GCP you create a workload identity pool and a provider for each issuer. The provider maps token claims into Google attributes and applies an attribute condition. Then you grant either the federated identity directly or a service account it may impersonate.

PROJECT_ID=example-prod
PROJECT_NUMBER=$(gcloud projects describe "$PROJECT_ID" --format='value(projectNumber)')

gcloud iam workload-identity-pools create ci-pool \
  --project="$PROJECT_ID" --location=global --display-name="CI pool"

# GitHub provider: only our organisation, only the main branch
gcloud iam workload-identity-pools providers create-oidc github \
  --project="$PROJECT_ID" --location=global --workload-identity-pool=ci-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_owner == 'example-org' && assertion.ref == 'refs/heads/main'"

# GitLab provider: only one group, only protected refs
gcloud iam workload-identity-pools providers create-oidc gitlab \
  --project="$PROJECT_ID" --location=global --workload-identity-pool=ci-pool \
  --issuer-uri="https://gitlab.com" \
  --allowed-audiences="https://gitlab.com" \
  --attribute-mapping="google.subject=assertion.sub,attribute.project_path=assertion.project_path" \
  --attribute-condition="assertion.namespace_path == 'example-group' && assertion.ref_protected == 'true'"

The attribute condition is the most important line. Without it, any repository on github.com could present a valid token to your provider. Google requires a condition for well-known public issuers for exactly this reason.

Grant access to one repository by its mapped attribute:

gcloud iam service-accounts add-iam-policy-binding "deployer@${PROJECT_ID}.iam.gserviceaccount.com" \
  --role=roles/iam.workloadIdentityUser \
  --member="principalSet://iam.googleapis.com/projects/${PROJECT_NUMBER}/locations/global/workloadIdentityPools/ci-pool/attribute.repository/example-org/web-app"

From GitHub Actions

jobs:
  deploy:
    runs-on: ubuntu-24.04
    environment: production
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: google-github-actions/auth@v2
        with:
          workload_identity_provider: projects/123456789012/locations/global/workloadIdentityPools/ci-pool/providers/github
          service_account: ${{ vars.GCP_DEPLOY_SA }}   # deployer service account email
      - uses: google-github-actions/setup-gcloud@v2
      - run: gcloud run deploy web --image="$IMAGE" --region=europe-west1

From GitLab

GitLab has no auth action, so the job writes a credential configuration file that points at the token and lets gcloud do the exchange.

deploy:
  image: google/cloud-sdk:slim
  id_tokens:
    GCP_ID_TOKEN:
      aud: https://gitlab.com
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
    - echo "$GCP_ID_TOKEN" > .ci_token
    - >
      gcloud iam workload-identity-pools create-cred-config
      projects/123456789012/locations/global/workloadIdentityPools/ci-pool/providers/gitlab
      --service-account="$GCP_DEPLOY_SA"
      --credential-source-file=.ci_token
      --output-file=.gcp_creds.json
    - gcloud auth login --cred-file=.gcp_creds.json
    - gcloud run deploy web --image="$IMAGE" --region=europe-west1

The aud in id_tokens must match --allowed-audiences on the provider. If you omit allowed audiences, GCP expects the full provider resource name prefixed with https://iam.googleapis.com/ as the audience.

Azure: federated identity credentials

On Azure you attach a federated identity credential to either an app registration or a user-assigned managed identity. Each credential matches exactly one issuer, one subject and one audience. The default audience for Azure is api://AzureADTokenExchange.

az identity create --name ci-deployer --resource-group rg-ci

az identity federated-credential create \
  --name github-web-app-prod \
  --identity-name ci-deployer --resource-group rg-ci \
  --issuer https://token.actions.githubusercontent.com \
  --subject repo:example-org/web-app:environment:production \
  --audiences api://AzureADTokenExchange

az identity federated-credential create \
  --name gitlab-infra-main \
  --identity-name ci-deployer --resource-group rg-ci \
  --issuer https://gitlab.com \
  --subject project_path:example-group/infra:ref_type:branch:ref:main \
  --audiences api://AzureADTokenExchange

Then assign an Azure role to the identity at the smallest useful scope, for example one resource group.

From GitHub Actions

jobs:
  deploy:
    runs-on: ubuntu-24.04
    environment: production
    permissions:
      id-token: write
      contents: read
    steps:
      - uses: azure/login@v2
        with:
          client-id: ${{ vars.AZURE_CLIENT_ID }}
          tenant-id: ${{ vars.AZURE_TENANT_ID }}
          subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
      - run: az webapp deploy --resource-group rg-web --name web-prod --src-path app.zip

The client, tenant and subscription IDs are identifiers, not secrets; storing them as plain variables is fine.

From GitLab

deploy:
  image: mcr.microsoft.com/azure-cli
  environment: production
  id_tokens:
    AZURE_ID_TOKEN:
      aud: api://AzureADTokenExchange
  script:
    - >
      az login --service-principal
      -u "$AZURE_CLIENT_ID" -t "$AZURE_TENANT_ID"
      --federated-token "$AZURE_ID_TOKEN"
    - az account set --subscription "$AZURE_SUBSCRIPTION_ID"
    - az webapp deploy --resource-group rg-web --name web-prod --src-path app.zip

The error you will see first

Azure matches the subject string exactly. When it does not match, login fails with:

AADSTS70021: No matching federated identity record found for presented assertion.

The usual causes: the GitHub job declares environment: (subject switches from ref: form to environment: form), the pipeline runs on a tag instead of a branch, or GitLab's ref_type is tag. Decode the token in a debug run to see the exact sub; print only the claims, never the raw token.

# GitLab debug job: show claims, not the signature
echo "$AZURE_ID_TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | jq '{iss, sub, aud, ref, environment}'

Choosing claim conditions

The difference between a safe and an unsafe federation is almost entirely in the conditions.

Condition

Protects against

Organisation or namespace match

Any other GitHub/GitLab user getting tokens accepted

Single repository or project

Other repos in your org reaching production

Branch main or protected refs only

Feature branches and forks deploying

GitHub environment in subject

Bypassing environment reviewers

GitHub job_workflow_ref

Copies of the deploy workflow edited in a branch

Our rule: production credentials require an environment (GitHub) or a protected ref plus environment (GitLab). Read-only credentials for plan or preview jobs can be looser, but always scoped to the organisation.

Migrating off stored keys

  1. Create the pool/provider or federated credential alongside the existing key.

  2. Switch one pipeline, deploy twice, confirm cloud audit logs show the federated principal.

  3. Switch the remaining pipelines.

  4. Disable the old key, wait a release cycle, then delete it. On GCP consider the organisation policy that blocks service-account key creation.

  5. Remove the old CI variables.

Common mistakes

Mistake

Why it hurts

Do this instead

GCP provider with no attribute condition

Tokens from unrelated repositories may be accepted

Always condition on owner/namespace at minimum

Granting the whole pool workloadIdentityUser

Every repo in the pool can deploy

Bind principalSet by repository attribute

Azure subject built for branch while job uses environment:

AADSTS70021

Match the subject form the job actually emits

Forgetting id-token: write in GitHub

Auth action cannot fetch a token

Set it per job, not globally

Printing the full token in logs

Token is replayable until expiry

Print decoded claims only

Key takeaways

  • Both GitHub and GitLab issue per-job OIDC tokens; GCP and Azure both accept them.

  • Trust is defined by issuer, audience and claim conditions, so review those like code.

  • GCP uses attribute conditions and principal sets; Azure uses exact subject matching per credential.

  • Production access should require a protected branch or environment claim.

  • Delete stored keys after the switch; a disabled key is still a key.

References