Wednesday, 11 November 2020

Deploying .NET 5 Web App via GitHub Actions to Azure App Service

.NET 5 is released recently and now you can develop applications using .NET. While releasing .NET five with zero delays Azure App Services started supporting .NET 5 with Early Access. This is helping us to deploy our application code, without having to deply them as self-contained with framework. Meaning we can now deploy our .NET 5 Web Apps, APIs or Function Apps to Azure, just as published project binaries and things will start to run without any issues. In this post let’s look at how we can setup a quick build deployment pipeline deploying a .NET 5 web application to Azure using GitHub Actions.

You can create .NET 5 Early Access enabled app service app in Azure using the Azure portal or by utilizing the latest version of Azure CLI (2.15.1) as described here. In the portal you are allowed to select the runtime as .NET 5 as shown below.


Using CLI to create .NET 5 Web App is explained in the post here.

Once you have created the app service app in Azure you can download the publish profile and copy it is content and store it as a secret in GitHub secrets.



To create the .NET 5 web app you can use below command and then commit the code to the GitHub repo.

dotnet new webapp -f net5.0 --name mynet5app


Let’s create a simple GitHub Action workflow which can build publish and deploy the .NET 5 web app to Azure.

For this demo the trigger for the workflow is set as workflow_dispatch letting it to be triggered manually.

on: [workflow_dispatch]
name: Net5BuildDeployGitHubHostedWindowsRunner


Then the job is setup to build and deploy and the Windows latest GitHub hosted runner is used for the job.

jobs:
  build-and-deploy:
    runs-on: windows-latest


First step is to checkout the repo.

    steps:
    - uses: actions/checkout@master


Then the .NET SDK should be set to use the latest .NET 5 SDK. Information on available SDKs can be found here.

    - name: Set up .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '5.0.100'


Next, we can build the web app using dotnet build command.

    - name: Build with dotnet
      run: dotnet build  .\mynet5app\mynet5app.csproj --configuration Release


As the next step, we have to publish the built project. env.DOTNET_ROOT here is referring .Net run time location. You can see more information here.

    - name: dotnet publish
      run: dotnet publish .\mynet5app\mynet5app.csproj -c Release -o ${{env.DOTNET_ROOT}}/myapp --no-build --no-restore


The published .NET 5 project then can be deployed to Azure with webapps-deploy action.

    - name: Deploy to Azure Web App
      uses: azure/webapps-deploy@v1
      with:
        app-name: 'app-githubact-demo'
        slot-name: 'production'
        publish-profile: ${{ secrets.MYNET5WEBAPPPUBLISHPROFILE }}
        package: ${{env.DOTNET_ROOT}}/myapp 


The full workflow implementation as below.

on: [workflow_dispatch]
name: Net5BuildDeployGitHubHostedWindowsRunner
 
jobs:
  build-and-deploy:
    runs-on: windows-latest
 
    steps:
    - uses: actions/checkout@master
 
    - name: Set up .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '5.0.100'
 
    - name: Build with dotnet
      run: dotnet build  .\mynet5app\mynet5app.csproj --configuration Release
 
    - name: dotnet publish
      run: dotnet publish .\mynet5app\mynet5app.csproj -c Release -o ${{env.DOTNET_ROOT}}/myapp --no-build --no-restore
 
    - name: Deploy to Azure Web App
      uses: azure/webapps-deploy@v1
      with:
        app-name: 'app-githubact-demo'
        slot-name: 'production'
        publish-profile: ${{ secrets.MYNET5WEBAPPPUBLISHPROFILE }}
        package: ${{env.DOTNET_ROOT}}/myapp 

No comments:

Popular Posts