CI/CD on Azure · Part 5 of 7
GitHub Actions for staging
OIDC identity federation between GitHub and Azure (zero static secrets), then the workflow that builds, pushes to ACR and deploys to Container Apps on every push to staging.
- GitHub Actions
- OIDC
- Azure
Where we are, and what is still missing
Articles 3 and 4 laid down the two Azure building blocks of the staging environment: the container registry (ACR) and the Container App that pulls its image from it. Each step was validated by hand, on purpose, before automating anything.
This chapter finally puts it all together in a GitHub Actions pipeline: on every push to the staging branch, the image is built, pushed to ACR, and the Container App is updated to this new image, with no manual intervention at all.
We focus here on staging only. Deploying production to Azure Static Web Apps (main branch) follows a rather different logic, with its own prerequisites; that will be the subject of the next chapter.
But before writing this staging workflow, one prerequisite is still missing, and it is better to fill the gap now rather than along the way: identity federation between GitHub and Azure (OIDC). Listed as a goal as early as article 1 (“Handling authentication between GitHub Actions and Azure without static secrets”), it has not been configured yet. So we will set it up, then write the workflow that depends on it.
Why OIDC authentication rather than a static secret
The “classic” way to connect a CI/CD pipeline to Azure is to create a “service principal”, generate a client secret, and paste that secret into the GitHub secrets. It works, but this secret is a long-lived static credential: if it leaks (a badly filtered log, a repository made public by mistake, etc.), anyone who gets hold of it can authenticate as that service principal until someone thinks of revoking it. And in the meantime, you also have to remember to rotate it regularly.
OpenID Connect (OIDC), here through what Azure calls workload identity federation, changes the game: instead of storing a secret, we establish a trust relationship between Azure and GitHub. On every workflow run, GitHub issues a short-lived (a few minutes), signed identity token that states “this run really comes from repo X, on branch Y”. Azure checks this token against the configured trust relationship and, if everything matches, exchanges it for an Azure access token, also short-lived.
The most telling analogy for me would be a Kerberos ticket rather than a shared password: nothing static to store, nothing to rotate, and a token that expires on its own if it leaks. No Azure secret ever shows up in the GitHub logs, nor in the repository secrets.
Step 1: creating the app registration and the service principal for GitHub Actions
An app registration alone is not enough to receive RBAC roles: we also need the service principal (the “enterprise application”) backed by it. It is this service principal, through its PrincipalId, that will be the target of the role assignments.
$app_name = "gh-actions-westbalidrivers"
$ghApp = New-AzADApplication -DisplayName $app_name
$ghSp = New-AzADServicePrincipal -ApplicationId $ghApp.AppId# The 3 identifiers GitHub Actions will need to authenticate
$ghApp.AppId # Client ID
(Get-AzContext).Tenant.Id # Tenant ID
(Get-AzContext).Subscription.Id # Subscription IDWrite these three values down; they will go into the GitHub secrets in step 4.
Step 2: configuring identity federation (OIDC)
This is where the trust relationship with GitHub is actually established. The -Subject parameter is the most important one: it defines which GitHub token Azure will accept.
The repo:<owner>/<repo>:ref:refs/heads/<branch> format restricts trust to a precise trigger, a precise branch, of the precise repository. However, this syntax has been deprecated since 15 July 2026. GitHub now enforces the generation of “immutable” subjects, in the repo:<owner>@<owner_id>/<repo>@<repo_id>:ref:refs/heads/<branch> format.
So we need to query the GitHub API to get the required IDs. Since my repository is private, I use a PAT (Personal Access Token) here to authenticate with the GitHub API. The “Metadata: Read Only” permission, applied to this repo only, is plenty for this use.
$headers = @{
Authorization = "Bearer $GITHUB_TOKEN"
Accept = "application/vnd.github+json"
}
$repo = Invoke-RestMethod -Uri "https://api.github.com/repos/lolecat/WestBaliDrivers" -Headers $headers
# From there, we can extract the $repo.id and $repo.owner.id values to build the "subject" field
New-AzADAppFederatedCredential `
-ApplicationObjectId $ghApp.Id `
-Name "github-staging-branch" `
-Issuer "https://token.actions.githubusercontent.com" `
-Subject "repo:lolecat@$($repo.owner.id)/WestBaliDrivers@$($repo.id):ref:refs/heads/staging" `
-Audience "api://AzureADTokenExchange"A point of attention on -Subject: the comparison Azure makes is case-sensitive and strictly literal, with no fuzzy rule or wildcard on this type of credential. A run triggered from another branch, a tag, or a pull request will never match this Subject, and authentication will fail with an AADSTS700213: No matching federated identity record found error. That is the intended behavior here: only the staging branch must be able to authenticate.
Other “-Subject” formats exist for other use cases (
repo:<owner>/<repo>:environment:<name>for GitHub environments,repo:<owner>/<repo>:pull_requestfor PRs, etc.). Here we stick to the simplest format, a singleSubjectfor a single branch, consistent with a pipeline where each branch has a strictly defined role.
Step 3: assigning the required roles
Same principle of least privilege as in article 4 for the managed identity: this service principal must only be able to do what the staging workflow actually does. In practice, two things: push an image to ACR, and update the Container App.
$rg = "rg-westbalidrivers"
$acr_name = "acrwestbalidrivers"
$acr_id = (Get-AzContainerRegistry -ResourceGroupName $rg -Name $acr_name).Id
$aca_id = (Get-AzContainerApp -Name "ca-westbalidrivers-staging" -ResourceGroupName $rg).Id# Allow pushing images to ACR
New-AzRoleAssignment `
-ObjectId $ghSp.Id `
-RoleDefinitionName "AcrPush" `
-Scope $acr_id
# Allow updating the Container App
New-AzRoleAssignment `
-ObjectId $ghSp.Id `
-RoleDefinitionName "Container Apps Contributor" `
-Scope $aca_idBoth assignments are scoped to the precise resource, not to the whole resource group. This service principal will neither be able to touch the ACR beyond pushing images, nor touch any other resource in the group.
A reminder of how identities are split in this project:
- the managed identity from article 4 (
id-westbalidrivers-staging,AcrPullrole) is used by the Container App to pull its image at runtime.- The service principal created here is used by GitHub Actions to push the image and drive the Container App from the pipeline. Two distinct identities with two distinct sets of permissions, limited to the bare minimum.
Step 4: preparing the GitHub secrets
Head to Settings → Secrets and variables → Actions of the private lolecat/WestBaliDrivers repository.
Two choices are available: Environment secrets, or Repository secrets. At this stage, we will stick with Repository secrets.
| Name | Value | Role |
|---|---|---|
| AZURE_CLIENT_ID | $ghApp.AppId | identifies the app registration for the OIDC exchange |
| AZURE_TENANT_ID | (Get-AzContext).Tenant.Id | the target Azure tenant |
| AZURE_SUBSCRIPTION_ID | (Get-AzContext).Subscription.Id | the target subscription |
| MAPBOX_TOKEN | the project’s Mapbox token | injected when building the image (--build-arg) |
None of these secrets ever goes through a file in the repository, nor through a committed .env: that is the whole point of storing them at the GitHub level rather than in the code. Note that there is no Azure secret as such here: no password, no key, no certificate.
The three AZURE_* values are public identifiers (GUIDs), not secrets; that is the whole promise of OIDC.
Step 5: the staging workflow
.github/workflows/staging.yml:
name: Deploy to staging
on:
push:
branches: [staging]
permissions:
id-token: write # required for the OIDC exchange
contents: read
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Compute short SHA
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> "$GITHUB_ENV"
- name: Azure login (OIDC)
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
enable-AzPSSession: true
- name: Authenticate Docker against ACR
uses: azure/powershell@v2
with:
inlineScript: |
Connect-AzContainerRegistry -Name "acrwestbalidrivers"
azPSVersion: "latest"
- name: Build & push image
run: |
docker build \
-f docker/Dockerfile \
--target runner \
--build-arg NEXT_PUBLIC_MAPBOX_TOKEN=${{ secrets.MAPBOX_TOKEN }} \
-t acrwestbalidrivers.azurecr.io/westbalidrivers:staging \
-t acrwestbalidrivers.azurecr.io/westbalidrivers:sha-${{ env.SHORT_SHA }} \
.
docker push acrwestbalidrivers.azurecr.io/westbalidrivers:staging
docker push acrwestbalidrivers.azurecr.io/westbalidrivers:sha-${{ env.SHORT_SHA }}
- name: Deploy new image to Container App
uses: azure/powershell@v2
with:
inlineScript: |
$container = New-AzContainerAppTemplateObject `
-Name "westbalidrivers" `
-Image "acrwestbalidrivers.azurecr.io/westbalidrivers:sha-${{ env.SHORT_SHA }}" `
-ResourceCpu 0.25 `
-ResourceMemory "0.5Gi"
Update-AzContainerApp `
-Name "ca-westbalidrivers-staging" `
-ResourceGroupName "rg-westbalidrivers" `
-TemplateContainer $container
azPSVersion: "latest"The workflow reproduces, step by step, the manual sequence validated in articles 3 and 4: authentication, staging tag + sha tag, push, then Container App update. A few choices deserve an explanation.
permissions: id-token: writeis what allows the job to request an OIDC token from GitHub. Without this line,azure/loginfails with an explicit error about missing permissions, a great classic of the first run.Connect-AzContainerRegistryrather thandocker/login-actionoraz acr login: it is exactly the same cmdlet as the one used by hand in article 3. Sinceenable-AzPSSession: truehas already opened an Az PowerShell session authenticated through OIDC, this cmdlet configures Docker to use that same session, with no password or token to handle explicitly. Consistent with the project’s preference for theAzmodule over the Azure CLI.- The deployment points to the
sha-...tag, not tostaging: thestagingtag remains useful as a “latest version” pointer for a manualdocker pull, but the Container App is explicitly updated to a precise, immutable digest. This is the safety net for rollback mentioned as early as article 3: finding and redeploying an exact version never depends on a moving tag.
The Docker build runs here on the GitHub runner (Ubuntu), not in WSL2. The
docker buildcommand is therefore invoked directly, without the WSL2 gymnastics specific to the local workstation. The build context stays identical to the one in article 2; only the host changes.
Validation
Once the file is committed on the staging branch:
git checkout staging
git push origin stagingIn the Actions tab of the GitHub repository, the Deploy to staging workflow should trigger, and every step should turn green: OIDC login, Docker authentication, build, push, Container App update.
On the Azure side, we confirm that a new revision has been created with the right image (a reminder from article 4):
Get-AzContainerAppRevision `
-ContainerAppName "ca-westbalidrivers-staging" `
-ResourceGroupName "rg-westbalidrivers" | Select-Object Name, Active, CreatedTime, @{Name='Image'; Expression={$_.TemplateContainer.Image}}The image of the active revision must carry the sha-... tag matching the last commit pushed to staging. A final curl -IL on the Container App URL (see article 4) confirms that the site responds, Mapbox map included, this time served from an image built and deployed fully automatically.
If the Entra ID appendix of article 4 has been applied, this URL now redirects to a sign-in page: the
curlvalidation is then done on the redirect, and checking the site visually requires an authenticated browser.
Checklist before moving on
- The
gh-actions-westbalidriversapp registration exists, with an associated service principal - Identity federation is configured, scoped to
repo:lolecat@ID/WestBaliDrivers@ID:ref:refs/heads/staging - The service principal has the
AcrPush(on the ACR) andContainer Apps Contributor(on the Container App) roles, and nothing more - The 4 secrets are set in GitHub (
AZURE_CLIENT_ID,AZURE_TENANT_ID,AZURE_SUBSCRIPTION_ID,MAPBOX_TOKEN) - A push to
stagingtriggers the workflow and turns it fully green - A new revision of the Container App is active, with the image tagged with the SHA of the last commit
- The staging site responds correctly, Mapbox map included
Next
The staging environment is now fully automated: a simple push to staging is enough to build, publish and deploy the new version, without touching PowerShell anymore. Production remains. In the next chapter, we create the Azure Static Web Apps resource and write the workflow for the main branch, which follows a deliberately different logic: no container, no ACR, no Azure identity, but a direct deployment of the out/ folder to Static Web Apps.