Skip to main content

Deploying an Azure Static Web App

Reusable Workflow
WorkflowVersionSource
Deploy Static Site to Azurev1release-azure-staticapp-v1.yaml

This guide provides a complete walkthrough for setting up and deploying a static website to Azure Static Web Apps using Terraform for infrastructure provisioning and GitHub Actions for automated CI/CD.

Overview​

Deploying a static site to Azure Static Web Apps involves two main phases:

  1. Infrastructure Setup: Create the Azure Static Web App resource using Terraform
  2. CI/CD Configuration: Set up automated deployment using GitHub Actions

Prerequisites​

  • An Azure subscription
  • Terraform installed and configured
  • A GitHub repository with your static site code
  • Azure CLI installed (for authentication)

You can set up a fully configured environment using a templated dev container. See Setting Up Dev Containers for details.

Required Permissions and Secrets​

GitHub Actions workflow requires specific permissions and secrets. If you're already using the DX bootstrap module, these are pre-configured aside custom roles for the static web app.

  • Azure Identity Role: Assign PagoPA Static Web Apps List Secrets role
  • Workflow Permissions: Ensure id-token: write, contents: read, pull-requests: write
  • Secrets: Use secrets: inherit or configure Azure authentication secrets

Step 1: Infrastructure Setup with Terraform​

Create the Azure Static Web App resource using Terraform.

Use the official azurerm_static_web_app resource:

resource "azurerm_static_web_app" "example" {
name = provider::azuredx::resource_name(merge(
var.naming_config,
{
name = "website",
resource_type = "static_web_app",
})
)
resource_group_name = var.resource_group_name
location = "westeurope"
sku_size = "Standard"

tags = var.tags
}

Step 2: Configure CI/CD with GitHub Actions​

Use the reusable workflow to automate build and deployment.

The workflow automates the entire deployment process:

  1. Detects your package manager (pnpm, yarn, or npm)
  2. Builds the application using turbo build
  3. Deploys using the official Azure Static Web Apps action
  4. Creates preview environments for pull requests

Workflow Configuration​

Create a workflow file in .github/workflows/:

name: Deploy Static App to Azure

on:
push:
branches:
- main
paths:
- "apps/my-static-app/**"
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main
paths:
- "apps/my-static-app/**"

jobs:
deploy_to_static_web_app:
permissions:
contents: read
id-token: write
# The build job installs the repository dependencies, which may come from
# GitHub Packages.
packages: read
pull-requests: write
uses: pagopa/dx/.github/workflows/release-azure-staticapp-v1.yaml@main
secrets: inherit
with:
workspace_name: "my-static-app" # Matches package.json name
output_dir: "dist" # Build output directory
static_web_app_name: "your-static-web-app-name" # From Terraform
resource_group_name: "your-resource-group-name" # From Terraform
environment: "app-dev"
GitHub Packages permissions

The calling job must grant packages: read so the build job can install dependencies hosted on GitHub Packages. A reusable workflow can only downgrade the permissions granted by its caller: a permissions block without packages sets the scope to none, and the install fails later with 401 when the registry refuses the token.

Optional stale preview cleanup​

Set cleanup_stale_previews: true (default: false) to delete closed or merged PR previews after a successful production deployment. Open PR previews, production, and named environments are preserved; cleanup errors fail the job.

All PR previews must belong to the calling repository. The deployment identity needs Microsoft.Web/staticSites/read, Microsoft.Web/staticSites/builds/read, and Microsoft.Web/staticSites/builds/delete; the list-secrets role alone is insufficient. See the cleanup action for standalone usage.

Additional Notes​