Sign in to Azure from a workflow with OpenID Connect
In this task you'll let a workflow sign in to Azure without storing a secret.
With OpenID Connect (OIDC), GitHub gives each workflow run a short-lived token saying which repository, branch or environment it is. An Entra app registration with a federated credential trusts that exact claim and lets it sign in. There's no client secret to leak or rotate.
The subject must match exactly. New repositories may send a subject with ID numbers, such as repo:owner@123/shop@456:ref:refs/heads/main. If sign-in fails with AADSTS700213, copy the subject from the error into a new credential.
Example. SS Kaiser's deploy workflow signs in as app registration gh-shop, which trusts only the main branch of their repository and has Website Contributor on the shop's web app alone.
Before you start
You need: Rights to create an app registration. Time: 30 minutes.
Where to look
- Microsoft Entra ID
- App registrations
- (the app)
- Certificates & secrets
- Federated credentials
Steps
- Create an app registration and note its client ID and your tenant ID.Lab I2 in the Azure Portal guide walks through the same set-up.
- Under Certificates & secrets, Federated credentials, add one for GitHub Actions: your organisation, repository, entity type Branch, branch main.
- Give its service principal the smallest role on the smallest scope it needs, such as Website Contributor on one web app.
- In the workflow, set
permissions: id-token: writeand use azure/login with client-id, tenant-id and subscription-id from repository secrets.These IDs aren't secrets, but secrets keep them out of logs. - Run it. If login fails with AADSTS700213, add a credential with the exact subject shown in the error.
Commands
# Create the app and its service principal, and trust main of one repository
APPID=$(az ad app create --display-name gh-shop --query appId -o tsv)
az ad sp create --id $APPID
cat > fc.json <<'EOF'
{"name":"main","issuer":"https://token.actions.githubusercontent.com","subject":"repo:OWNER/REPO:ref:refs/heads/main","audiences":["api://AzureADTokenExchange"]}
EOF
az ad app federated-credential create --id $APPID --parameters fc.json
Watch out for
- Cost
- Free.
- Security
- Trust a branch or environment, never a whole repository with a wildcard. Scope the role to the resource, not the subscription.
- Common mistakes
- Forgetting
id-token: write, or a subject that doesn't match (case, branch name or ID-based format).
Clean up
Delete the app registration when you no longer need it, then remove it from Deleted applications.
Words to know
- Federated credential
- Trust between an Entra app and an outside identity provider, matched on issuer, subject and audience.
- Subject
- Who the token is for: repository plus branch, environment, tag or pull request.
- id-token: write
- The workflow permission that lets it ask GitHub for an OIDC token.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On