This article will go through setting up GitHub Actions to build and test a .NET 5 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
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
GitHub Actions is driven by having workflow definitions in the
.github/workflows directory. Create these 2 directories and add a file named
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]
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
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
dotnet-versionused 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.xshould 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
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
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.