Deploy a Blazor WASM site to Azure Storage using GitHub Actions
Tags: azure, webdev, tutorial, devops
Published: 2019-12-18 21:50:44

---
title: Deploy a Blazor WASM site to Azure Storage using GitHub Actions
published: true
description: Learn how you can deploy your Blazor WebAssembly app to Azure Storage for static site hosting using a GitHub Actions workflow!
tags: azure,webdev,tutorial,devops
canonical_url: https://timheuer.com/blog/deploy-blazor-app-to-azure-using-github-actions/
cover_image: https://storage2.timheuer.com/staticconfig.png
---
<p>I’ve been spending a lot of time looking at the GitHub Actions experience for .NET developers.&nbsp; Right now I’m still using Azure Pipelines for my project, <a href="https://github.com/timheuer/alexa-skills-dotnet">Alexa.NET</a>, in building, testing, and deploying to NuGet.&nbsp; As the tools and process for using DevOps tools for CI/CD have so vastly improved over the years, I’ve become a huge advocate for this being the means for your build/deploy steps…YES, even as a single developer or a smaller team.&nbsp; It simply really helps you get to a purer sense of preserving the ability for your code to live on, for others to accurately build it, and for you to have peace of mind that your code works as intended.&nbsp; I’m just such a huge fan now.&nbsp; That said, I still think there is a place for ‘right-click publish’ activities in inner-loop development.&nbsp; In fact, I use it regularly for a few internal apps I’ve written.&nbsp; For simple solutions that method works well, but I certainly don’t think I can right-click-publish a full solution to a Kubernetes environment though.&nbsp; I’m currently researching new tooling ways to help those ‘publish to CI/CD’ from Visual Studio (would love your opinions here) so I’ve been spending a lot more time in GitHub Actions.&nbsp; I decided to look at publishing a Blazor app to Azure Storage as a static site…here’s what I did.</p>  <h2>Setting up the Storage endpoint</h2>  <p>The first thing you need is an Azure Storage account.&nbsp; Don’t have an Azure account, no worries you can get a <a href="https://azure.com/free">Free Azure account</a> easily which includes up to 5GB of Azure Blob Storage free for the first 12 months.&nbsp; Worried about pricing afterwards?&nbsp; Well check out the <a href="https://azure.microsoft.com/en-us/pricing/calculator/?service=storage#storage91556251-d72c-4e8e-96b8-19ef29b05273">storage pricing calculator</a> and I’m sure you’ll see that even at 1TB storage it is cost-effective means of storage.&nbsp; But any rate, you need a storage account and here are the configuration you need.</p>  <p>First, you may already have one.&nbsp; As a developer do you create your infrastructure resources or are these provisioned for you by infra/devops roles in your company (leave comments)?&nbsp; Earlier this year at Build we enabled <a href="https://docs.microsoft.com/azure/storage/blobs/storage-blob-static-website">static website hosting in Azure Storage</a>.&nbsp; You first create a Storage resource (ensuring you choose v2 which is the default, but that is the version that enables this feature).&nbsp; After you create your resource scroll on the left and you’ll see ‘Static website' section.&nbsp; Here’s what the configuration looks like and let me explain a few areas here:</p>  <p><img title="Screenshot of the Static website configuration" style="border: 0px currentcolor; border-image: none; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="Screenshot of the Static website configuration" src="https://storage2.timheuer.com/staticconfig.png" width="1185" height="792">    <br>All of this configuration is under the Static website area.&nbsp; First you obviously need to enable it…that’s just toggling the enabled/disabled capability.&nbsp; Enabling this now gets you two things: 2 endpoints (basically the URI to the website) and a specific blob contianer named $web where your static content needs to live.&nbsp; The endpoints default map to this blob container without having to add a container name to the root URI.&nbsp; Remember the <strong>resource group </strong>you’ve given to your storage instance here, you will need that later.</p>  <blockquote>   <p>NOTE: You can later add CDN/custom domain to these endpoints, but I’m not covering those here.</p> </blockquote>  <p>The second thing you need is to set a default document and error page.&nbsp; The default document for your SPA is your root entry point, and for most frameworks I’ve seen this is indeed index.html.&nbsp; For Blazor WebAssembly (WASM) apps, this is also the default if you are using the template.&nbsp; So you set the default document as ‘index.html’ and move on.&nbsp; The error document path is another interesting one…you need to set this for SPA apps because right now the static website capability of Azure Storage does not account for custom routing rules.&nbsp; What this means is that storage will throw an HTTP 404 error message when you go to something like /somepage where it actually doesn’t exist but your SPA framework knows how to handle it.&nbsp; Until custom routing works on Azure Storage your error document becomes your route entry point.&nbsp; So for this set the error document path to also be index.html for Blazor WASM.</p>  <blockquote>   <p>NOTE: Yes this isn’t ideal for routing.&nbsp; On top of that it still does show an HTTP 404 actual network message even though your route is being handled.&nbsp; Azure Storage team has heard this request…working on advocating for y’all.</p> </blockquote>  <p>That’s it.&nbsp; Now you have a storage endpoint with a blob container that you can begin putting your content in and browse to using your endpoint URI provided from the portal.&nbsp; For a simple tool to navigate your storage, I’ve been using <a href="https://azure.microsoft.com/features/storage-explorer/">Azure Storage Explorer</a> and it is intuitive to me and works well to quickly navigate your storage account and containers (and supports multi-account!).&nbsp; <br></p>  <h2>Setting up your Azure Service Principal credentials</h2>  <p>The next thing you will need is a service principal credential.&nbsp; This will be used to authenticate with your Azure account to be able to use DevOps tools to work on your behalf in your account.&nbsp; It’s a simple process if you have a standard account.&nbsp; I say this only because I know there might be some configurations for environments where you yourself don’t have access to create service principals and may need someone to create one on your behalf, or also there might be credentials you can already use.&nbsp; Either way here is the process I used.</p>  <p>I used the <a href="https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest">Azure CLI</a> so if you don’t have that installed go ahead and grab that and install it.&nbsp; This should now be in your PATH environment and using your favorite terminal you should be able to start executing commands.&nbsp; To start out, login to the CLI using `az login` – this will launch a browser for you to authenticate via your account and then issue the token in your environment so that for the remainder of your session you’ll be authenticated.&nbsp; After logging in successfully running `az account show` will emit what subscription you are using and you’ll need the subscription ID later so grab that and put it somewhere on your scratch notepad for later command usage.</p>  <blockquote>   <p>NOTE: If you have more than one subscription and have not set a default subscription you should set that using the `<a href="https://docs.microsoft.com/cli/azure/manage-azure-subscriptions-azure-cli?view=azure-cli-latest">az account set</a>` command.</p> </blockquote>  <p>Now you can use the CLI to create a new service principal.&nbsp; To do that issue this command:</p>  <pre class="brush: bash; toolbar: false; highlight: [2];">az ad sp create-for-rbac --name "myApp" --role contributor \
                            --scopes /subscriptions/{subscription-id}/resourceGroups/{resource-group} \
                            --sdk-auth
</pre>

<p>Note on line 2 here that you need to replace {subscription-id} with your own actual subscription id (the GUID) and {resource-group} with the resource group name where your storage account is located.&nbsp; On line 1 the “myApp” can be anything but I recommend making it meaningful as this is basically the account name of the principal.&nbsp; The output of this command is your service principal.&nbsp; The full JSON output.&nbsp; Save this off in a place for now as we’ll need that later to configure GitHub Actions properly.&nbsp; Great now to move on to the app!</p>

<h2>Create your Blazor WASM app</h2>

<p>I assume since you may be reading this far you aren’t new to Blazor and probably already have the tools.&nbsp; As of this writing, Blazor WASM is still in preview so you have to install the templates separately to acquire them to show up in `dotnet new` and in Visual Studio File…New Project.&nbsp; To do that from a terminal run:</p>

<pre class="brush: bash; toolbar: false;">dotnet new -i Microsoft.AspNetCore.Blazor.Templates::3.1.0-preview4.19579.2
</pre>

<p>Then you will be able to create a new project.&nbsp; I’m showing Visual Studio here and this is the WASM template:</p>

<p><img title="Screenshot of Blazor new project dialog" style="border: 0px currentcolor; border-image: none; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="Screenshot of Blazor new project dialog" src="https://storage2.timheuer.com/blazornpd.png" width="1536" height="1056"></p>

<p>In the dialog here you will see Blazor WebAssembly App and that’s what you will use.&nbsp; Now you have a choice to have it ASP.NET Core hosted using that checkbox, which if you were going to do other things in ASP.NET maybe that’s what you want.&nbsp; For the purposes of this article we are talking about just having the WASM app and having a place to host it that isn’t a web server with other content…just hosting static content and using storage to do so…so we aren’t checking that box.&nbsp; The result will be a Blazor WASM app with no host.&nbsp; Now let’s add that to GitHub.&nbsp; If you are using Visual Studio 16.4+ you’ll be able to take advantage of an improved flow for pushing to GitHub.&nbsp; Once you have your project, in the lower right click ‘Add to Source Control’ choosing Git and then you’ll see the panel to choose GitHub and create/push a repo right away.&nbsp; You don’t have to go to GitHub site first and clone later…all in one step:</p>

<p><img title="Animation of Visual Studio GitHub flow" style="border: 0px currentcolor; border-image: none; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="Animation of Visual Studio GitHub flow" src="https://storage2.timheuer.com/newgitflow.gif" width="1048" height="1074"></p>

<p>Great!&nbsp; Now we have our WASM project and we’ve created and pushed the current bits to a new GitHub repository.&nbsp; Now to create the workflow.</p>

<h2>Setup the GitHub Action Workflow</h2>

<p>Now we’ve got an Azure Storage blob container, a service principal, a Blazor WASM project, and a GitHub repository…all set to configure the CI/CD flow now.&nbsp; First let’s put that service principal as a secret in our repository.&nbsp; In the settings of your repository navigate to the Secrets section and add a secret named AZURE_CREDENTIALS.&nbsp; The content of this is the full content of your service principal (the JSON blob) that we generated earlier:</p>

<p><img title="Screenshot of GitHub Secrets configuration" style="border: 0px currentcolor; border-image: none; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="Screenshot of GitHub Secrets configuration" src="https://storage2.timheuer.com/githubsecretconfig.png" width="1133" height="1017"></p>

<p>This saves the secret for us to use in the workflow and reference as a variable.&nbsp; You can add more secrets here if you’d like if you wanted to add your resource storage account name as well (probably a good idea).&nbsp; Secrets are isolated to the original repository so no forks get the secrets at all.&nbsp; Now that we have these let’s create the workflow file.</p>

<p>Today, Visual Studio isn’t too helpful in authoring the YAML files (would love your feedback here too!) but a GitHub Action is just a YAML file in a specific location in your repository: .github/workflows/azure-storage-deploy.yaml.&nbsp; The file name can be anything but putting it in this folder structure is what is required.&nbsp; You can start in the GitHub repo itself using the Actions tab and through the online editor get <em>some</em> level of completion assistance to help you navigate the YAML editing.&nbsp; Go to the Actions tab in your repository and create a new workflow.&nbsp; You’ll be offered a starter workflow based on what GitHub thinks your project is like.&nbsp; As of this writing it thinks Blazor apps are Jekyll workflows so you’ll need to expand and either find the .NET Core one or just start from a blank workflow yourself.</p>

<p><img title="Screenshot of GitHub Actions config" style="border: 0px currentcolor; border-image: none; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="Screenshot of GitHub Actions config" src="https://storage2.timheuer.com/workflowstarter.png" width="1910" height="751"></p>

<p>Four my workflow I want to build, publish and deploy my app.&nbsp; I’ve separated it into a build and deploy jobs.&nbsp; You can read all about the various aspects of <a href="https://github.com/features/actions">GitHub Actions in their docs</a> with regard to jobs and other syntax as I won’t try to expound upon that in this article.&nbsp; Here is my full YAML for the entire workflow with some key areas highlighted:</p>



<pre class="brush: yaml; toolbar: false; highlight: [5,6,7,26,27,28,29,30,39,40,41,42,45,48,51,61];">name: .NET Core Build and Deploy

on: [push]

env:
  AZURE_RESOURCE_GROUP: blazor-deployment-samples
  BLOB_STORAGE_ACCOUNT_NAME: timheuerblazorwasm

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.100

    - name: Build with dotnet
      run: dotnet build --configuration Release
    
    - name: Publish with dotnet
      run: dotnet publish --configuration Release 
    
    - name: Publish artifacts
      uses: actions/upload-artifact@master
      with:
        name: webapp
        path: bin/Release/netstandard2.1/publish/BlazorApp27/dist

  deploy:
    needs: build
    name: Deploy
    runs-on: ubuntu-latest
    steps:

    # Download artifacts
    - name: Download artifacts
      uses: actions/download-artifact@master
      with:
        name: webapp

    # Authentication
    - name: Authenticate with Azure
      uses: azure/login@v1
      with:
        creds: `${{ secrets.AZURE_CREDENTIALS  }}`

    # Deploy to storage using CLI
    - name: Deploy to storage using CLI
      uses: azure/CLI@v1
      with:
        azcliversion: latest
        inlineScript: | 
          # show azure account being used
          az account show
          # az storage account upload
          az storage blob upload-batch --account-name `${{ env.BLOB_STORAGE_ACCOUNT_NAME }}` -s webapp -d \$web
          # az set content type of wasm file until this is fixed by default from Azure Storage
          az storage blob update --account-name `${{ env.BLOB_STORAGE_ACCOUNT_NAME }}` -c \$web -n _framework/wasm/mono.wasm --content-type application/wasm

</pre>



<p>So a few things going on here, let’s talk about them.</p>

<ul>
  <li>Lines 5-7: these are ‘local’ environment variables I set up.&nbsp; The storage account name is NOT the blob container name but the actual storage account name.&nbsp; This ideally probably should be a Secret as mentioned above.&nbsp; Environment variables can be set here and then placeholders reference them later.</li>

  <li>Starting at line 9 is where the ‘build’ portion is here.&nbsp; We checkout the code, acquire the SDK and run the build and publish commands.&nbsp; On line 26-30 is a step where we put the publish output to a specific artifact location for later retrieval of steps.&nbsp; This is good practice.</li>

  <li>Lines 40-42 is where we are now in the ‘deploy’ step of our CD and we retrieve those artifacts we previously pushed and we set them as a name ‘webapp’ that the later will use in deployment</li>

  <li>Line 45 is where we are going to first authenticate to Azure using our service principal retrieved from the Secrets.&nbsp; The ‘secrets’ object is not something you have to define and is part of the workflow so you just add the property you want to retrieve</li>

  <li>Line 51 is where we start the deployment to Azure using the CLI commands and our param ‘webapp’ as the source.&nbsp; This is the CLI command for uploading batch to storage as described in the docs for `<a href="https://docs.microsoft.com/cli/azure/storage/blob?view=azure-cli-latest#az-storage-blob-upload-batch">az storage blob upload-batch</a>`</li>

  <li>Line 61 is an additional step that we need for .wasm files.&nbsp; I believe this to be a bug because there is logic in the CLI to correctly map the content-type but for some reason it is not working…so for now you need to set the content-type for .wasm to `application/wasm` or the Blazor app will not work</li>
</ul>

<p>This is made possible through a series of actions: checkout, dotnetcore, azure…all brining their functionality we can draw on and configure.&nbsp; There are a <a href="https://azure.microsoft.com/blog/github-actions-for-azure-is-now-generally-available/">bunch of Azure GitHub Actions</a> we just released for specific tasks like deploying to App Service and such.&nbsp; These don’t require CLI commands but rather just provide parameters to configure.&nbsp; Because there is no Storage specific Action as of now, we can use the default CLI action to script what we want.&nbsp; It is an enabler in lieu of a more strongly-typed action.&nbsp; Now that we have this workflow YAML file complete we can commit and push to the repository.&nbsp; In doing that we now have a CI/CD action that will trigger on any push (because that’s how we configured it).&nbsp; We can see this action happening in my sample repo and you can see since we separate it in two jobs it will show them separately:</p>

<p><img title="Screenshot of action deployment log" style="border: 0px currentcolor; border-image: none; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="Screenshot of action deployment log" src="https://storage2.timheuer.com/deploylogs.png" width="1564" height="1063"></p>

<h2>Summary</h2>

<p>So now we have it complete end-to-end.&nbsp; And subsequent check-in will trigger the workflow and deploy the bits to my storage account and I can now use my Azure Storage account as a host for my static website built on Blazor WASM.&nbsp; This full YAML sample flow is available on my repo for this and you can examine it in more detail.</p>

<p>I would love to know how y’all are coming along using GitHub Actions with your .NET projects.&nbsp; Please comment below!&nbsp; </p>

<p><br/><br/></p><p>(this article <a href="https://timheuer.com/blog/deploy-blazor-app-to-azure-using-github-actions/">cross-posted from timheuer.com</a>)</p>