Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 57 ยท Infrastructure as Code and DevOps

CI/CD with GitHub Actions and Azure DevOps

Automate build, test and deploy to Azure with GitHub Actions or Azure Pipelines, using OIDC federated credentials instead of stored secrets, with approvals.

Deploying by hand works until the day it does not: someone forgets a step, deploys the wrong branch, or is on holiday when a fix is needed. Continuous integration (CI) builds and tests every change automatically. Continuous delivery or deployment (CD) then ships tested changes to your environments by the same repeatable steps every time. Together they turn "deploy day" into a non-event.

For Azure, two systems dominate: GitHub Actions, built into GitHub, and Azure Pipelines, part of Azure DevOps. Both describe the process in a YAML file that lives in your repository. The hard part is not the YAML but the question underneath: how does a pipeline prove to Azure that it may deploy, without a password stored somewhere? This lesson answers that with OpenID Connect (OIDC) federated credentials, then covers stages, environments and approvals. Real pipelines need a repository and an Azure account, so the YAML and commands are read-only. The runnable examples are Node.js stand-ins: a pipeline runner, and the identity check behind OIDC.

The shape of a pipeline

Whatever the tool, a delivery pipeline has the same skeleton:

  1. Trigger: a push, a pull request, a schedule or a manual click.
  2. Build and test: restore dependencies, compile, run unit tests, produce an artifact (a package, a folder, a container image).
  3. Deploy: send that artifact to an environment. Ideally the same artifact goes to every environment; do not rebuild for production.
  4. Verify: smoke tests, health checks, and a rollback path.
flowchart LR
  T["Trigger<br/>push or PR"] --> B["Build + test"]
  B --> A["Artifact"]
  A --> D1["Deploy: staging"]
  D1 --> G{"Approval"}
  G --> D2["Deploy: production"]

One artifact is built once, then promoted through environments, with a human gate before production.

No stored secrets: OIDC federation

The old way to let a pipeline deploy was to create a service principal, generate a client secret, and paste it into the CI system's secret store. That secret is a long-lived password: it expires (and breaks the pipeline at 3 a.m.), it can leak through logs or a compromised account, and it works from anywhere.

Workload identity federation replaces it with trust. You tell Microsoft Entra ID: "tokens issued by GitHub, for this repository and this environment, may act as this app." At run time:

  1. GitHub issues the job a short-lived OIDC token that states who the job is: the repository, the branch or environment, and the workflow.
  2. The job sends that token to Entra ID and asks for an Azure access token.
  3. Entra checks the signature and compares the token's claims with the federated credential you configured. If they match, it returns an access token for the app's service principal.
  4. The job calls Azure with that token. Nothing secret was stored anywhere.
sequenceDiagram
    participant Job as "GitHub job"
    participant GH as "GitHub OIDC issuer"
    participant Entra as "Microsoft Entra ID"
    participant Az as "Azure resources"
    Job->>GH: "Request OIDC token"
    GH-->>Job: "Signed token: repo, environment, audience"
    Job->>Entra: "Exchange token"
    Entra-->>Job: "Azure access token (short-lived)"
    Job->>Az: "Deploy, with Bearer token"

The job proves who it is with a token GitHub signs; Azure checks it against a trust rule and issues a short-lived token.

Set it up once (read-only; it needs an account and permission to create app registrations, as described in Microsoft Entra ID):

Terminal
# 1. An app registration and service principal for the pipeline
appId=$(az ad app create --display-name shop-deployer --query appId --output tsv)
az ad sp create --id $appId

# 2. Trust tokens from one GitHub environment of one repository
az ad app federated-credential create --id $appId --parameters '{
  "name": "github-production",
  "issuer": "https://token.actions.githubusercontent.com",
  "subject": "repo:contoso/shop:environment:production",
  "audiences": ["api://AzureADTokenExchange"]
}'

# 3. Give it the least privilege it needs, on the narrowest scope
az role assignment create \
  --assignee $appId \
  --role "Website Contributor" \
  --scope /subscriptions/<sub>/resourceGroups/shop-prod-rg

The subject is the important line. It can name a branch (repo:contoso/shop:ref:refs/heads/main), an environment (repo:contoso/shop:environment:production), or a pull request (repo:contoso/shop:pull_request). Prefer the environment form for production: then only jobs that passed that environment's protection rules get tokens. Do the role assignment in line with Role-Based Access Control: a deployment identity rarely needs more than contributor rights on one resource group.

The identity check: a stand-in

Real tokens are signed with GitHub's private key and verified with its public keys. The stand-in below keeps the logic with a shared-key HMAC signature instead (a simplification: real OIDC tokens use public-key signatures, and Entra checks more claims than shown). It issues tokens for several jobs and tests each against one federated credential, so you can see exactly which claim makes the exchange fail.

Node.js Federated credential check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Only the first token succeeds. The pull request token fails because its subject is different, which is what you want: code from a pull request should not be able to deploy to production.

GitHub Actions

A workflow lives in .github/workflows/. Here is a build, test and deploy workflow for a Node.js web app on App Service (read-only; it needs a repository, an Azure app and the setup above):

YAML
name: build-and-deploy

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read
  id-token: write

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm test
      - run: npm run build
      - uses: actions/upload-artifact@v4
        with:
          name: site
          path: dist

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: site
          path: dist
      - uses: azure/login@v2
        with:
          client-id: ${{ vars.AZURE_CLIENT_ID }}
          tenant-id: ${{ vars.AZURE_TENANT_ID }}
          subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
      - uses: azure/webapps-deploy@v3
        with:
          app-name: shop-web
          package: dist

Key points:

  • permissions: id-token: write is what allows the job to request an OIDC token. Without it, login fails.
  • needs: test makes deploy wait for test, and skip if test fails.
  • if: limits deployment to the main branch, so pull requests only build and test.
  • environment: production attaches the job to a GitHub environment, where you configure required reviewers, a wait timer and branch rules. The job pauses until someone approves. Its OIDC subject becomes the environment form shown earlier.
  • The client, tenant and subscription IDs are identifiers, not secrets, so they can be plain variables. No password appears anywhere.
  • Pin actions to a version (@v4) or, for stricter supply-chain safety, to a full commit hash.

Azure Pipelines

Azure DevOps expresses the same ideas with stages, jobs and service connections. A service connection to Azure can use workload identity federation too, so there is again no stored secret. An azure-pipelines.yml (read-only):

YAML
trigger:
  branches:
    include: [main]

pool:
  vmImage: ubuntu-latest

stages:
  - stage: Build
    jobs:
      - job: BuildAndTest
        steps:
          - script: |
              npm ci
              npm test
              npm run build
            displayName: Install, test, build
          - publish: $(System.DefaultWorkingDirectory)/dist
            artifact: site

  - stage: DeployProd
    dependsOn: Build
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
    jobs:
      - deployment: Deploy
        environment: production
        strategy:
          runOnce:
            deploy:
              steps:
                - task: AzureWebApp@1
                  inputs:
                    azureSubscription: shop-prod-connection
                    appName: shop-web
                    package: $(Pipeline.Workspace)/site

A deployment job targets an environment. In Azure DevOps you add approvals and checks to that environment (approvers, business hours, required branch), and the pipeline waits at the gate. Choose GitHub Actions if your code is on GitHub; choose Azure Pipelines if you already use Azure Boards, test plans and the rest of Azure DevOps. Large teams sometimes use both.

A pipeline runner: a stand-in

GitHub and Azure DevOps run your jobs on their own machines, which this page cannot do. The stand-in below runs a pipeline model: jobs with needs, an if condition, steps that can fail, and an environment gate that waits for an approver. It runs four scenarios so you can see how a failing test, a pull request and a missing approval each stop the flow. The steps are tiny functions standing in for real commands.

Node.js Run a staged pipeline with an approval gate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The pull request builds and tests but never reaches deploy. A failing test stops everything after it. A missing approval parks the pipeline at the gate. Only the last scenario deploys, and only the build output from that same run reaches production.

Habits that keep pipelines healthy

  • Keep builds fast with dependency caching and parallel jobs; slow pipelines get skipped.
  • Build once, deploy many. Promote the same artifact or image through environments. Container-based apps push to a registry first (Azure Container Registry).
  • Run infrastructure code in the pipeline too: what-if or terraform plan on pull requests and apply after review (Bicep and ARM Templates, Terraform on Azure).
  • Separate identities per environment, each with minimal rights on its own resource group.
  • Plan the rollback before you need it. Strategies such as slots, blue-green and canary releases come in Deployment Slots, Blue-Green and Canary.
  • Treat pipeline files as code: review them, and restrict who can edit workflows that can deploy to production.

Azure DevOps beyond pipelines: Boards, Repos, Test Plans and Artifacts

Azure Pipelines is one of five services in Azure DevOps. The others cover the planning and storage that surround a pipeline, and they work best when linked:

  • Azure Boards: work tracking with a backlog, sprints and Kanban boards.
  • Azure Repos: Git repositories with branch policies and pull requests.
  • Azure Pipelines: build and release automation (this lesson).
  • Azure Test Plans: manual and exploratory test management (a paid add-on).
  • Azure Artifacts: private package feeds for NuGet, npm, Maven, Python and universal packages.

Organizations, projects and process templates

An organization (such as dev.azure.com/contoso) contains projects; a project holds the boards, repos, pipelines and permissions of one product or team. When you create a project you choose a process, which decides the work item types and states: Basic (Issue, Task, simplest), Agile (Epic, Feature, User Story, Task, Bug), Scrum (Product Backlog Item, Bug) or CMMI (formal requirements and change requests). Pick the simplest that fits; changing later is possible but tedious.

Terminal
az extension add --name azure-devops
az devops configure --defaults organization=https://dev.azure.com/contoso project=Shop

az devops project create --name Shop --process Agile --visibility private
az repos create --name shop-web
az boards work-item create --type "User Story" --title "Customer can reset password"
az repos pr create --source-branch feature/reset --target-branch main \
  --title "Password reset" --work-items 42

Backlog, sprints and boards

The backlog is an ordered list of work items. A sprint (iteration) is a fixed period, often two weeks, to which you assign items; the sprint board shows columns such as New, Active and Closed. Area paths divide the project by team or component, and iteration paths by time. The Kanban board is the same data in a continuous-flow view with work-in-progress limits.

Linking the pieces

The real value is traceability. Write AB#123 (work item 123) in a commit message or pull request description and Azure Boards links it automatically and can move the item to Done when the pull request merges. A pipeline run shows which work items it contains, so a release note can list exactly what shipped. Combine with branch policies on main (required reviewers, a passing build, linked work item) and a protected release path (see CI/CD with GitHub Actions and Azure Pipelines above).

Azure Artifacts feeds let a pipeline publish a shared library once and many apps consume it with a normal package manager command; upstream sources can proxy public registries so builds keep working when the public one is down. Test Plans hold test cases and suites that testers run and record by hand, with results linked to bugs, useful where automated tests do not cover a workflow.

GitHub offers equivalents (Issues, Projects, Actions, Packages); many teams mix them, for example GitHub repos with Azure Boards. Choose by where your code and people already live.

The stand-in scans invented commit messages for AB# references and moves linked work items along, the way the integration does when a pull request completes.

JS AB# links move work items

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main advantage of OIDC federated credentials over a stored client secret?

    Federation replaces a stored password with trust in a token issuer. You still assign roles to the identity, and Entra ID still issues the access token.
  2. 2. Which GitHub workflow setting allows a job to request an OIDC token?

    The id-token: write permission lets the job obtain an OIDC token. Without it, azure/login cannot authenticate.
  3. 3. A federated credential has the subject repo:contoso/shop:environment:production. Which job can get an Azure token?

    The subject must match exactly. Pull request tokens and tokens from other repositories carry different subjects and are refused.
  4. 4. What does needs: test do on the deploy job?

    needs defines a dependency between jobs. A failed or skipped dependency means the dependent job does not run.
  5. 5. Why build the artifact once and promote it through environments?

    A rebuild can differ (new dependency versions, different tools). Promoting one artifact removes that risk.

Summary

  • CI builds and tests every change; CD ships tested artifacts to environments the same way every time.
  • OIDC workload identity federation lets a pipeline authenticate with a short-lived token instead of a stored secret; the federated credential's issuer, subject and audience must match the token.
  • Prefer environment-scoped subjects for production and give the deployment identity least-privilege roles.
  • GitHub Actions uses workflows, jobs, needs, if, environments and permissions: id-token: write; Azure Pipelines uses stages, deployment jobs and service connections.
  • Environments add approvals and checks, so a human gate can protect production.
  • Build once, promote the artifact, and plan rollbacks.

Next: Deployment Slots, Blue-Green and Canary.