| 1 |
--- |
| 2 |
title: Host on Azure Static Web Apps |
| 3 |
description: Host your project on Azure Static Web Apps. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
aliases: [/hosting-and-deployment/hosting-on-azure-static-web-apps/] |
| 7 |
--- |
| 8 |
|
| 9 |
Use these instructions to enable continuous deployment from a GitHub repository. The same general steps apply for other Git providers such as GitLab or Bitbucket. |
| 10 |
|
| 11 |
{{% include "/_common/gitignore-public.md" %}} |
| 12 |
|
| 13 |
## Prerequisites |
| 14 |
|
| 15 |
Please complete the following tasks before continuing: |
| 16 |
|
| 17 |
1. [Create](https://signup.live.com/) a Microsoft account. |
| 18 |
1. [Create](https://azure.microsoft.com/free/) an Azure account. |
| 19 |
1. [Log in](https://portal.azure.com/) to the Azure Portal. |
| 20 |
1. [Create](https://github.com/signup) a GitHub account. |
| 21 |
1. [Log in](https://github.com/login) to your GitHub account. |
| 22 |
1. [Create](https://github.com/new) a GitHub repository for your project. |
| 23 |
1. [Create](https://git-scm.com/docs/git-init) a local Git repository for your project with a [remote][] reference to your GitHub repository. |
| 24 |
1. Create a Hugo project within your local Git repository and test it with the `hugo server` command. |
| 25 |
1. Commit the changes to your local Git repository and push to your GitHub repository. |
| 26 |
|
| 27 |
## Procedure |
| 28 |
|
| 29 |
Step 1 |
| 30 |
: Create an Azure Static Web App. |
| 31 |
|
| 32 |
1. Go to [Static Web Apps][] in the Azure Portal. |
| 33 |
1. Press the **Create** button. |
| 34 |
|
| 35 |
 |
| 36 |
|
| 37 |
1. Under **Project details**, select your Subscription and choose or create a Resource Group. |
| 38 |
|
| 39 |
 |
| 40 |
|
| 41 |
1. Under **Static Web App details**, enter a Name for your site. |
| 42 |
|
| 43 |
 |
| 44 |
|
| 45 |
1. Under **Hosting plan**, select **Free**. |
| 46 |
|
| 47 |
 |
| 48 |
|
| 49 |
1. Under **Deployment details**, select **Other** as the deployment source, then press the **Review + create** button. This allows deployment using a GitHub Actions token without auto-generating default workflow files. |
| 50 |
|
| 51 |
 |
| 52 |
|
| 53 |
1. Wait for the validation to complete, then press the **Create** button. |
| 54 |
|
| 55 |
 |
| 56 |
|
| 57 |
1. Once the deployment is complete, press the **Go to resource** button. |
| 58 |
|
| 59 |
 |
| 60 |
|
| 61 |
1. Copy the assigned URL to your clipboard. |
| 62 |
|
| 63 |
 |
| 64 |
|
| 65 |
1. In the project configuration file in the root of your local Git repository, set the [`baseURL`][] to the assigned URL as shown below. |
| 66 |
|
| 67 |
{{< code-toggle file=hugo >}} |
| 68 |
baseURL = 'https://salmon-desert-04c512910.4.azurestaticapps.net/' |
| 69 |
locale = 'en-US' |
| 70 |
title = 'Hosting Test - Azure' |
| 71 |
{{< /code-toggle >}} |
| 72 |
|
| 73 |
1. Click the **Manage deployment token** link at the top of the page, and copy the deployment token to your clipboard. |
| 74 |
|
| 75 |
 |
| 76 |
|
| 77 |
Step 2 |
| 78 |
: Add the deployment token to GitHub Secrets. |
| 79 |
|
| 80 |
1. Go to your GitHub repository. |
| 81 |
1. Navigate to **Settings** > **Secrets and variables** > **Actions**. |
| 82 |
1. Click the **New repository secret** button. |
| 83 |
1. Enter `AZURE_STATIC_WEB_APPS_API_TOKEN` for the Name. |
| 84 |
1. Paste the deployment token into the Secret field. |
| 85 |
1. Press the **Add secret** button. |
| 86 |
|
| 87 |
Step 3 |
| 88 |
: Create a `hugo.yaml` file in the `.github/workflows` directory, adjusting the tool versions and time zone as needed. |
| 89 |
|
| 90 |
```yaml {file=".github/workflows/hugo.yaml" copy=true} |
| 91 |
name: Build and deploy |
| 92 |
env: |
| 93 |
# Define tool versions |
| 94 |
DART_SASS_VERSION: 1.104.0 |
| 95 |
GO_VERSION: 1.27.0 |
| 96 |
HUGO_VERSION: 0.166.0 |
| 97 |
NODE_VERSION: 24.20.0 |
| 98 |
|
| 99 |
# Set the build time zone |
| 100 |
TZ: Europe/Oslo |
| 101 |
on: |
| 102 |
push: |
| 103 |
branches: |
| 104 |
- main |
| 105 |
workflow_dispatch: |
| 106 |
permissions: |
| 107 |
contents: read |
| 108 |
concurrency: |
| 109 |
group: deployment |
| 110 |
cancel-in-progress: false |
| 111 |
defaults: |
| 112 |
run: |
| 113 |
shell: bash |
| 114 |
jobs: |
| 115 |
build: |
| 116 |
runs-on: ubuntu-latest |
| 117 |
steps: |
| 118 |
- name: Checkout |
| 119 |
uses: actions/checkout@v7 |
| 120 |
with: |
| 121 |
submodules: recursive |
| 122 |
fetch-depth: 0 |
| 123 |
lfs: false |
| 124 |
|
| 125 |
- name: Create a local tools directory |
| 126 |
run: | |
| 127 |
mkdir -p "${HOME}/.local" |
| 128 |
|
| 129 |
- name: Install Go |
| 130 |
if: hashFiles('go.mod') != '' |
| 131 |
uses: actions/setup-go@v7 |
| 132 |
with: |
| 133 |
go-version: ${{ env.GO_VERSION }} |
| 134 |
cache: false |
| 135 |
|
| 136 |
- name: Install Node.js |
| 137 |
if: hashFiles('package-lock.json') != '' |
| 138 |
uses: actions/setup-node@v7 |
| 139 |
with: |
| 140 |
node-version: ${{ env.NODE_VERSION }} |
| 141 |
|
| 142 |
- name: Install Dart Sass |
| 143 |
run: | |
| 144 |
echo "Installing Dart Sass ${DART_SASS_VERSION}..." |
| 145 |
curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 146 |
tar -C "${HOME}/.local" -xf "${{ runner.temp }}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 147 |
echo "${HOME}/.local/dart-sass" >> "${GITHUB_PATH}" |
| 148 |
|
| 149 |
- name: Install Hugo |
| 150 |
run: | |
| 151 |
echo "Installing Hugo ${HUGO_VERSION}..." |
| 152 |
curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 153 |
mkdir "${HOME}/.local/hugo" |
| 154 |
tar -C "${HOME}/.local/hugo" -xf "${{ runner.temp }}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 155 |
echo "${HOME}/.local/hugo" >> "${GITHUB_PATH}" |
| 156 |
|
| 157 |
- name: Log tool versions |
| 158 |
run: | |
| 159 |
echo "Logging tool versions..." |
| 160 |
command -v sass &> /dev/null && echo "Dart Sass: $(sass --version)" || echo "Dart Sass: not installed" |
| 161 |
command -v go &> /dev/null && echo "Go: $(go version)" || echo "Go: not installed" |
| 162 |
command -v hugo &> /dev/null && echo "Hugo: $(hugo version)" || echo "Hugo: not installed" |
| 163 |
command -v node &> /dev/null && echo "Node.js: $(node --version)" || echo "Node.js: not installed" |
| 164 |
|
| 165 |
- name: Configure Git |
| 166 |
run: | |
| 167 |
echo "Configuring Git..." |
| 168 |
git config --global core.quotepath false |
| 169 |
|
| 170 |
- name: Fetch full Git history |
| 171 |
run: | |
| 172 |
if [[ $(git rev-parse --is-shallow-repository) == true ]]; then |
| 173 |
echo "Fetching full Git history..." |
| 174 |
git fetch --unshallow |
| 175 |
fi |
| 176 |
|
| 177 |
- name: Initialize Git submodules |
| 178 |
run: | |
| 179 |
if [[ -f .gitmodules ]]; then |
| 180 |
echo "Initializing Git submodules..." |
| 181 |
git submodule update --init --recursive |
| 182 |
fi |
| 183 |
|
| 184 |
- name: Install Node.js dependencies |
| 185 |
run: | |
| 186 |
if [[ -f package-lock.json ]]; then |
| 187 |
echo "Installing Node.js dependencies..." |
| 188 |
npm ci |
| 189 |
fi |
| 190 |
|
| 191 |
- name: Cache restore |
| 192 |
id: cache-restore |
| 193 |
uses: actions/cache/restore@v6 |
| 194 |
with: |
| 195 |
path: ${{ runner.temp }}/.cache/hugo |
| 196 |
key: hugo-${{ github.run_id }} |
| 197 |
restore-keys: hugo- |
| 198 |
|
| 199 |
- name: Build |
| 200 |
run: | |
| 201 |
echo "Building the project..." |
| 202 |
hugo build \ |
| 203 |
--gc \ |
| 204 |
--minify \ |
| 205 |
--cacheDir "${{ runner.temp }}/.cache/hugo" |
| 206 |
|
| 207 |
- name: Cache save |
| 208 |
uses: actions/cache/save@v6 |
| 209 |
with: |
| 210 |
path: ${{ runner.temp }}/.cache/hugo |
| 211 |
key: ${{ steps.cache-restore.outputs.cache-primary-key }} |
| 212 |
|
| 213 |
- name: Upload build artifact |
| 214 |
uses: actions/upload-artifact@v7 |
| 215 |
with: |
| 216 |
name: build-artifact |
| 217 |
path: public |
| 218 |
retention-days: 1 |
| 219 |
deploy: |
| 220 |
needs: build |
| 221 |
runs-on: ubuntu-latest |
| 222 |
steps: |
| 223 |
- name: Download build artifact |
| 224 |
uses: actions/download-artifact@v8 |
| 225 |
with: |
| 226 |
name: build-artifact |
| 227 |
path: public |
| 228 |
|
| 229 |
- name: Create Azure Static Web Apps config |
| 230 |
run: | |
| 231 |
cat << 'EOF' > staticwebapp.config.json |
| 232 |
{ |
| 233 |
"responseOverrides": { |
| 234 |
"404": { |
| 235 |
"rewrite": "/404.html", |
| 236 |
"statusCode": 404 |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
EOF |
| 241 |
|
| 242 |
- name: Setup Node.js |
| 243 |
uses: actions/setup-node@v7 |
| 244 |
with: |
| 245 |
node-version: ${{ env.NODE_VERSION }} |
| 246 |
|
| 247 |
- name: Install SWA CLI |
| 248 |
run: npm install -g @azure/static-web-apps-cli --no-fund --no-audit --quiet |
| 249 |
|
| 250 |
- name: Deploy |
| 251 |
env: |
| 252 |
SWA_CLI_DEPLOYMENT_TOKEN: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }} |
| 253 |
run: swa deploy ./public --env production --api-location "" --swa-config-location ./ |
| 254 |
``` |
| 255 |
|
| 256 |
Step 4 |
| 257 |
: In the project configuration file in the root of your local Git repository, set the location of the image cache to the [`cacheDir`][] as shown below. |
| 258 |
|
| 259 |
{{< code-toggle file=hugo copy=true >}} |
| 260 |
[caches.images] |
| 261 |
dir = ':cacheDir/images' |
| 262 |
{{< /code-toggle >}} |
| 263 |
|
| 264 |
See [configure file caches][] for more information. |
| 265 |
|
| 266 |
Step 5 |
| 267 |
: Commit the changes to your local Git repository and push to your GitHub repository. |
| 268 |
|
| 269 |
Step 6 |
| 270 |
: From GitHub's main menu, choose **Actions**. You will see something like this: |
| 271 |
|
| 272 |
 |
| 273 |
|
| 274 |
Step 7 |
| 275 |
: When GitHub has finished building and deploying your site, the color of the status indicator will change to green. |
| 276 |
|
| 277 |
 |
| 278 |
|
| 279 |
In the future, whenever you push a change from your local Git repository, GitHub will rebuild and deploy your site. |
| 280 |
|
| 281 |
## Related resources |
| 282 |
|
| 283 |
For more information on hosting and managing your site with Azure Static Web Apps, consult the official documentation: |
| 284 |
|
| 285 |
- [General documentation][] |
| 286 |
- [Custom domain setup][] |
| 287 |
|
| 288 |
[Custom domain setup]: https://learn.microsoft.com/en-us/azure/static-web-apps/custom-domain-external |
| 289 |
[General documentation]: https://learn.microsoft.com/en-us/azure/static-web-apps/overview |
| 290 |
[Static Web Apps]: https://portal.azure.com/#browse/Microsoft.Web%2FStaticSites |
| 291 |
[`baseURL`]: /configuration/all/#baseurl |
| 292 |
[`cacheDir`]: /configuration/all/#cachedir |
| 293 |
[configure file caches]: /configuration/caches/ |
| 294 |
[remote]: https://git-scm.com/docs/git-remote |