Getting started with Azure Static Web Apps

June 30, 2020
Development, Microsoft Azure, DevOps, GitHub
microsoft azure, azure cli, azure static web app, devops, github actions, hugo

Introduction #

In the last article, I talked about how to deploy a Hugo static site to Azure storage and CDN using GitHub Actions. Setting up that pipeline however had many steps, and could perhaps be considered a bit abstruse.

With the advent of the new Static Web App service, Microsoft has now significantly simplified the same solution. Within a few clicks on Azure Portal, your web app (in a GitHub repository) can be automatically configured for CI/CD via GitHub Actions and published to Azure. The impressive part of the process is that the included GitHub Actions workflow is configured with Microsoft Oryx - a build tool which automatically detects your project type and runs the suitable build command(s). You can have your React, Vue, Angular, Hugo and Gatsby projects etc. built automatically with little to no added build configs.

The Azure Static Web App service supports caching by default, distributing your site’s assets to your users’ geographic location for faster loading times. Additional features include Integrated API support via Azure Functions, custom domain name and authentication support. You can find the list of supported features here.

Prerequisites #

To get started:

  • You’ll need an Azure subscription. You can setup a Trial Account.
  • A static web app in a repository on GitHub. You can fork/clone this sample Hugo project.

Steps #

You can create a Static Web App via the Azure Portal or Azure CLI in Azure Cloud Shell / your local development machine. We’ll go over the two methods starting with the portal.

Create a Static Web App using Azure Portal #

  1. Go to Azure Portal.

  2. Navigate to Static Web Apps (Preview) and Click Add.

    Add new Static Web App
  3. Select an existing Resource Group, or click Create new to create a new one.

    Complete new Static Web App wizard
  4. Provide a Name for your static web app.

  5. Select the closest Region to you.

    Only Free SKU is currently available, likely because the service is in preview.
  6. Click Sign in with GitHub.

  7. Select your Organization (sometimes the name of your GitHub account).

    Complete new Static Web App wizard
  8. Select your Repository.

  9. Select your main Branch (e.g. master).

  10. Click Next : Build > to go to the Build tab.

  11. Specify App location (this is the path of your application code).

    Complete new Static Web App wizard
  12. If you have an API (Azure Functions) in the repository, enter the path of the API for Api Location. Otherwise, leave the default api value.

  13. Specify App artifact location (e.g public for sample Hugo project).

  14. Click Review + Create.

  15. Click Create.

  16. Navigate back to Static Web Apps page and select your new web app.

    It may take a few seconds for new apps to appear. Page may need to refreshed a few times.
  17. Click Browse or the link under URL field to view the published web application.

    Link to published app
  18. Also click on GitHub Action runs to view the build status. The link will take you to GitHub.

    View GitHub Actions job
  19. Fin.

Create a Static Web App using Azure CLI #

Before you start, you need to create a GitHub Personal Access Token.

  1. Open GitHub, and go to Settings.

    Go to GitHub Settings
  2. Navigate to Developer settings > Personal access tokens > Generate new token.

    Go to Personal access tokens
  3. Complete the Note field, tick/check the repo and workflow options.

    Create Personal access tokens
  4. Click Generate token.

    It is important to apply the Principle of Least Privilege when creating tokens and access credentials. Only select the roles/permissions needed for a task.
  5. Copy the generated token.

    Copy Personal access tokens

With the token now provisioned, you can proceed to execute the CLI commands.

  1. Navigate Azure Cloud Shell or install Azure CLI on your local development machine.

  2. Login to your Azure account if you haven’t yet. This step is not required when using Azure Cloud Shell because you’ll be automatically authenticated on Azure Portal.

    az login
    
  3. Create new Resource Group. Skip this step if you want to deploy to an existing resource group.

    az group create -l <region> -n <resource-group>
    

    Where:

    ArgumentDetails
    <region>Deployment region (e.g. WestEurope, WestUS, WestUS2).
    <resource-group>The name of your new resource group.
  4. Run this command to create a new Static Web App:

    az staticwebapp create -n <static-app-name> -g <resource-group> -s <github-repository-url> -l <region> -b <branch> --app-artifact-location <artifact-location> --app-location <app-location> --api-location <api-location> --token <token>
    

    Where:

    ArgumentDetails
    <static-app-name>Name of your new static web app.
    <resource-group>Your resource group name.
    <github-repository-url>URL of your project on GitHub
    (e.g. https://github.com/usmanmohammed/hugo-quickstart).
    <region>Deployment region (e.g. WestEurope, WestUS, WestUS2).
    <branch>Name of your main branch (e.g. master).
    <artifact-location>Path of your app artifacts (eg. public for a Hugo project).
    The full list of paths can be found here.
    <app-location>Path of your application code. Default is '/'.
    <api-location>Path of your API if exists in repository. Default value is api.
    <token>Your GitHub personal access token.

    The result of the command when executed should be similar to this:

    Command group 'staticwebapp' is in preview. It may be changed/removed in a future release.
    {
      "branch": "master",
      "buildProperties": null,
      "customDomains": [],
      "defaultHostname": "calm-smoke-03f632e03.azurestaticapps.net",
      "id": "<id>",
      "kind": null,
      "location": "West Europe",
      "name": "<static-app-name>",
      "repositoryToken": null,
      "repositoryUrl": "https://github.com/usmanmohammed/hugo-quickstart",
      "resourceGroup": "<resource-group>",
      "sku": {
        "capabilities": null,
        "capacity": null,
        "family": null,
        "locations": null,
        "name": "Free",
        "size": null,
        "skuCapacity": null,
        "tier": "Free"
      },
      "tags": null,
      "type": "Microsoft.Web/staticSites"
    }
    
  5. Copy the value of defaultHostname (e.g. calm-smoke-03f632e03.azurestaticapps.net) and paste in your browser. This is the URL of your published app.

  6. You can also view the deployment status by continuing from Step 16 from the GUI method.

  7. Fin.

Closing Remarks #

Developers have been asking for a simpler solution for deploying static web apps on Azure and this seems to be it. While there are limited features so far and still some bugs to be squashed (as of the time this article was published), it’s impressive to see how easy deploying static apps can be.

Azure Static Web App service has no paid pricing plan yet. It will be hard to imagine the service costing more than the cheapest configuration of a traditional Web app service. Perhaps Microsoft may let everyone to enjoy it for free post-preview.

More features are expected soon, and I’m really looking forward Application Insights integration. Let me know in the comments section below what features you’d love to see.

Further Reading #

comments powered by Disqus