• Stars
    star
    932
  • Rank 49,020 (Top 1.0 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Set up your GitHub Actions workflow with a specific version of the .NET core sdk

setup-dotnet

Basic validation e2e tests

This action sets up a .NET CLI environment for use in actions by:

  • optionally downloading and caching a version(s) of dotnet by SDK version(s) and adding to PATH
  • registering problem matchers for error output
  • setting up authentication to private package sources like GitHub Packages

Note: GitHub hosted runners have some versions of the .NET SDK preinstalled. Installed versions are subject to change. Please refer to the documentation: Software installed on github hosted runners for .NET SDK versions that are currently available.

Usage

See action.yml

Basic:

steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
  with:
    dotnet-version: '3.1.x'
- run: dotnet build <my project>

Warning: Unless a concrete version is specified in the global.json file, the latest .NET version installed on the runner (including preinstalled versions) will be used by default. Please refer to the documentation for the currently preinstalled .NET SDK versions.

Multiple version installation:

steps:
- uses: actions/checkout@v3
- name: Setup dotnet
  uses: actions/setup-dotnet@v3
  with:
    dotnet-version: | 
      3.1.x
      5.0.x
- run: dotnet build <my project>

Supported version syntax

The dotnet-version input supports following syntax:

  • A.B.C (e.g 6.0.400, 7.0.100-preview.7.22377.5) - installs exact version of .NET SDK
  • A.B or A.B.x (e.g. 3.1, 3.1.x) - installs the latest patch version of .NET SDK on the channel 3.1, including prerelease versions (preview, rc)
  • A or A.x (e.g. 3, 3.x) - installs the latest minor version of the specified major tag, including prerelease versions (preview, rc)
  • A.B.Cxx (e.g. 6.0.4xx) - available since .NET 5.0 release. Installs the latest version of the specific SDK release, including prerelease versions (preview, rc).

Using the dotnet-quality input

This input sets up the action to install the latest build of the specified quality in the channel. The possible values of dotnet-quality are: daily, signed, validated, preview, ga.

Note: dotnet-quality input can be used only with .NET SDK version 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, dotnet-quality input will be ignored.

steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
  with:
    dotnet-version: '6.0.x'
    dotnet-quality: 'preview'
- run: dotnet build <my project>

Using the global-json-file input

setup-dotnet action can read .NET SDK version from a global.json file. Input global-json-file is used for specifying the path to the global.json. If the file that was supplied to global-json-file input doesn't exist, the action will fail with error.

Note: In case both dotnet-version and global-json-file inputs are used, versions from both inputs will be installed.

steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
  with:
    global-json-file: csharp/global.json
- run: dotnet build <my project>
  working-directory: csharp

Caching NuGet Packages

The action has a built-in functionality for caching and restoring dependencies. It uses toolkit/cache under the hood for caching global packages data but requires less configuration settings. The cache input is optional, and caching is turned off by default.

The action searches for NuGet Lock files (packages.lock.json) in the repository root, calculates their hash and uses it as a part of the cache key. If lock file does not exist, this action throws error. Use cache-dependency-path for cases when multiple dependency files are used, or they are located in different subdirectories.

Warning: Caching NuGet packages is available since .NET SDK 2.1.500 and 2.2.100 as the NuGet lock file is available only for NuGet 4.9 and above.

steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
  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>

Reduce caching size

Note: Use NUGET_PACKAGES environment variable if available. Some action runners already has huge libraries. (ex. Xamarin)

env:
  NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
  with:
    dotnet-version: 6.x
    cache: true
- run: dotnet restore --locked-mode

Caching NuGet packages in monorepos

env:
  NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
  with:
    dotnet-version: 6.x
    cache: true
    cache-dependency-path: subdir/packages.lock.json
- run: dotnet restore --locked-mode

Matrix Testing

Using setup-dotnet it's possible to use matrix syntax to install several versions of .NET SDK:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        dotnet: [ '2.1.x', '3.1.x', '5.0.x' ]
    name: Dotnet ${{ matrix.dotnet }} sample
    steps:
      - uses: actions/checkout@v3
      - name: Setup dotnet
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: ${{ matrix.dotnet }}
      - name: Execute dotnet
        run: dotnet build <my project>

Note: Unless a concrete version is specified in the global.json file, the latest .NET version installed on the runner (including preinstalled versions) will be used by default. To control this behavior you may want to use temporary global.json files:

Matrix testing with temporary global.json creation

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        dotnet: [ '2.1.x', '3.1.x', '5.0.x' ]
    name: Dotnet ${{ matrix.dotnet }} sample
    steps:
      - uses: actions/checkout@v3
      - name: Setup dotnet
        uses: actions/setup-dotnet@v3
        id: stepid
        with:
          dotnet-version: ${{ matrix.dotnet }}
      - name: Create temporary global.json
        run: echo '{"sdk":{"version": "${{ steps.stepid.outputs.dotnet-version }}"}}' > ./global.json
      - name: Execute dotnet
        run: dotnet build <my project>

Setting up authentication for nuget feeds

Github Package Registry (GPR)

steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
  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

Azure Artifacts

- uses: actions/setup-dotnet@v3
  with:
    source-url: https://pkgs.dev.azure.com/<your-organization>/_packaging/<your-feed-name>/nuget/v3/index.json
  env:
    NUGET_AUTH_TOKEN: ${{secrets.AZURE_DEVOPS_PAT}} # Note, create a secret with this name in Settings
- name: Publish the package to Azure Artifacts
  run: dotnet nuget push <my project>/bin/Release/*.nupkg

nuget.org

- uses: actions/setup-dotnet@v3
  with:
    dotnet-version: 3.1.x
- name: Publish the package to nuget.org
  run: dotnet nuget push */bin/Release/*.nupkg -k $NUGET_AUTH_TOKEN -s https://api.nuget.org/v3/index.json
  env:
    NUGET_AUTH_TOKEN: ${{ secrets.NUGET_TOKEN }}

Note: It's the only way to push a package to nuget.org feed for macOS/Linux machines due to API key config store limitations.

Outputs and environment variables

Outputs

dotnet-version

Using the dotnet-version output it's possible to get the installed by the action .NET SDK version.

Single version installation

In case of a single version installation, the dotnet-version output contains the version that is installed by the action.

    - uses: actions/setup-dotnet@v3
      id: stepid
      with:
        dotnet-version: 3.1.422
    - run: echo '${{ steps.stepid.outputs.dotnet-version }}' # outputs 3.1.422

Multiple version installation

In case of a multiple version installation, the dotnet-version output contains the latest version that is installed by the action.

    - uses: actions/setup-dotnet@v3
      id: stepid
      with:
        dotnet-version: | 
          3.1.422
          5.0.408
    - run: echo '${{ steps.stepid.outputs.dotnet-version }}' # outputs 5.0.408

Installation from global.json

When the dotnet-version input is used along with the global-json-file input, the dotnet-version output contains the version resolved from the global.json.

    - uses: actions/setup-dotnet@v3
      id: stepid
      with:
        dotnet-version: | 
          3.1.422
          5.0.408
        global-json-file: "./global.json" # contains version 2.2.207
    - run: echo '${{ steps.stepid.outputs.dotnet-version }}' # outputs 2.2.207

cache-hit

A boolean value to indicate an exact match was found for the cache key (follows actions/cache)

Environment variables

Some environment variables may be necessary for your particular case or to improve logging. Some examples are listed below, but the full list with complete details can be found here: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables

Env.variable Description Default value
DOTNET_INSTALL_DIR Specifies a directory where .NET SDKs should be installed by the action. default value for each OS
DOTNET_NOLOGO Removes logo and telemetry message from first run of dotnet cli false
DOTNET_CLI_TELEMETRY_OPTOUT Opt-out of telemetry being sent to Microsoft false
DOTNET_MULTILEVEL_LOOKUP Configures whether the global install location is used as a fall-back true
NUGET_PACKAGES Configures a path to the NuGet global-packages folder default value for each OS

The default values of the DOTNET_INSTALL_DIR and NUGET_PACKAGES environment variables depend on the operation system which is used on a runner:

Operation system DOTNET_INSTALL_DIR NUGET_PACKAGES
Windows C:\Program Files\dotnet %userprofile%\.nuget\packages
Ubuntu /usr/share/dotnet ~/.nuget/packages
macOS /Users/runner/.dotnet ~/.nuget/packages

Example usage of environment variable:

build:
  runs-on: ubuntu-latest
  env:
    DOTNET_INSTALL_DIR: "path/to/directory"
    NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
  steps:
    - uses: actions/checkout@main
    - uses: actions/setup-dotnet@v3
      with:
        dotnet-version: '3.1.x'
        cache: true

License

The scripts and documentation in this project are released under the MIT License

Contributions

Contributions are welcome! See Contributor's Guide

More Repositories

1

runner-images

GitHub Actions runner images
PowerShell
9,683
star
2

starter-workflows

Accelerating new GitHub Actions workflows
TypeScript
8,687
star
3

toolkit

The GitHub ToolKit for developing GitHub Actions.
TypeScript
4,974
star
4

runner

The Runner for GitHub Actions 🚀
C#
4,825
star
5

checkout

Action for checking out a repo
TypeScript
4,634
star
6

actions-runner-controller

Kubernetes controller for GitHub Actions self-hosted runners
Go
4,595
star
7

cache

Cache dependencies and build outputs in GitHub Actions
TypeScript
4,511
star
8

github-script

Write workflows scripting the GitHub API in JavaScript
TypeScript
4,184
star
9

setup-node

Set up your GitHub Actions workflow with a specific version of node.js
TypeScript
3,895
star
10

upload-artifact

TypeScript
3,162
star
11

typescript-action

Create a TypeScript Action with tests, linting, workflow, publishing, and versioning
TypeScript
2,001
star
12

labeler

An action for automatically labelling pull requests
TypeScript
1,941
star
13

setup-python

Set up your GitHub Actions workflow with a specific version of Python
TypeScript
1,702
star
14

setup-java

Set up your GitHub Actions workflow with a specific version of Java
TypeScript
1,517
star
15

setup-go

Set up your GitHub Actions workflow with a specific version of Go
TypeScript
1,391
star
16

stale

Marks issues and pull requests that have not had recent interaction
TypeScript
1,345
star
17

download-artifact

TypeScript
1,338
star
18

create-release

An Action to create releases via the GitHub Release API
JavaScript
1,333
star
19

javascript-action

Create a JavaScript Action with tests, linting, workflow, publishing, and versioning
JavaScript
966
star
20

upload-release-asset

An Action to upload a release asset via the GitHub Release API
JavaScript
660
star
21

first-interaction

An action for filtering pull requests and issues from first-time contributors
JavaScript
648
star
22

dependency-review-action

A GitHub Action for detecting vulnerable dependencies and invalid licenses in your PRs
TypeScript
598
star
23

deploy-pages

GitHub Action to publish artifacts to GitHub Pages for deployments
JavaScript
555
star
24

add-to-project

Automate adding issues and pull requests to GitHub projects
TypeScript
455
star
25

delete-package-versions

TypeScript
345
star
26

gh-actions-cache

A GitHub (gh) CLI extension to manage the GitHub Actions caches being used in a GitHub repository.
Go
257
star
27

example-services

Example workflows using service containers
JavaScript
248
star
28

hello-world-javascript-action

A template to demonstrate how to build a JavaScript action.
JavaScript
230
star
29

container-action

Shell
185
star
30

heroku

GitHub Action for interacting with Heroku
HCL
179
star
31

setup-ruby

Set up your GitHub Actions workflow with a specific version of Ruby
TypeScript
173
star
32

upload-pages-artifact

A composite action for packaging and uploading an artifact that can be deployed to GitHub Pages.
Shell
171
star
33

hello-world-docker-action

A template to demonstrate how to build a Docker action.
Shell
162
star
34

setup-elixir

Set up your GitHub Actions workflow with OTP and Elixir
JavaScript
154
star
35

python-versions

Python builds for Actions Runner Images
PowerShell
148
star
36

container-toolkit-action

Template repo for creating container actions using https://github.com/actions/toolkit/
TypeScript
115
star
37

github

Wraps actions-toolkit into an Action for common GitHub automations.
JavaScript
103
star
38

actions-sync

This tool allows GHES administrators to sync Actions to their instances
Go
93
star
39

configure-pages

An action to enable Pages and extract various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.
JavaScript
91
star
40

create-github-app-token

GitHub Action for creating a GitHub App Installation Access Token
JavaScript
86
star
41

http-client

A lightweight HTTP client optimized for use with actions, TypeScript with generics and async await.
TypeScript
72
star
42

setup-haskell

Set up your GitHub Actions workflow with a specific version of Haskell (GHC and Cabal)
TypeScript
71
star
43

node-versions

Node builds for Actions Runner Images
PowerShell
69
star
44

languageservices

Language services for GitHub Actions workflows and expressions.
TypeScript
67
star
45

jekyll-build-pages

A simple GitHub Action for producing Jekyll build artifacts compatible with GitHub Pages.
HTML
66
star
46

importer-labs

GitHub Actions Importer helps you plan and automate the migration of Azure DevOps, Bamboo, CircleCI, GitLab, Jenkins, and Travis CI pipelines to GitHub Actions.
Ruby
65
star
47

runner-container-hooks

Runner Container Hooks for GitHub Actions
TypeScript
58
star
48

partner-runner-images

About GitHub Actions runner images provided by 3rd parties
54
star
49

go-dependency-submission

Calculates dependencies for a Go build-target and submits the list to the Dependency Submission API
TypeScript
53
star
50

importer-issue-ops

GitHub Actions Importer helps you plan and automate the migration of Azure DevOps, Bamboo, CircleCI, GitLab, Jenkins, and Travis CI pipelines to GitHub Actions.
Ruby
49
star
51

publish-action

TypeScript
39
star
52

go-versions

Go releases for Actions Runner Images
PowerShell
39
star
53

reusable-workflows

Reusable workflows for developing actions
JavaScript
38
star
54

.github

30
star
55

humans.txt

An Action to list out the humans who help feed and tend the robots of GitHub Actions.
JavaScript
29
star
56

versions-package-tools

Libs and tools used to build all *-version tools for GitHub Actions
PowerShell
20
star
57

publish-immutable-action

A GitHub Action used for publishing an Action to ghcr.io as an OCI container.
TypeScript
20
star
58

virtual-environments-packages

Code and scripts used to automate delivery of tool packages used in virtual-environments.
17
star
59

action-versions

Shell
17
star
60

boost-versions

Boost builds for Actions Virtual Environments
PowerShell
5
star
61

alpine_nodejs

Workflow for redistribution of Node.JS for actions/runner
Dockerfile
5
star
62

anno-test

1
star