If youβve ever wanted to deploy a Blazor WebAssembly app to GitHub Pages, GitHub Actions makes the process straightforward and automated.
In this guide, Iβll walk you through creating a Blazor WebAssembly app, setting up the GitHub Action workflow, and triggering your deployment.
π Live demo: https://reggieray.github.io/blazor-wasm/
First, create a new Blazor WebAssembly project:
dotnet new blazorwasm -o Blazor.Wasm
Here, Blazor.Wasm
is the name of your project folder.
Once itβs created, commit your changes and push the project to your GitHub repository.
Weβll now configure a GitHub Actions workflow to publish our app to GitHub Pages automatically.
π Helpful references:
π Workflow file: .github/workflows/static.yml
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages
env:
PUBLISH_DIR: src/Blazor.Wasm/bin/Release/net9.0/publish/wwwroot # π update project name
on:
push:
paths:
- src/Blazor.Wasm/** # π update project name
branches:
- main
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4.2.2
- name: Get latest .NET SDK
uses: actions/setup-dotnet@v4.3.0
with:
dotnet-version: '9.0'
- name: Publish app
run: dotnet publish src/Blazor.Wasm -c Release # π update project name
- name: Rewrite base href
uses: SteveSandersonMS/ghaction-rewrite-base-href@v1.1.0
with:
html_path: ${{ env.PUBLISH_DIR }}/index.html
base_href: /blazor-wasm/ # π update to your repo name
- name: Setup Pages
uses: actions/configure-pages@v5.0.0
- name: Upload artifact
uses: actions/upload-pages-artifact@v3.0.1
with:
path: ${{ env.PUBLISH_DIR }}
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4.0.5
There are two ways to kick off your first deployment:
main
Make any change to your Blazor app and push it to the main
branch. The workflow trigger looks like this:
on:
push:
paths:
- src/Blazor.Wasm/** # π update project name
branches:
- main
From your repository:
β Now every push to your Blazor WebAssembly projectβs main branch will automatically publish to GitHub Pages.
You get:
π Try it now and see your Blazor app live in minutes!