Deploying Azure WebApps in 2020 with GitHub Actions
Tags: dotnet, devops, github, csharp
Published: 2020-05-03 12:42:19


For the longest time, I've relied on, and recommended Azure's Kudu deployment engine as the simplest and most effective way to deploy web apps into Azure App services on Windows. Sadly, over the last couple of years, and after it's original author changed roles, Kudu has lagged behind .NET Core SDK versions, meaning if you want to run the latest versions of .NET Core for your webapp, Kudu can't build and deploy them.

We don't want to be trapped on prior versions of the framework, and luckily GitHub actions can successfully fill the gap left behind by Kudu without much additional effort.

Let's walk through creating and deploying an ASP Net Core 3.1 web app, without containerisation, to Azure App services and GitHub in 2020.

*This entire process will take less than 5-10 minutes to complete the first time, and once you understand the mechanisms, should be trivial for any future projects.*

## Create Your WebApp

* Create a new repository on GitHub.
* Clone it to your local machine.
* Create a new Visual Studio Solution
* Create a new ASP.NET Core 3.1 MVC WebApplication

## Create a new Azure App Service Web App

* Visit the Azure Portal
* Create a new web application
* Select your subscription and resource group

**Instance details**

Because we're talking about replacing Kudu for building our software - the Windows native deployment engine in AppServices, we're going to deploy just our code to Windows. It's worth noting that GitHub actions can also be used for Linux deployments, and Containerised applications, but this walkthrough is intended as a like-for-like example for Windows-on-Azure users.

* Give it a name
* Select `code` from the publish options
* Select `.NET Core 3.1 (LTS) as the runtime stack
* Select `Windows` as the operating system
* Select your Region
* Select your App service plan

## Configure it's deployment

Now we're going to link our GitHub repository, to our AppServices Web app.

* Visit the Deployment Centre in for your newly created application in the Azure Portal.
* Select GitHub actions (preview)
* Authorise your account
* Select your repository from the list

A preview of a GitHub action will be generated and displayed on the screen, click continue to have it automatically added to your repository.

When you confirm this step, a commit will be added to your repository on GitHub with this template stored in `.github/workflows` as a .yml file.

## Correct any required file paths

Depending on where you created your code, you might notice that your GitHub action fails by default. This is because the default template just calls `dotnet build` and `dotnet publish`, and if your projects are in some (probably sane) location like `/src` the command won't be able to find your web app by default.

Let's correct this now:

* Git pull your repository
* Customise generated .yml file in `.github/workflows` to make sure it's paths are correct.

In the sample I created for this walkthrough, the build and publish steps are changed to the following:

    - name: Build with dotnet
      run: dotnet build ANCWebsite.sln --configuration Release

    - name: dotnet publish
      run: dotnet publish src/ANCWebsite/ANCWebsite.csproj -c Release -o ${{env.DOTNET_ROOT}}/myapp
      
Note the explicit solution and CSProj paths. Commit your changes and Git push.

## Browse to your site!

You can now browse to your site and it works! 
Your deployments will show up both in the Azure Deployment Center and the GitHub Actions list.

This whole process takes less than ~2-3 minutes to setup, and is reliable and recommended.
A reasonable replacement for the similar Kudu "git pull deploy" build that worked for years.

## How it works under the hood

This is all powered by three things:

* An Azure deployment profile
* A .yml file added to .github/workflows
* A GitHub secret

As you click through the Azure Deployment center setup process, it does the following:

* Adds a copy of the templated GitHub Actions Dot Net Core .yml deployment file from https://github.com/Azure/webapps-deploy to your repository
* Downloads the Azure publishing profile for your newly created Website and adds it as a GitHub secret to your GitHub repositories "Secrets" setting.
* Makes sure the name of the secret referenced in .github/workflows/projectname_branchname.yml matches the name of the secret it added to the respository.

The rest is taken care of by post-commit hooks and GitHub actions automatically.

You can set this entire build pipeline up yourself by creating the .yml file by hand, and adding your secret by hand.
You can download the content for your Publish profile by visiting 

    Deployment Center -> Deployment Credentials -> Get Publish Profile
    
In the Azure portal.

It's an XML blob that you can paste into the GitHub UI, but honestly, you may aswell let Azure do the setup for you.

## Next Steps

You've just built a simple CI pipeline that deploys to Azure WebApps without any of the overhead of k8s, docker, or third party build systems.

Some things you can now do:

* Consider running your tests as part of this build pipeline by adding a step to call `dotnet test`
* Add an additional branch specification to deploy test builds to different deployment slots

The nice thing about this approach, is that your build runs in a container on GitHub actions, so you can always make sure you're using the versions of tools and SDKs that you desire.

You can find all the code used in this walkthrough [here](https://github.com/davidwhitney/ANCWebsite).