DEV Community

Cover image for Creating a basic GitHub workflow for .NET Core and Azure App Services
Owen
Owen

Posted on

Creating a basic GitHub workflow for .NET Core and Azure App Services

For the end result, skip to the bottom of the page.

When learning how to deploy .NET apps to Azure I struggled to find to find a solid example specific to my needs.

My learning is still in progress and after several messy but still successful attempts, I came up with what you see below. It's an extremely basic example and I intend to build on it in future posts.

Prerequisites

  • Azure Setup:
    • Create an Azure App Service configured for .NET hosting.
    • Obtain the publish profile of the App Service:
      • Go to your App Service in the Azure portal.
      • Navigate to Deployment Center > Get Publish Profile, and download it.
  • GitHub Setup:
    • Your GitHub repository should contain your .NET web app source code.
    • Save the App Service publish profile as a secret
      • Go to your repo > Settings > Secrets & Variables > Actions > New Repository Secret.
      • Name it AZURE_PUBLISH_PROFILE and set the value to the content of the publish profile file you downloaded.

Creating the Workflow

  • Create the following directory in the root of your project: .github/workflows/
  • Create a YAML file inside that directory. It doesn't matter what it's called, as long as has the .yml extension, for example: deploy.yml.
  • Add the following code blocks:

Give your workflow a name:

name: Deploy .NET App to Azure App Service
Enter fullscreen mode Exit fullscreen mode

Define the trigger

In this example, the workflow is run whenever a push is made to main branch, and workflow_dispatch enables it to be run manually from GitHub.
Follow this link to see all the trigger options you have.

on:
  push:
    branches:
      - main
  workflow_dispatch:
Enter fullscreen mode Exit fullscreen mode

Configure the workflow environment

jobs:
  build-and-deploy:
    permissions: write-all
    runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

Now we start creating the workflow

    steps:
Enter fullscreen mode Exit fullscreen mode

Fetch the code from your repository

      - name: Checkout codedepen
        uses: actions/checkout@v4
Enter fullscreen mode Exit fullscreen mode

Install .NET Core to build your application

Set the dotnet-version to the version you're using.

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '8.x'
Enter fullscreen mode Exit fullscreen mode

Download all dependencies

      - name: Restore dependencies
        run: dotnet restore
Enter fullscreen mode Exit fullscreen mode

Publish the application in release mode to the publish folder

-c defines the build configuration which in this workflow is Release.
-o defines the output directory which is ./publish.
More information on dotnet publish can be found here.

      - name: Build the project
        run: dotnet publish -c Release -o ./publish
Enter fullscreen mode Exit fullscreen mode

Deploy the application using the publish profile you saved earlier

      - name: Deploy to Azure Web App
        uses: azure/webapps-deploy@v2
        with:
          app-name: '<your app service name>'
          publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}
          package: ./publish
Enter fullscreen mode Exit fullscreen mode

The end result

name: simple deploy

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build-and-deploy:
    permissions: write-all
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '8.x'

      - name: Restore dependencies
        run: dotnet restore

      - name: Build the project
        run: dotnet publish -c Release -o ./publish

      - name: Deploy to Azure Web App
        uses: azure/webapps-deploy@v2
        with:
          app-name: '<your app service name>'
          publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}
          package: ./publish
Enter fullscreen mode Exit fullscreen mode

Top comments (0)