Using GitHub Actions to deploy a .Net Web App

So I wanted to take a look into GitHub Action’s and deploy a .Net Web App, this is my first real look into them and will be blogging about them a lot more in the future, below are the steps I took to do this.

  • Create a basic .Net MVC Web App using .Net Core 3.1
  • Run the app and make sure all is working as expected.
  • Deploy this to Azure web app to Azure and check it runs ok.
  • I then go into my Resource Group, locate the app service and from the click on ‘Get publish profile‘ – download the file and keep handy.
  • Then I added the code to GitHub in a new repo which you can find here: – https://github.com/gsuttie/GHActionsWebApp1
  • Next, I need to create a secret from within GitHub, so once you have your code in a new repository within Github go click on Settings and then Security and click Add a new secret, call it azureWebAppPublishProfile if you want to use the YAML below.
  • Then I click on Actions and create my first workflow, I chose, Setup a workflow yourself.
  • And then from here – https://github.com/actions/setup-dotnet I used the YAML and pasted in so that my main.yaml file looks like this
name: Deploy ASP.NET Core app to Azure Web App

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - '*'
# CONFIGURATION
# For help, go to https://github.com/Azure/Actions
#
# 1. Set up the following secrets in your repository:
#   AZURE_WEBAPP_PUBLISH_PROFILE
#
# 2. Change these variables for your configuration:
env:
  AZURE_WEBAPP_NAME: dummywebsite     # set this to your application's name
  AZURE_WEBAPP_PACKAGE_PATH: '.'      # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '3.1.100'           # set this to the dot net version to use

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:

      # Checkout the repo
      - uses: actions/checkout@master
      
      # Setup .NET Core SDK
      - name: Setup .NET Core
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }} 
      
      # Run dotnet build and publish
      - name: dotnet build and publish
        run: |
          dotnet build --configuration Release
          dotnet publish -c Release -o '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp' 
          
      # Deploy to Azure Web apps
      - name: 'Run Azure webapp deploy action using publish profile credentials'
        uses: azure/webapps-deploy@v2
        with: 
          app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name
          publish-profile: ${{ secrets.azureWebAppPublishProfile }} # Define secret variable in repository settings as per action documentation
          package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp'

# For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples
  • If you plan to use the Yaml from the above, mind and change the one setting which is AZURE_WEBAPP_NAME – I set this to dummywebsite so rename that to whatever your app service is called.
  • Save that, and then start your new GitHub workflow.
  • If all is well then your good to go, push a change to the web app and it should build and deploy automatically using your new GitHub Action.