How to deploy Blazor Webassembly on GitHub Pages using GitHub Actions
I have been spending quite some time lately playing with Blazor. One of the nice things is that with webassembly you can generate a static website and have it hosted on GitHub Pages for free.
Most importantly, the whole process can be automated with GitHub Actions so you don’t have to worry about it.
It’s not a complicated process, all in all just a few steps. There is only one caveat: if your repository is a “standard” User or Organization repository, you can deploy to Pages only from the master branch.
From the docs:
The default publishing source for user and organization sites is the
masterbranch. If the repository for your user or organization site has a
masterbranch, your site will publish automatically from that branch. You cannot choose a different publishing source for user or organization sites.
Otherwise, if your repository belongs to a Project, you can configure it to deploy from a different branch:
The default publishing source for a project site is the
gh-pagesbranch. If the repository for your project site has a
gh-pagesbranch, your site will publish automatically from that branch.
Project sites can also be published from the
masterbranch or a
/docsfolder on the
So what do we have to do in order to see our nice website? The core it’s all here:
name: gh-pages on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Setup .NET Core uses: actions/setup-dotnet@v1 with: dotnet-version: 3.1.301 - name: Publish with dotnet run: dotnet publish --configuration Release --output build - name: Deploy to Github Pages uses: JamesIves/github-pages-deploy-action@releases/v3 with: ACCESS_TOKEN: $ BASE_BRANCH: development # The branch the action should deploy from. BRANCH: master # The branch the action should deploy to. FOLDER: build/wwwroot # The folder the action should deploy. SINGLE_COMMIT: true
This GitHub Action workflow is doing all the grunt work for us:
- setup .NET Core
- build the application and publish it to the
- uses JamesIves/github-pages-deploy-action to deploy the code to the
As you can see from the comment, this code helps to handle URLs and redirections.
You’ll also have to update the
<base href="/BlazorOnGitHubPages/" />
So if the name is BlazorOnGitHubPages, the final URL will be something like https://mizrael.github.io/BlazorOnGitHubPages/ (which happens to be the one I’m using, feel free to try it).
The next step is to add a 404.html page and an empty .nojekyll file in the /wwwroot folder. GitHub Pages are built using Jekyll and it does not build anything that starts with _ . Blazor, however, generates a _framework subfolder inside /wwwroot and as you can imagine, it’s quite important.