title | intro | redirect_from | versions | shortTitle | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Building and testing .NET | You can create a continuous integration (CI) workflow to build and test your .NET project. |
|
| Build & test .NET |
{% data reusables.actions.enterprise-github-hosted-runners %}
This guide shows you how to build, test, and publish a .NET package.
{% data variables.product.prodname_dotcom %}-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, see software installed on {% data variables.product.prodname_dotcom %}-hosted runners.
You should already be familiar with YAML syntax and how it's used with {% data variables.product.prodname_actions %}. For more information, see AUTOTITLE.
We recommend that you have a basic understanding of the .NET Core SDK. For more information, see Getting started with .NET.
{% data reusables.actions.workflow-templates-get-started %}
{% data variables.product.prodname_dotcom %} 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.
{% data reusables.repositories.navigate-to-repo %} {% data reusables.repositories.actions-tab %} {% data reusables.actions.new-starter-workflow %}
- The "Choose a workflow" page shows a selection of recommended workflow templates. Search for "dotnet".
- On the ".NET" workflow, click Configure.
{%- ifversion ghes %}
If you don't find the ".NET" workflow template, copy the following workflow code to a new file called dotnet.yml
in the .github/workflows
directory of your repository.
name: .NETon: push: branches: [ "main" ]pull_request: branches: [ "main" ]jobs: build: runs-on: ubuntu-lateststeps: - uses: {% data reusables.actions.action-checkout %} - name: Setup .NETuses: {% data reusables.actions.action-setup-dotnet %}with: dotnet-version: 6.0.x - name: Restore dependenciesrun: dotnet restore - name: Buildrun: dotnet build --no-restore - name: Testrun: dotnet test --no-build --verbosity normal
{%- endif %}
- Edit the workflow as required. For example, change the .NET version.
- Click Commit changes.
{% ifversion fpt or ghec %} The dotnet.yml
workflow file is added to the .github/workflows
directory of your repository. {% endif %}
To use a preinstalled version of the .NET Core SDK on a {% data variables.product.prodname_dotcom %}-hosted runner, use the setup-dotnet
action. This action finds a specific version of .NET from the tools cache on each runner, and adds the necessary binaries to PATH
. These changes will persist for the remainder of the job.
The setup-dotnet
action is the recommended way of using .NET with {% data variables.product.prodname_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 to PATH
. For more information, see the setup-dotnet
action.
name: dotnet packageon: [push]jobs: build: runs-on: ubuntu-lateststrategy: matrix: dotnet-version: [ '3.1.x', '6.0.x' ]steps: - uses: {% data reusables.actions.action-checkout %} - name: Setup dotnet {% raw %}${{ matrix.dotnet-version }}{% endraw %}uses: {% data reusables.actions.action-setup-dotnet %}with: dotnet-version: {% raw %}${{ matrix.dotnet-version }}{% endraw %}# You can test your matrix by printing the current dotnet version - name: Display dotnet versionrun: dotnet --version
You can configure your job to use a specific version of .NET, such as 6.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 .NET 6.xuses: {% data reusables.actions.action-setup-dotnet %}with: # Semantic version range syntax or exact version of a dotnet versiondotnet-version: '6.x'
{% data variables.product.prodname_dotcom %}-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 the Newtonsoft
package.
steps: - uses: {% data reusables.actions.action-checkout %} - name: Setup dotnetuses: {% data reusables.actions.action-setup-dotnet %}with: dotnet-version: '6.0.x' - name: Install dependenciesrun: dotnet add package Newtonsoft.Json --version 12.0.1
You can cache NuGet dependencies for future workflows using the optional cache
input. For example, the YAML below caches the NuGet global-packages
folder, and then installs the Newtonsoft
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, see AUTOTITLE.
steps: - uses: {% data reusables.actions.action-checkout %} - name: Setup dotnetuses: {% data reusables.actions.action-setup-dotnet %}with: dotnet-version: '6.x'cache: true - name: Install dependenciesrun: dotnet add package Newtonsoft.Json --version 12.0.1
Note
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.
You can use the same commands that you use locally to build and test your code. This example demonstrates how to use dotnet build
and dotnet test
in a job:
steps: - uses: {% data reusables.actions.action-checkout %} - name: Setup dotnetuses: {% data reusables.actions.action-setup-dotnet %}with: dotnet-version: '6.0.x' - name: Install dependenciesrun: dotnet restore - name: Buildrun: dotnet build --no-restore - name: Test with the dotnet CLIrun: dotnet test --no-build
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 the upload-artifact
action to upload test results.
For more information, see AUTOTITLE.
name: dotnet packageon: [push]jobs: build: runs-on: ubuntu-lateststrategy: matrix: dotnet-version: [ '3.1.x', '6.0.x' ]steps: - uses: {% data reusables.actions.action-checkout %} - name: Setup dotnetuses: {% data reusables.actions.action-setup-dotnet %}with: dotnet-version: {% raw %}${{ matrix.dotnet-version }}{% endraw %} - name: Install dependenciesrun: dotnet restore - name: Test with dotnetrun: dotnet test --no-restore --logger trx --results-directory {% raw %}"TestResults-${{ matrix.dotnet-version }}"{% endraw %} - name: Upload dotnet test resultsuses: {% data reusables.actions.action-upload-artifact %}with: name: {% raw %}dotnet-results-${{ matrix.dotnet-version }}{% endraw %}path: {% raw %}TestResults-${{ matrix.dotnet-version }}{% endraw %}# Use always() to always run this step to publish test results when there are test failuresif: {% raw %}${{ always() }}{% endraw %}
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 {% data variables.product.prodname_registry %} using dotnet core cli
.
name: Upload dotnet packageon: release: types: [created]jobs: deploy: runs-on: ubuntu-latestpermissions: packages: writecontents: readsteps: - uses: {% data reusables.actions.action-checkout %} - uses: {% data reusables.actions.action-setup-dotnet %}with: dotnet-version: '6.0.x'# SDK Version to use.source-url: https://nuget.pkg.github.com/<owner>/index.jsonenv: NUGET_AUTH_TOKEN: {% raw %}${{secrets.GITHUB_TOKEN}}{% endraw %} - run: dotnet build --configuration Release <my project> - name: Create the packagerun: dotnet pack --configuration Release <my project> - name: Publish the package to GPRrun: dotnet nuget push <my project>/bin/Release/*.nupkg