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.
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.
DNSCNAME at @, flattened by Cloudflare, DNS only: points to the app hostname
Static Web AppIt isn't the default domain, so the request is redirected
DNSCNAME www, DNS only: points to the same app hostname
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.
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 hostnameWith no --source, Azure links no repository and adds no workflow.
Search Static Web Apps → Create:
- Resource group: Create new.
- Plan type: Free.
- Deployment details / Source: Other.
Review + create → Create → Go to resource. Copy: the URL on Overview.
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.
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>- App → Overview → Manage deployment token → copy it.
- GitHub repository → Settings → Secrets and variables → Actions → New repository secret: name
AZURE_STATIC_WEB_APPS_API_TOKEN, paste the token → Add secret.
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.
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: truePin 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.
az staticwebapp hostname set --name $SWA --resource-group $RG \
--hostname www.<domain> # CNAME validation is the defaultApp → Custom domains → + Add → Custom domain on other DNS → www.<domain> → Next → Hostname record type: CNAME → Add.
5. Add the bare domain
For the bare domain, Azure checks a TXT code instead of a CNAME.
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 soApp → Custom domains → + Add → Custom domain on other DNS → <domain> → Next → Hostname record type: TXT → Generate code. It takes a minute or so. Copy: the code → Close.
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:
| Type | Name | Target or Content | Proxy status |
|---|---|---|---|
CNAME | www | <app-hostname> | DNS only |
CNAME | @ | <app-hostname> | DNS only |
TXT | @ | the code from Azure | n/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.
App → Custom domains → select www.<domain> → Set default → refresh the table.
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
- Static Web Apps hosting plans (Microsoft Learn)
- Deploy with source "Other" (Microsoft Learn)
- Build configuration (Microsoft Learn)
- Custom domain with external DNS (Microsoft Learn)
- Apex domain with external DNS (Microsoft Learn)
- Set the default domain (Microsoft Learn)
- az staticwebapp (Azure CLI reference)
- CNAME flattening and proxied records (Cloudflare Docs)