Skip to content
Visual explainer 3h to learn 5 steps

Deploy to Azure from CI/CD with OIDC

Deploy from GitHub Actions or Azure Pipelines with workload identity federation, scoped roles and protected environments.

By Invicly Tech, , in Microsoft Azure Cloud Engineering. About 12 minutes to read.

Topic 21 of 33

In short

A pipeline that deploys to Azure needs an identity, and the safest one has no stored secret. With workload identity federation, GitHub Actions or Azure Pipelines gets a short-lived token from its own OpenID Connect issuer, and Microsoft Entra ID swaps it for an Azure access token because you registered a matching federated credential. Pair that with a narrowly scoped role and a protected environment, and a leaked repository holds nothing to steal.

Think of it like this: Instead of copying a house key for the courier, you tell the doorman to let in anyone wearing today's badge from one named company.

Explained simply

To deploy to Azure, your pipeline must sign in. The old way was to create a client secret, a long password for an app, and paste it into the pipeline's secret store. Secrets leak, expire at bad moments, and are hard to rotate.

Workload identity federation removes the secret. GitHub Actions and Azure Pipelines can each hand out short-lived, signed tokens that say which repository and environment a job belongs to. You tell Microsoft Entra ID to trust tokens with exactly that description. When they match, Entra ID gives the job an Azure access token for a few minutes.

The identity still needs permission, and it should get as little as possible. A deploy identity for production might be Contributor on one resource group, nothing more. Dev gets its own identity on its own group.

Finally, protect production in the pipeline tool itself. A GitHub environment or an Azure Pipelines environment can require a person to approve before the job runs. Because the trust is tied to that environment, a job that skips the approval cannot get a token.

Press Play on a diagram to walk through it one step at a time.

SequenceGitHub Actions signs in to Azure with OIDC
Workflow job
GitHub OIDC issuer
Microsoft Entra ID
Azure Resource Manager
  1. 1Request ID token (needs id-token: write)
    Workflow job to GitHub OIDC issuer: Request ID token (needs id-token: write)
  2. 2Signed token, subject repo:contoso/app:environment:production
    GitHub OIDC issuer to Workflow job: Signed token, subject repo:contoso/app:environment:production
  3. 3Exchange token for client ID
    Workflow job to Microsoft Entra ID: Exchange token for client ID
  4. 4Access token if issuer, subject and audience match
    Microsoft Entra ID to Workflow job: Access token if issuer, subject and audience match
  5. 5Deploy Bicep with the access token
    Workflow job to Azure Resource Manager: Deploy Bicep with the access token
  6. 6Allowed only within the role's scope
    Azure Resource Manager to Workflow job: Allowed only within the role's scope
MapSubject claims for one repository
repo:contoso/shop
  • :ref:refs/heads/main
    • Jobs on main without an environment
  • :environment:staging
    • Jobs that use the staging environment
  • :environment:production
    • Jobs approved for production
  • :pull_request
    • Pull request jobs; give read-only or no access

In depth

What Entra ID checks

A federated credential has three values that must match the incoming token. The issuer is who signed it: https://token.actions.githubusercontent.com for GitHub Actions, or an Azure DevOps issuer URL for Azure Pipelines. The subject says which workload is asking. For GitHub it looks like repo:contoso/shop:ref:refs/heads/main for a branch, repo:contoso/shop:environment:production when the job uses an environment, or repo:contoso/shop:pull_request for pull requests. The audience is api://AzureADTokenExchange. Matching is exact, so a job on a different branch or environment is refused with AADSTS700213, no matching federated identity record. You can put the credential on an app registration or on a user-assigned managed identity. Each one holds up to 20 federated credentials, so plan one per environment, not one per branch.

LayersLayers of trust for one deploy job
  1. 1
    GitHub environment rules

    Required reviewers, allowed branches

  2. 2
    OIDC token claims

    Issuer, subject, audience

  3. 3
    Federated credential

    Exact match on the identity in Entra ID

  4. 4
    Azure role assignment

    Contributor on rg-shop-prod only

  5. 5
    Azure Policy

    Still applies to whatever is deployed

Setting it up step by step

Prerequisites: a repository, a subscription where you can assign roles, and the Azure CLI. Step 1: create a user-assigned managed identity, or an app registration with a service principal, for each environment. Step 2: add a federated credential with the issuer, the subject for the environment and the audience. Step 3: assign a role on the target resource group. Step 4: store the client ID, tenant ID and subscription ID as repository or environment variables; they are identifiers, not secrets. Step 5: in the workflow, set permissions id-token: write and use azure/login@v2 with those three values. Evidence: the login step prints the subscription it connected to, and the Entra ID sign-in logs for service principals show a successful federated sign-in. Rollback: delete the federated credential to cut off access at once.

  • One identity per environment
  • Variables, not secrets, for IDs
  • permissions: id-token: write
  • Delete the credential to revoke

Scoping the deploy identity

Least privilege means the role, the scope and the identity all fit the job. Contributor on one resource group lets the pipeline create and change resources there but not grant access. If your Bicep also creates role assignments, for example giving the app's managed identity read access to storage, Contributor is not enough. Avoid jumping to Owner or User Access Administrator. Use Role Based Access Control Administrator with a condition that limits which roles it may assign and to which principal types, or have a platform team create those assignments separately. Keep dev and prod identities apart so a compromised dev workflow cannot touch production. Review role assignments for these identities regularly, and remove ones that are no longer used.

  • Contributor: create and change, not grant access
  • RBAC Administrator with conditions: limited role assignment
  • Avoid Owner on subscriptions
  • Separate identity and scope per environment

Protected environments

In GitHub, an environment such as production can require named reviewers, enforce a wait timer, and restrict which branches may deploy to it. A job that names the environment pauses until a reviewer approves. Because the federated subject is repo:OWNER/REPO:environment:production, only jobs that went through that gate can get a token. Availability of these protection rules for private repositories depends on your GitHub plan, so check before relying on them. In Azure Pipelines, an environment can have approvals and checks, such as a manual approval, business hours or a required template, and service connections can be restricted to selected pipelines. The service connection type to choose is Azure Resource Manager with workload identity federation, which Azure DevOps can create for you with the matching federated credential.

Bicep or Terraform from the pipeline

Both work with OIDC. With Bicep, the job runs az deployment group create after azure/login, and Azure is the only record of state. With Terraform, the azurerm provider supports OIDC with use_oidc = true, or the ARM_USE_OIDC environment variable, and the job needs id-token: write too. Terraform also needs a state file, commonly in a storage account through the azurerm backend, and the deploy identity needs data access to that container. Terraform's plan is similar to what-if, but it compares against state rather than live resources, which is why it catches drift only after a refresh. Choose based on your team and estate, not on the login method.

CompareBicep or Terraform for Azure delivery
Bicep
  • Azure only
  • No state file
  • New Azure features usable right away
  • Preview with what-if
Terraform
  • Many clouds and SaaS providers
  • State file to store and lock
  • Large module ecosystem
  • Preview with terraform plan

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Create a federated credential~35m
  2. 2
    Scope the deploy identity~35m
  3. 3
    Log in with azure/login~35m
  4. 4
    Protect production environments~35m
  5. 5
    Compare Bicep and Terraform~35m

Key points

  • A federated credential trusts one issuer, one exact subject and one audience.
  • No client secret is stored in GitHub; tokens live for minutes.
  • Give each environment its own identity, scoped to its own resource group.
  • Bind the production credential to a protected environment with required reviewers.
  • Granting role assignments from a pipeline needs extra rights; keep them constrained.

Key terms

Tap a card to flip it. 0/5 known

Examples

yaml
on: { push: { branches: [main] } }
permissions: { id-token: write, contents: read }
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    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 deployment group create -g rg-app-prod -f infra/main.bicep -p infra/prod.bicepparam

The IDs are not secrets, so repository or environment variables are fine.

Create a production deploy identity with the CLI

Prerequisites: rights to create a managed identity and assign roles on rg-shop-prod. Step 1: create the identity. Step 2: add a federated credential for the production environment of contoso/shop. Step 3: assign Contributor on the resource group only. Step 4: copy the client ID into the production environment's variables as AZURE_CLIENT_ID. Evidence: az role assignment list for the principal shows exactly one assignment, at the resource group scope. Rollback: az identity federated-credential delete removes the trust immediately; deleting the identity removes everything.

bash
az identity create -g rg-identities -n id-deploy-prod
az identity federated-credential create -g rg-identities \
  --identity-name id-deploy-prod --name github-production \
  --issuer https://token.actions.githubusercontent.com \
  --subject repo:contoso/shop:environment:production \
  --audiences api://AzureADTokenExchange
PID=$(az identity show -g rg-identities -n id-deploy-prod --query principalId -o tsv)
az role assignment create --assignee-object-id "$PID" \
  --assignee-principal-type ServicePrincipal --role Contributor \
  --scope /subscriptions/<subscription-id>/resourceGroups/rg-shop-prod
az identity show -g rg-identities -n id-deploy-prod --query clientId -o tsv

The same deployment from Azure Pipelines

Step 1: in Project settings, create an Azure Resource Manager service connection named sc-shop-prod using workload identity federation, scoped to rg-shop-prod. Azure DevOps creates the identity and federated credential, or you can point it at an existing one. Step 2: create an environment called production and add an approval check. Step 3: use a deployment job so the approval applies. Evidence: the run pauses for approval, then the AzureCLI task logs in without any secret and the deployment appears in the resource group's history. Rollback: re-run the pipeline for the previous commit.

yaml
trigger: [main]
jobs:
- deployment: deploy
  environment: production
  strategy:
    runOnce:
      deploy:
        steps:
        - checkout: self
        - task: AzureCLI@2
          inputs:
            azureSubscription: sc-shop-prod
            scriptType: bash
            scriptLocation: inlineScript
            inlineScript: az deployment group create -g rg-shop-prod -f infra/main.bicep -p infra/prod.bicepparam

Quick reference

  • GitHub issuer: https://token.actions.githubusercontent.com; audience: api://AzureADTokenExchange
  • Environment subject: repo:OWNER/REPO:environment:NAME
  • Workflow needs permissions: id-token: write
  • azure/login@v2 takes client-id, tenant-id and subscription-id; no secret
  • AADSTS700213 means the subject or issuer does not match any federated credential
  • Up to 20 federated credentials per app registration or managed identity
  • Deploy role: Contributor on one resource group; role assignments need a constrained admin role
  • Terraform: use_oidc = true (or ARM_USE_OIDC=true) plus a remote state backend

Common mistakes

  • A subject that does not match exactly, such as a branch subject when the job uses an environment, which fails with AADSTS700213.
  • Forgetting permissions: id-token: write, so the job cannot request an OIDC token.
  • Assigning Owner on the subscription to the deploy identity because it was quicker.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which workflow permission must be set for GitHub Actions to request an OIDC token?

Question 2

2Login fails with AADSTS700213. What is the most likely cause?

Question 3

3Your Bicep creates a role assignment for the app's managed identity. The deploy identity has Contributor. What happens?

Question 4

4Why tie the production federated credential to an environment subject rather than the main branch?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write the subject strings for three cases in repository fabrikam/api: jobs on the main branch, jobs in the staging environment, and pull request jobs.

  2. Exercise 2medium

    Turn a workflow that logs in with a client secret into one that uses OIDC. List each change.

  3. Exercise 3hard

    Design identities and permissions for a repo that deploys app resources to dev and prod, where prod Bicep also assigns Storage Blob Data Reader to the app's identity. Include how pull requests are handled.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Are the client ID and tenant ID secrets?
No. They identify the app and directory but cannot be used to sign in on their own. Storing them as variables is fine.
Should I use an app registration or a managed identity for federation?
Both work. A user-assigned managed identity is an Azure resource you can manage with Bicep and RBAC, while an app registration lives only in Entra ID and needs directory permissions to create.
Can one federated credential cover all branches?
Standard federated credentials need an exact subject match. Prefer environment subjects, which give one stable value per deployment target, rather than trying to match many branches.

Go deeper

ShareXLinkedInWhatsApp