Build .NET 5 Applications with GitHub Actions
Tags: dotnet, devops, tutorial
Published: 2020-12-23 06:04:59

---
title: Build .NET 5 Applications with GitHub Actions
published: true
date: 2020-12-23 00:00:00 UTC
tags: dotnet, devops, tutorial
canonical_url: https://elliotdenolf.com/posts/build-net-5-applications-with-github-actions
---

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

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#create-basic-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:

```sh
mkdir MyApp MyApp.Tests
cd MyApp
dotnet new webapi
cd ../MyApp.Tests
dotnet new xunit
cd ..
dotnet new sln
dotnet sln add **/*.csproj
```

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
```

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#create-workflow)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.

```yaml
name: .NET 5 CI
on: [push]
```

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.

```yaml
jobs:
  build:
    runs-on: ubuntu-latest
    name: .NET 5 Application
```

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

```yaml
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
```

> NOTE: The `dotnet-version` used is the latest from the [.NET 5 Download page](https://dotnet.microsoft.com/download/dotnet/5.0). 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:

```yaml
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
```

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.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#links)Links

- **[Source code for this example](https://github.com/denolfe/blog-code-samples/tree/master/posts/build-net-5-github-actions)**
- [GitHub Actions Documentation](https://docs.github.com/en/free-pro-team@latest/actions)
- [.NET 5 Download page](https://dotnet.microsoft.com/download/dotnet/5.0)
- [Dotnet CLI documentation](https://docs.microsoft.com/en-us/dotnet/core/tools/?tabs=netcore2x)