DEV Community

Elliot DeNolf
Elliot DeNolf

Posted on • Originally published at elliotdenolf.com on

Build .NET 5 Applications with GitHub Actions

This article will go through setting up GitHub Actions to build and test a .NET 5 application.

Create Basic Application

First, let’s build out our basic application and test suite using the dotnet CLI. Create a new directory for your project and go through the following commands:

mkdir MyApp MyApp.Tests
cd MyApp
dotnet new webapi
cd ../MyApp.Tests
dotnet new xunit
cd ..
dotnet new sln
dotnet sln add **/*.csproj
Enter fullscreen mode Exit fullscreen mode

We’ve now built out a solution with a web API and test suite to use in our Workflow.

Before we move forward, here is our simplified project structure:

.
|-- MyApp
| |-- MyApp.csproj
| |-- Program.cs
| `-- Startup.cs
|-- MyApp.Tests
| |-- MyApp.Tests.csproj
| `-- UnitTest1.cs
`-- MyApp.sln
Enter fullscreen mode Exit fullscreen mode

Create Workflow

GitHub Actions is driven by having workflow definitions in the .github/workflows directory. Create these 2 directories and add a file named ci.yml.

The first thing we will add to our ci.yml will be a name and an on trigger. This simply gives our workflow a name and also defines when the workflow should run - when we push to the repository.

name: .NET 5 CI
on: [push]
Enter fullscreen mode Exit fullscreen mode

Next, we define our jobs section. We will only be adding a single job that will have multiple steps. Add the following to our ci.yml which will simply define our job and what OS it will run on.

jobs:
  build:
    runs-on: ubuntu-latest
    name: .NET 5 Application
Enter fullscreen mode Exit fullscreen mode

The first step of our job will be to configure .NET with our desired version - .NET 5 in our case

jobs:
  build:
    runs-on: ubuntu-latest
    name: .NET 5 Application
    steps:
      - uses: actions/checkout@v2
      - name: Setup dotnet
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '5.0.101' # Check for latest at link below
Enter fullscreen mode Exit fullscreen mode

NOTE: The dotnet-version used is the latest from the .NET 5 Download page. Be sure to check the latest version available and swap that in. Wildcarding the version like 5.x should be supported in the near future.

Now that .NET is configured and makes the dotnet cli available to our workflow, we will add steps for dotnet build and dotnet test.

Let’s add those. The complete ci.yml should look like this:

name: .NET 5 CI
on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    name: .NET 5 Application
    steps:
      - uses: actions/checkout@v2
      - name: Setup dotnet
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '5.0.101' # Check for latest at link at .NET 5 download page
      - run: dotnet build
      - run: dotnet test
Enter fullscreen mode Exit fullscreen mode

If we add commit this file, push, and navigate to the Actions section of our repository, we will see a new workflow run that goes through all of the steps.

Links

Top comments (0)