CI/CD on Azure · Part 6 of 7

Production on Azure Static Web Apps

Rather than rebuilding the site for prod, we promote the image validated in staging. Static Web App, security headers, Git and ACR version tags, and a first manual release to production.

  • Azure
  • Static Web Apps
  • Build once, deploy many

Where we are, and a change of course

Staging is now fully automated: a push to staging builds the image, pushes it to ACR under a sha-<hash> tag, and updates the Container App. Production remains.

The initial plan, announced as early as article 1 and recalled at the end of article 5, was the following: a push to main triggers a next build, and the resulting out/ folder is sent directly to Azure Static Web Apps. No container, no ACR, no Azure identity.

While preparing this chapter, I changed my mind. This plan has a flaw I had not really taken the measure of: the site deployed to production would never have been tested as is. It would be rebuilt from the same sources, admittedly, but by another build, on another runner, at another moment. Dependencies are pinned by the lockfile, so in 99% of cases the result would be identical. But “identical in 99% of cases” is not “identical”.

The principle I keep instead is a classic of continuous delivery: build once, deploy many. We build the artifact only once, we validate it in staging, and it is that same artifact that we promote to production. In practice:

  • the sha-<hash> image validated in staging is marked as “ready for prod” with a version tag (v0.1.0);
  • we extract from this image the static files it contains;
  • we publish these files to Static Web Apps.

This change has three consequences, all of which I find positive:

  1. The departure documented in article 1 shrinks considerably. I wrote there that what is validated in staging (an Nginx container) is not strictly what runs in production. That is still true for the server (Nginx on one side, the Static Web Apps infrastructure on the other), but the files served become identical down to the byte.
  2. Staging becomes a real gateway to production. No image validated in staging, no release to production possible.
  3. Rollback requires no rebuild. Going back to the previous version amounts to re-promoting the v0.0.9 tag, whose artifact already exists in ACR and has already run.

On the other hand, production now consumes ACR, which articles 1, 3 and 5 explicitly announced would not happen. I would rather own this U-turn here than rewrite history: that is also what an evolving architecture looks like.

A bit of housekeeping along the way: the production branch of the repository was still called master, while the whole series talks about main. It was renamed before writing this chapter. No workflow and no identity depended on it yet, so it was the cheapest moment to do it.

This chapter focuses on the Azure side and on a first, fully manual promotion, true to the rule followed since the beginning of the series: validate by hand before automating. The production GitHub Actions workflow will be the subject of the next chapter.

The prerequisite for promotion: an environment-agnostic artifact

Promoting an artifact only makes sense if this artifact contains nothing specific to the environment in which it was built. Everything Next.js inlines at build time is frozen into the staging image, and will go to production as is.

In our case, the only value injected at build time is NEXT_PUBLIC_MAPBOX_TOKEN, and it is the same token for all environments. The rest of what sets staging apart from production already lives, or will live, in the hosting layer:

  • staging is protected by Easy Auth (appendix to article 4), configured on the Container App, not in the site;
  • production, as we are about to see, will carry its own HTTP headers through the Static Web Apps configuration.

The rule to keep in mind from now on: what varies per environment belongs to the hosting, not to the artifact. The day the Mapbox token is split between prod and the rest (to restrict it to the production domain), we will have to either load it at runtime, or give up promotion and go back to a rebuild.

Step 1: creating the Static Web App

Prerequisites

powershell
Register-AzResourceProvider -ProviderNamespace Microsoft.Web

Working variables

powershell
$rg       = "rg-westbalidrivers"
$location = "westeurope"
$swa_name = "swa-westbalidrivers-prod"

Creation

powershell
New-AzStaticWebApp `
  -ResourceGroupName $rg `
  -Name $swa_name `
  -Location $location `
  -SkuName "Free"

The “Free” SKU is plenty for Phase 0: HTTPS and custom domain included, global CDN, no cost. The Standard SKU brings an SLA, more custom domains and advanced authentication features, none of which a static showcase site needs today.

The -Location does not determine where the site is served from (the content is distributed globally), only where the resource’s metadata lives. We stay consistent with the rest of the project by choosing westeurope.

A point of attention: what we do not pass to this cmdlet. New-AzStaticWebApp accepts -RepositoryUrl, -RepositoryToken and -Branch parameters. Setting them triggers the “turnkey” Static Web Apps path: Azure commits a workflow file itself into the GitHub repository, with its own build logic, and configures the required secrets. It is handy for a first try, but it is exactly the opposite of what we want here: we would lose control of the pipeline, and this workflow would rebuild the site instead of promoting the validated artifact. So we create a “bare” resource, which we will feed ourselves.

A welcome side effect: with no linked repository, Static Web Apps does not create preview environments on every pull request either. We already have a staging, no need for a second one.

Getting the generated URL

powershell
$swa = Get-AzStaticWebApp -ResourceGroupName $rg -Name $swa_name
$swa.DefaultHostname

The result looks like <random-words>-<hash>.azurestaticapps.net. At this point, this URL serves a generic placeholder page: nothing has been deployed yet.

Step 2: hosting configuration with “staticwebapp.config.json”

In staging, docker/nginx.conf decides how the files are served. In production, that role falls to a staticwebapp.config.json file, read by Static Web Apps at deployment time.

Where to put it

There were two options:

  • put it in public/: Next.js would copy it into out/, so it would end up in the staging image (inert under Nginx, but served publicly to anyone requesting /staticwebapp.config.json);
  • version it separately, and add it to the extracted files when deploying to production.

I keep the second one, to stay true to the rule stated above: this is hosting configuration, it has no business in the artifact. So we need to create a deploy/swa directory at the root of the project, and add the staticwebapp.config.json file to it.

The content

json
{
  "responseOverrides": {
    "404": {
      "rewrite": "/404.html",
      "statusCode": 404
    }
  },
  "globalHeaders": {
    "X-Robots-Tag": "noindex, nofollow",
    "X-Content-Type-Options": "nosniff",
    "X-Frame-Options": "DENY",
    "Referrer-Policy": "strict-origin-when-cross-origin",
    "Content-Security-Policy-Report-Only": "default-src 'self'; script-src 'self' 'unsafe-inline' https://api.mapbox.com; style-src 'self' 'unsafe-inline' https://api.mapbox.com; img-src 'self' data: blob: https://res.cloudinary.com https://api.mapbox.com; connect-src 'self' https://api.mapbox.com https://events.mapbox.com; worker-src 'self' blob:; frame-ancestors 'none'"
  },
  "routes": [
    {
      "route": "/_next/static/*",
      "headers": {
        "Cache-Control": "public, max-age=31536000, immutable"
      }
    }
  ]
}

A few choices deserve an explanation.

No navigationFallback. It is the Static Web Apps equivalent of the last argument of try_files in Nginx. Pointed at /index.html, it would return the home page for any URL that does not exist, thus hiding the real 404s. That is exactly what we avoided on the Nginx side with =404. Without navigationFallback, Static Web Apps only serves the files actually deployed: parity with staging is preserved.

responseOverrides on the 404 code. Static Web Apps does not automatically serve the 404.html generated by Next.js: we have to tell it to, as we did with error_page 404 /404.html in Nginx. The explicit statusCode: 404 guarantees that the custom error page is returned with a 404 code, not a 200. The difference is invisible to a visitor, but not to a search engine or a monitoring tool.

X-Robots-Tag: noindex, nofollow, temporary. As long as the custom domain is not in place, the production site is public on its *.azurestaticapps.net URL. If a search engine indexes it at that address, we would then have to deal with duplicate content between this URL and the future domain. This header prevents it. It is set at the hosting level, not in the HTML: it will go away with a simple change to this file, the day the domain is wired up, without touching the artifact.

The CSP in Report-Only mode. A Content Security Policy restricts the origins from which the browser agrees to load scripts, styles, images, etc. Badly tuned, it breaks the site without warning: the Mapbox map, for example, needs api.mapbox.com, events.mapbox.com and blob: workers, and Next.js injects inline scripts. In Report-Only mode, the browser enforces nothing: it only reports in its console what it would have blocked. We observe, we adjust, and we switch to blocking mode once the policy is validated. Always the same philosophy: validate before enforcing.

Step 3: manually promoting the version currently in staging

We are going to walk through by hand, a first time, what the production pipeline will then do automatically.

Identifying the image running in staging

powershell
$appRevision = Get-AzContainerAppRevision -ContainerAppName "ca-westbalidrivers-staging" -ResourceGroupName $rg `
    | ?{$_.Active -eq $true} `
    | Select-Object -Property Name, CreatedTime, @{Name ='Image'; Expression = {$_.TemplateContainer.Image}}

The image of the active revision carries a tag of the form sha-95ab7cf. It is this image, and this image only, that we are going to promote.

powershell
$short_sha = $appRevision.Image.Substring($appRevision.Image.Length - 7) # Extract the last 7 characters of the image name
$version   = "v0.1.0"
$acr       = "acrwestbalidrivers.azurecr.io"
$image     = "$acr/westbalidrivers"

Setting the version tag on the Git side

The Git tag marks the source commit of the artifact. It makes the link between a version in production and the exact state of the code visible to anyone with access to the repository.

bash
git fetch origin staging
$full_sha=$(git rev-parse "95ab7cf^{commit}")
git tag -a v0.1.0 "$full_sha" -m "First release to production"
git push origin v0.1.0

An annotated tag (-a) rather than a lightweight one: it records the author, the date and a message. It is a minimal, but real, audit trail of who decided to promote what, and when.

Setting the same tag on the ACR side

powershell
Connect-AzContainerRegistry -Name "acrwestbalidrivers"
bash
docker pull acrwestbalidrivers.azurecr.io/westbalidrivers:sha-95ab7cf
docker tag  acrwestbalidrivers.azurecr.io/westbalidrivers:sha-95ab7cf acrwestbalidrivers.azurecr.io/westbalidrivers:v0.1.0
docker push acrwestbalidrivers.azurecr.io/westbalidrivers:v0.1.0

The docker push is almost instant: all the layers are already in ACR, only a new tag is created, pointing to the same manifest. Let’s check:

powershell
Get-AzContainerRegistryTag -RegistryName "acrwestbalidrivers" -RepositoryName "westbalidrivers"

The sha-95ab7cf and v0.1.0 tags must appear on the same image, with the same digest.

This version tag has a second role, which we will make use of when we finally write the retention policy left pending in article 3: sha-* tags can be purged regularly, while v* tags will be kept. An image promoted to production thus stays available as a rollback point, even once its sha-* tag has been deleted.

Writing down the digest

A tag can technically be moved to another image. A digest (sha256:...), on the other hand, designates a precise content and cannot lie. We write it down to keep an exact record of what goes to production:

bash
docker inspect --format '{{index .RepoDigests 0}}' acrwestbalidrivers.azurecr.io/westbalidrivers:v0.1.0

A small correction along the way: article 5 stated that the Container App was deployed to “a precise, immutable digest” through the sha-... tag. That was loose wording: sha-95ab7cf is a tag, admittedly never rewritten by our pipeline, but a tag all the same. The digest is this sha256:....

Extracting the static files from the image

We do not need to run the container to get its files: docker create instantiates a stopped container, whose content we simply copy.

bash
docker create --name swa-extract acrwestbalidrivers.azurecr.io/westbalidrivers:v0.1.0
docker cp swa-extract:/usr/share/nginx/html ./release
docker rm swa-extract

Adding the hosting configuration

bash
cp deploy/swa/staticwebapp.config.json ./release/

The ./release folder now contains exactly what is going to production: the artifact validated in staging, plus the configuration specific to this hosting.

Step 4: deploying with the SWA CLI

Static Web Apps accepts deployments through a deployment token, specific to each resource. We get it with the Az module, without ever displaying it, by placing it directly in the environment variable expected by the deployment tool:

powershell
$env:SWA_CLI_DEPLOYMENT_TOKEN = ((Get-AzStaticWebAppSecret -ResourceGroupName $rg -Name $swa_name).Property | ConvertFrom-Json).apiKey

Deployment is done with the SWA CLI, Microsoft’s official tool. Rather than installing Node.js and this tool on the workstation, we run it in a throwaway container, consistent with the approach of the series:

shell
docker run --rm `
  -v "$(pwd)/release:/release" `
  -e SWA_CLI_DEPLOYMENT_TOKEN `
  node:22 `
  npx -y @azure/static-web-apps-cli deploy /release --env production

A point of attention on --env production: without this option, the SWA CLI deploys by default to a preview environment, not to production. The deployment succeeds, the CLI shows a URL… but not the one of the production site, which keeps serving its placeholder page. It is the kind of mistake that costs a good quarter of an hour, so better know it beforehand.

Once the deployment is done, we clean up:

powershell
Remove-Item env:SWA_CLI_DEPLOYMENT_TOKEN

This token is a long-lived secret, exactly the kind of credential article 5 worked hard to eliminate. It is acceptable for a one-off manual operation, fetched on the fly and never stored, but not for a pipeline. In the next chapter, the workflow will fetch it at runtime through a dedicated OIDC identity, and it will never be saved in the GitHub secrets. If you suspect a leak, it can be regenerated from the portal (Manage deployment token), which immediately invalidates the old one.

Step 5: validation

bash
fqdn="<random-words>-<hash>.azurestaticapps.net"

# Home page: 200
curl -I "https://$fqdn/"

# Site routes: 200
curl -I "https://$fqdn/tours"
curl -I "https://$fqdn/transfers"
curl -I "https://$fqdn/gallery"

# A URL that does not exist: 404, not 200 with the home page
curl -I "https://$fqdn/this-page-does-not-exist"

# Hosting headers: X-Robots-Tag, security headers, CSP Report-Only
curl -sI "https://$fqdn/" | grep -iE "x-robots-tag|x-content-type|x-frame|referrer|content-security"

# The configuration file must not be served publicly
curl -I "https://$fqdn/staticwebapp.config.json"

The test on the non-existent URL is the most important one on the list: it is what confirms that the absence of navigationFallback produces the same behavior as =404 in staging.

What is still manual, and will not stay that way

This first promotion took a good ten commands, in two shells, with a secret handled by hand. That was intended, so that each step would be validated on its own. The next chapter automates the whole thing:

  • a production.yml workflow, triggered manually (workflow_dispatch) with the tag to promote as a parameter
  • an identity dedicated to production, separate from the staging one, with a “federated credential” restricted to the main branch and rights limited to the bare minimum
  • a structural safeguard: the workflow will refuse to promote a tag whose sha-* image does not exist in ACR
  • the ACR retention policy, at last, designed to keep the promoted versions

Checklist before moving on

  • The swa-westbalidrivers-prod Static Web App exists on the Free SKU, without a linked GitHub repository
  • deploy/swa/staticwebapp.config.json is versioned in the repository, outside of public/
  • The promoted commit carries the annotated Git tag v0.1.0
  • The matching image carries the sha-<hash> and v0.1.0 tags in ACR, on the same digest, and this digest is written down
  • The extracted folder no longer contains 50x.html
  • The deployment was done with --env production, and the token was removed from the session
  • The site responds over HTTPS on its azurestaticapps.net URL, routes included
  • A URL that does not exist returns 404 with the custom page
  • The X-Robots-Tag, security and cache headers are present
  • staticwebapp.config.json is not publicly reachable
  • The Mapbox map works in production

Next

Production is running, and it serves exactly the files validated in staging. All that is left is to take the human out of the mechanical steps without taking them out of the decision. In the next chapter, we write the production.yml workflow: one-click promotion of a tag, a dedicated OIDC identity, no stored secret, and a rollback that boils down to running the workflow again with the previous tag.

© 2026 Louis Lecat