Build and Test .NET Core with GitHub Actions

Picture: GitHub

Build and Test .NET Core with GitHub Actions

What is GitHub Actions?

GitHub Actions is the counterpart to Azure DevOps Pipelines: a fully configurable automation tool for setting up workflows, such as building, testing and publishing software and applications.

Similar to Azure DevOps and other similar systems, the configuration and setup of the workflows is done via text files, more precisely YAML files.

The advantage of YAML files

The advantage of configuring directly in text files is that they live together with the source code of the application: the Git repository. The source code is thus always synchronized with the workflow configuration and can also live through the lifecycle with feature and release branches.

Templates

As in Azure DevOps, GitHub Actions provides corresponding workflow templates for various programming languages and tools - and they can be extended with your own templates.

My template to build .NET

Templates are not ready-made solutions, but only building blocks that can be used to put together your workflow accordingly.

Here you can see the basic workflow I use for .NET applications:

name: NETCore

on:
    push:
      branches:
        - master
    pull_request:
      branches:
        - master

env:
  BuildConfig: Release
  SolutionFile: MySolution.sln

jobs:
    build:
      runs-on: ubuntu-latest
      steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0 # avoid shallow clone so nbgv can do its work.
      
      - uses: aarnott/[email protected] # https://github.com/AArnott/nbgv
        id: nbgv

      - name: Versioning
        run: echo ${{ steps.nbgv.outputs.SemVer2 }}

      - name: Build with dotnet
        run: dotnet build
              --configuration ${{ env.BuildConfig }}
              /p:Version=${{ steps.nbgv.outputs.AssemblyVersion }}

      - name: Test with dotnet
        run: dotnet test ${{ env.SolutionFile }}

This workflow basically already exists in my Azure DevOps templates. It basically runs through the following steps:

  • Loading the source code in the corresponding Git Branch
  • Automatically generate a version based on Semantic Versioning using the Nerdbank.GitVersioning tool (an alternative would be GitVersion)
  • Compiling the source code (entire .NET Solution)
  • Testing the solution (e.g. unit tests)

In a real mission, this would usually be added here:

  • Checking for security issues with WhiteSource or GitHub Security, for example
  • Packaging of software and applications
  • Possibly publish the software, for example on NuGet, Azure, AWS, Docker.