Secure GitHub Integration with Azure
This lesson explains how workload identity federation lets GitHub Actions sign in to Azure without stored secrets, and how federated credential subjects scope that access.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why stored client secrets are a risk.
- Describe how federated credentials match OIDC tokens.
- Choose subjects that tie access to environments.
- Identify the workflow settings azure/login needs.
The problem with stored secrets
The traditional way for a workflow to reach Azure is a service principal whose client secret is stored as a GitHub secret. It works, but the secret is a long-lived credential: anyone who obtains it can use it from anywhere until it expires or is rotated, and rotation requires updating every repository that uses it. Secrets also expire, which breaks pipelines at inconvenient times.
Workload identity federation removes the stored secret. Instead of proving its identity with a secret, the workflow presents a short-lived OpenID Connect (OIDC) token issued by GitHub for that specific run, and Microsoft Entra ID exchanges it for an Azure access token if it matches a trust relationship that you configured.
How federation works
The trust is configured as a federated credential on a Microsoft Entra app registration or on a user-assigned managed identity. It names the token issuer, https://token.actions.githubusercontent.com, the audience, api://AzureADTokenExchange, and the subject that identifies which workflows may use it.
-
The workflow requests a token
With
permissions: id-token: write, the runner can request an OIDC token from GitHub. -
GitHub issues a token
The token's subject describes the run, such as the repository and branch or environment.
-
azure/login presents it
The action sends the token to Microsoft Entra ID with the client ID of the app or identity.
-
Microsoft Entra ID validates it
The issuer, audience and subject must match a federated credential exactly.
-
Azure issues an access token
Later steps, such as
az mlcommands, use it until the job ends.
az identity federated-credential create --name gh-main \
--identity-name id-gh-mlops --resource-group rg-ml-shared \
--issuer https://token.actions.githubusercontent.com \
--subject repo:contoso/churn-mlops:ref:refs/heads/main \
--audiences api://AzureADTokenExchangeFederated credential subjects
The subject is the security boundary, because it decides which workflow runs can obtain the identity's access. A subject that matches only the main branch prevents a workflow on a feature branch from deploying to production. The common forms are listed in the following table.
| Subject | Matches | Typical use |
|---|---|---|
repo:ORG/REPO:ref:refs/heads/main | Runs on the main branch | Training or deployment after merge |
repo:ORG/REPO:environment:production | Jobs that use the production GitHub environment | Gated production deployments |
repo:ORG/REPO:pull_request | Runs triggered by pull requests | What-if previews and validation |
repo:ORG/REPO:ref:refs/tags/v1.2.0 | Runs for a specific tag | Tag-based releases |
Subject mismatches
A login that fails with an error about no matching federated identity record usually means the subject does not match. For example, a job that uses environment: production presents the environment subject, not the branch subject, so a credential for the main branch does not match it.
Signing in from a workflow
The azure/login action takes the client ID, tenant ID and subscription ID, which are identifiers rather than secrets and are usually stored as variables or secrets for convenience. The job must grant the id-token: write permission.
permissions:
id-token: write
contents: read
jobs:
train:
runs-on: ubuntu-latest
environment: dev
steps:
- uses: actions/checkout@v4
- 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 extension add --name ml --yes
- run: az ml job create --file pipelines/train.yml -g rg-ml-dev -w mlw-churn-devAfter login, the identity needs Azure roles for what the workflow does: AzureML Data Scientist on a workspace to submit jobs and deploy models, AzureML Registry User to publish to a registry, or Contributor on a resource group to deploy Bicep, plus a role-assignment role if the template creates role assignments.
One identity per environment
A repository has a workflow that trains in the development workspace on every merge to main, and deploys to the production workspace from a job that uses the production GitHub environment with required reviewers. Today both jobs use one service principal with a client secret and Contributor on the subscription. The security team wants no stored secrets and wants development jobs unable to change production.
Create two user-assigned managed identities (or app registrations). The development identity gets a federated credential for repo:ORG/REPO:ref:refs/heads/main and AzureML Data Scientist on the development workspace. The production identity gets a federated credential for repo:ORG/REPO:environment:production and only the roles needed on the production workspace. Each job signs in with its own client ID through azure/login, with id-token: write.
Rotating the existing secret more often reduces but does not remove the stored secret. One federated identity with access to both workspaces would let a development job change production. Scoping the single service principal to two resource groups still leaves a shared identity and a stored secret.
A workflow job that targets the production environment fails to sign in, although a federated credential exists for the main branch. Why?
Show answer
When a job uses an environment, the OIDC token's subject is repo:org/repo:environment:production, not the branch subject. The federated credential must match the subject exactly, so one is needed for the environment.
Exam Tips
Remember for the exam
- Workload identity federation replaces stored client secrets with short-lived OIDC tokens.
- Federated credentials match issuer, audience and subject exactly.
- Environment-based subjects tie production access to gated GitHub environments.
- Workflows need permissions: id-token: write and azure/login with client, tenant and subscription IDs.