Building and testing .NET
Learn how to create a continuous integration (CI) workflow to build and test your .NET project.
In this article
Introduction
This guide shows you how to build, test, and publish a .NET package.
GitHub-hosted runners have a tools cache with preinstalled software, which includes the .NET Core SDK. For a full list of up-to-date software and the preinstalled versions of .NET Core SDK, seesoftware installed on GitHub-hosted runners.
Prerequisites
You should already be familiar with YAML syntax and how it's used with GitHub Actions. For more information, seeWorkflow syntax for GitHub Actions.
We recommend that you have a basic understanding of the .NET Core SDK. For more information, seeGetting started with .NET.
Using a .NET workflow template
To get started quickly, add a workflow template to the.github/workflows directory of your repository.
GitHub provides a workflow template for .NET that should work for most .NET projects. The subsequent sections of this guide give examples of how you can customize this workflow template.
On GitHub, navigate to the main page of the repository.
Under your repository name, click Actions.

If you already have a workflow in your repository, clickNew workflow.
The "Choose a workflow" page shows a selection of recommended workflow templates. Search for "dotnet".
On the ".NET" workflow, clickConfigure.
Edit the workflow as required. For example, change the .NET version.
ClickCommit changes.
The
dotnet.ymlworkflow file is added to the.github/workflowsdirectory of your repository.
Specifying a .NET version
To use a preinstalled version of the .NET Core SDK on a GitHub-hosted runner, use thesetup-dotnet action. This action finds a specific version of .NET from the tools cache on each runner, and adds the necessary binaries toPATH. These changes will persist for the remainder of the job.
Thesetup-dotnet action is the recommended way of using .NET with GitHub Actions, because it ensures consistent behavior across different runners and different versions of .NET. If you are using a self-hosted runner, you must install .NET and add it toPATH. For more information, see thesetup-dotnet action.
Using multiple .NET versions
name:dotnetpackageon: [push]jobs:build:runs-on:ubuntu-lateststrategy:matrix:dotnet-version: ['3.1.x','6.0.x' ]steps:-uses:actions/checkout@v5-name:Setupdotnet${{matrix.dotnet-version}}uses:actions/setup-dotnet@v4with:dotnet-version:${{matrix.dotnet-version}}# You can test your matrix by printing the current dotnet version-name:Displaydotnetversionrun:dotnet--versionUsing a specific .NET version
You can configure your job to use a specific version of .NET, such as6.0.22. Alternatively, you can use semantic version syntax to get the latest minor release. This example uses the latest minor release of .NET 6.
-name:Setup.NET6.xuses:actions/setup-dotnet@v4with:# Semantic version range syntax or exact version of a dotnet versiondotnet-version:'6.x'Installing dependencies
GitHub-hosted runners have the NuGet package manager installed. You can use the dotnet CLI to install dependencies from the NuGet package registry before building and testing your code. For example, the YAML below installs theNewtonsoft package.
steps:-uses:actions/checkout@v5-name:Setupdotnetuses:actions/setup-dotnet@v4with:dotnet-version:'6.0.x'-name:Installdependenciesrun:dotnetaddpackageNewtonsoft.Json--version12.0.1Caching dependencies
You can cache NuGet dependencies for future workflows using the optionalcache input. For example, the YAML below caches the NuGetglobal-packages folder, and then installs theNewtonsoft package. A second optional input,cache-dependency-path, can be used to specify the path to a dependency file:packages.lock.json.
For more information, seeDependency caching reference.
steps:-uses:actions/checkout@v5-name:Setupdotnetuses:actions/setup-dotnet@v4with:dotnet-version:'6.x'cache:true-name:Installdependenciesrun:dotnetaddpackageNewtonsoft.Json--version12.0.1Note
Depending on the number of dependencies, it may be faster to use the dependency cache. Projects with many large dependencies should see a performance increase as it cuts down the time required for downloading. Projects with fewer dependencies may not see a significant performance increase and may even see a slight decrease due to how NuGet installs cached dependencies. The performance varies from project to project.
Building and testing your code
You can use the same commands that you use locally to build and test your code. This example demonstrates how to usedotnet build anddotnet test in a job:
steps:-uses:actions/checkout@v5-name:Setupdotnetuses:actions/setup-dotnet@v4with:dotnet-version:'6.0.x'-name:Installdependenciesrun:dotnetrestore-name:Buildrun:dotnetbuild--no-restore-name:TestwiththedotnetCLIrun:dotnettest--no-buildPackaging workflow data as artifacts
After a workflow completes, you can upload the resulting artifacts for analysis. For example, you may need to save log files, core dumps, test results, or screenshots. The following example demonstrates how you can use theupload-artifact action to upload test results.
For more information, seeStore and share data with workflow artifacts.
name:dotnetpackageon: [push]jobs:build:runs-on:ubuntu-lateststrategy:matrix:dotnet-version: ['3.1.x','6.0.x' ]steps:-uses:actions/checkout@v5-name:Setupdotnetuses:actions/setup-dotnet@v4with:dotnet-version:${{matrix.dotnet-version}}-name:Installdependenciesrun:dotnetrestore-name:Testwithdotnetrun:dotnettest--no-restore--loggertrx--results-directory"TestResults-${{ matrix.dotnet-version }}"-name:Uploaddotnettestresultsuses:actions/upload-artifact@v4with:name:dotnet-results-${{matrix.dotnet-version}}path:TestResults-${{matrix.dotnet-version}}# Use always() to always run this step to publish test results when there are test failuresif:${{always()}}Publishing to package registries
You can configure your workflow to publish your .NET package to a package registry when your CI tests pass. You can use repository secrets to store any tokens or credentials needed to publish your binary. The following example creates and publishes a package to GitHub Packages usingdotnet core cli.
name:Uploaddotnetpackageon:release:types: [created]jobs:deploy:runs-on:ubuntu-latestpermissions:packages:writecontents:readsteps:-uses:actions/checkout@v5-uses:actions/setup-dotnet@v4with:dotnet-version:'6.0.x'# SDK Version to use.source-url:https://nuget.pkg.github.com/<owner>/index.jsonenv:NUGET_AUTH_TOKEN:${{secrets.GITHUB_TOKEN}}-run:dotnetbuild--configurationRelease<myproject>-name:Createthepackagerun:dotnetpack--configurationRelease<myproject>-name:PublishthepackagetoGPRrun:dotnetnugetpush<myproject>/bin/Release/*.nupkg