DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Effective Java Collection Framework: Best Practices and Tips
  • Operator Overloading in Java
  • Automating the Migration From JS to TS for the ZK Framework
  • SRE vs. DevOps

Trending

  • Effective Java Collection Framework: Best Practices and Tips
  • Operator Overloading in Java
  • Automating the Migration From JS to TS for the ZK Framework
  • SRE vs. DevOps
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Deploy Blazor WebAssembly on GitHub Pages Using GitHub Actions

How to Deploy Blazor WebAssembly on GitHub Pages Using GitHub Actions

With Blazor webassembly you can generate a static website and deploy it on GitHub Pages through GitHub Actions. Find out how!

David Guida user avatar by
David Guida
CORE ·
Sep. 01, 20 · Tutorial
Like (2)
Save
Tweet
Share
6.48K Views

Join the DZone community and get the full member experience.

Join For Free

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 master branch. If the repository for your user or organization site has a master branch, 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-pages branch. If the repository for your project site has a gh-pages branch, your site will publish automatically from that branch.

Project sites can also be published from the master branch or a /docs folder on the master branch.

So what do we have to do in order to see our nice website? The core’s all here:

YAML
 




x
25


 
1
name: gh-pages
2

          
3
on: [push]
4

          
5
jobs:
6
  build:
7

          
8
    runs-on: ubuntu-latest
9

          
10
    steps:
11
    - uses: actions/checkout@v2
12
    - name: Setup .NET Core
13
      uses: actions/setup-dotnet@v1
14
      with:
15
        dotnet-version: 3.1.301
16
    - name: Publish with dotnet
17
      run: dotnet publish --configuration Release --output build
18
    - name: Deploy to Github Pages
19
      uses: JamesIves/github-pages-deploy-action@releases/v3
20
      with:
21
        ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
22
        BASE_BRANCH: development # The branch the action should deploy from.
23
        BRANCH: master # The branch the action should deploy to.
24
        FOLDER: build/wwwroot # The folder the action should deploy.
25
        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 /build/ folder
  • Uses JamesIves/github-pages-deploy-action to deploy the code to the master branch

There are also few things to add to the application code as well. First of all, add this Javascript snippet to your index.html:

HTML
 




xxxxxxxxxx
1
31


 
1
<!-- Start Single Page Apps for GitHub Pages -->
2
    <script type="text/javascript">
3
        // Single Page Apps for GitHub Pages
4
        // https://github.com/rafrex/spa-github-pages
5
        // Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License
6
        // ----------------------------------------------------------------------
7
        // This script checks to see if a redirect is present in the query string
8
        // and converts it back into the correct url and adds it to the
9
        // browser's history using window.history.replaceState(...),
10
        // which won't cause the browser to attempt to load the new url.
11
        // When the single page app is loaded further down in this file,
12
        // the correct url will be waiting in the browser's history for
13
        // the single page app to route accordingly.
14
        (function (l) {
15
            if (l.search) {
16
                var q = {};
17
                l.search.slice(1).split('&').forEach(function (v) {
18
                    var a = v.split('=');
19
                    q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&');
20
                });
21
                if (q.p !== undefined) {
22
                    window.history.replaceState(null, null,
23
                        l.pathname.slice(0, -1) + (q.p || '') +
24
                        (q.q ? ('?' + q.q) : '') +
25
                        l.hash
26
                    );
27
                }
28
            }
29
        }(window.location))
30
    </script>
31
    <!-- End Single Page Apps for GitHub Pages -->



As you can see from the comment, this code helps to handle URLs and redirections.

You’ll also have to update the <base> tag with the repository name:

HTML
 




xxxxxxxxxx
1


 
1
<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.

That's all for today. And if you're into gamedev, don't miss my Blazor Series

GitHub Blazor Branch (computer science) WebAssembly

Opinions expressed by DZone contributors are their own.

Trending

  • Effective Java Collection Framework: Best Practices and Tips
  • Operator Overloading in Java
  • Automating the Migration From JS to TS for the ZK Framework
  • SRE vs. DevOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: