name: 'Usage of setup-dotnet GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '3.1.x'
Setup Dotnet
Set up your GitHub Actions workflow with a specific version of the .NET core sdk
What is Setup Dotnet?
The setup-dotnet
action sets up a .NET CLI environment for use in GitHub Actions workflows. It optionally downloads and caches a version of the .NET SDK, registers problem matchers for error output, and sets up authentication to private package sources like GitHub Packages.
How to Set Up Multiple Versions of .NET Core SDK Using GitHub Actions
The setup-dotnet
action allows you to install multiple versions of the .NET Core SDK for use in your GitHub Actions workflows. This is useful for projects that need to build against multiple versions of the .NET SDK.
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
3.1.x
5.0.x
- run: dotnet build <my project>
How to Use the dotnet-quality Input with setup-dotnet Action
The dotnet-quality
input allows you to install the latest build of the specified quality for the .NET SDK. The possible values for dotnet-quality
are: daily, signed, validated, preview, and ga.
The
dotnet-quality
input can only be used with .NET SDK versions in 'A.B', 'A.B.x', 'A', 'A.x', and 'A.B.Cxx' formats where the major version is higher than 5. In other cases, thedotnet-quality
input will be ignored.
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '6.0.x'
dotnet-quality: 'preview'
- run: dotnet build <my project>
How to Use the global-json-file Input with setup-dotnet Action
The setup-dotnet
action can read the .NET SDK version from a global.json
file. This allows you to specify the SDK version in your project's configuration file. The global-json-file
input is used to specify the path to the global.json
. If the specified file doesn't exist, the action will fail with an error.
- uses: actions/setup-dotnet@v4
with:
global-json-file: csharp/global.json
- run: dotnet build <my project>
working-directory: csharp
How to Cache NuGet Packages Using setup-dotnet Action
The setup-dotnet
action includes built-in functionality for caching and restoring NuGet package dependencies. It uses toolkit/cache
under the hood to cache global packages data but requires less configuration settings. By default, caching is turned off.
- uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.x
cache: true
- run: dotnet restore --locked-mode
Note: This action will only restore global-packages folder, so you will probably get the NU1403 error when running dotnet restore. To avoid this, you can use DisableImplicitNuGetFallbackFolder option.
<PropertyGroup>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
</PropertyGroup>
How to Cache NuGet Packages and Reduce Cache Size Using setup-dotnet Action
The setup-dotnet
action includes built-in functionality for caching and restoring NuGet package dependencies. You can also reduce the cache size by using the NUGET_PACKAGES
environment variable, especially useful when some action runners already have large libraries (e.g., Xamarin).
env:
NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.x
cache: true
- run: dotnet restore --locked-mode
How to Cache NuGet Packages in Monorepos Using setup-dotnet Action
The setup-dotnet
action includes built-in functionality for caching and restoring NuGet package dependencies. In monorepos, you can specify multiple dependency paths for caching.
env:
NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.x
cache: true
cache-dependency-path: subdir/packages.lock.json
- run: dotnet restore --locked-mode
How to Set Up Authentication for NuGet Feeds Using GitHub Actions
This guide will show you how to set up authentication for NuGet feeds, specifically for GitHub Package Registry (GPR), using GitHub Actions. This setup allows you to authenticate and publish your .NET packages to GPR securely.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '3.1.x'
source-url: https://nuget.pkg.github.com/<owner>/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- run: dotnet build <my project>
- name: Create the package
run: dotnet pack --configuration Release <my project>
- name: Publish the package to GPR
run: dotnet nuget push <my project>/bin/Release/*.nupkg