, Mark Joseph Colibao, home

Deploy a static site to Azure Static Web Apps with a custom domain

Build a static site in GitHub Actions, upload it to Azure Static Web Apps on the Free plan, then point www and the bare domain at it with free HTTPS.

Written 4 min read

A static site built in GitHub Actions and uploaded to Azure Static Web Apps, on www.<domain> and <domain> with a free certificate. It deploys on every merge to main, or only when you press Run workflow if you drop the push trigger. My DNS is on Cloudflare, so its screens are the example.

What happens when someone types the bare domain
  1. <domain>

    DNSCNAME at @, flattened by Cloudflare, DNS only: points to the app hostname

  2. Static Web AppIt isn't the default domain, so the request is redirected

  3. https://www.<domain>

    DNSCNAME www, DNS only: points to the same app hostname

  4. Static Web AppServes the built site over HTTPS, with a free certificate

Copy: marks a value you'll need later.

1. Create the Static Web App

The Free plan gives 2 custom domains, 3 preview environments and up to 250 MB per app.

bash
RG=<resource-group>; LOCATION=<location>; SWA=<static-web-app>

az group create --name $RG --location $LOCATION
az staticwebapp create --name $SWA --resource-group $RG --location $LOCATION --sku Free

az staticwebapp show --name $SWA --resource-group $RG \
  --query defaultHostname -o tsv   # copy: the app's hostname

With no --source, Azure links no repository and adds no workflow.

Pick Other, not GitHub. With GitHub, Azure commits its own workflow, which deploys on every push and every pull request. If that happened, delete the file it added under .github/workflows/.

2. Save the deployment token in GitHub

The workflow signs in with this token. Anyone who has it can deploy to the app, so it only lives in a secret.

bash
az staticwebapp secrets list --name $SWA --resource-group $RG \
  --query properties.apiKey -o tsv \
  | gh secret set AZURE_STATIC_WEB_APPS_API_TOKEN --repo <owner>/<repo>

If the token leaks, reset it (az staticwebapp secrets reset-api-key, or Reset token in the portal) and update the secret.

3. Add the workflow

Save as .github/workflows/ci-cd.yml. Every run installs, tests and builds first. A pull request stops there. A merge to main, or Run workflow on main, then uploads the built site. Remove push to deploy only by hand.

YAML
name: deploy

on:
  pull_request:
    branches: [main] # checks only, never deploys
  push:
    branches: [main] # automatic: every merge to main deploys
  workflow_dispatch: # manual: the Run workflow button on the Actions tab

permissions:
  contents: read

jobs:
  ci-cd:
    runs-on: ubuntu-latest
    concurrency:
      group: ${{ github.ref }}
      cancel-in-progress: false # let a running deploy finish
    steps:
      - uses: actions/checkout@<full-length-commit-sha> # v7
      - uses: actions/setup-node@<full-length-commit-sha> # v7
        with:
          node-version: 24
          cache: npm
      - run: npm ci
      - run: npm test # add your lint and type checks here too
      - run: npm run build # writes the site to dist/

      - name: Deploy
        if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
        uses: Azure/static-web-apps-deploy@<full-length-commit-sha> # v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
          action: upload
          app_location: dist # the built files
          output_location: ''
          skip_app_build: true
          skip_api_build: true

Pin each action to a full commit SHA, as in the OIDC note. Azure/static-web-apps-deploy has no releases, so take the latest commit on its v1 branch. If you use staticwebapp.config.json (routes, headers), it has to end up in dist/. If a test or the build fails, the job stops and nothing is deployed. For CI and deploy as separate jobs, see the CI/CD note.

For a test copy, add deployment_environment: preview. It gets its own address, <default-host-name>-preview.<location>.azurestaticapps.net, and production stays as it is.

Merge to main or press Run workflow, then open the app's URL.

4. Add the www domain

Add the DNS record first. Azure looks for it when you add the domain.

At your DNS provider, add a CNAME with host www and the app's hostname as the value (no https://).

On Cloudflare: your domain → DNS → Records → Add record:

  • Type: CNAME, Name: www, Target: <app-hostname>.
  • Proxy status: off, so it reads DNS only (gray cloud). Proxied, the name resolves to Cloudflare's addresses, not the app.

Save.

bash
az staticwebapp hostname set --name $SWA --resource-group $RG \
  --hostname www.<domain>   # CNAME validation is the default

5. Add the bare domain

For the bare domain, Azure checks a TXT code instead of a CNAME.

bash
az staticwebapp hostname set --name $SWA --resource-group $RG \
  --hostname <domain> --validation-method dns-txt-token --no-wait

az staticwebapp hostname show --name $SWA --resource-group $RG \
  --hostname <domain> --query validationToken -o tsv   # copy; empty for a minute or so

Then add two records with host @: a TXT with the code, and an ALIAS (or ANAME) with the app's hostname.

On Cloudflare, use a CNAME at @ instead: Cloudflare flattens a CNAME at the apex on every plan. Two more Add records, and you end up with:

TypeNameTarget or ContentProxy status
CNAMEwww<app-hostname>DNS only
CNAME@<app-hostname>DNS only
TXT@the code from Azuren/a

Another provider with neither ALIAS nor flattening? Forward the bare domain to www, or move the domain's DNS to Azure DNS. An A record to the app's stableInboundIP works too, but then traffic goes to one region.

Apex changes can take up to 72 hours to show up everywhere.

6. Set the default domain

Pick the one address people should end up on. Requests to the other domains are redirected to it.

The az staticwebapp commands can't set it. Use the portal.

If your build writes the site URL into canonical links or a sitemap, change it to the new domain and deploy again.

When HTTPS doesn't work

Certificates are free and renew on their own, as long as the domain still resolves to the app.

  • The domain isn't validated yet. In my setup the bare domain took a few hours after the TXT record before its certificate was ready.
  • The record is proxied. Check that both records are still DNS only and the TXT record is still there.
  • The browser remembers the failure. Try a private window.
  • It works on mobile data, not at work. That network is filtering the domain. New domains are often blocked by office firewalls.

Sources