### Basic setup-dotnet usage with dotnet-version '8.0.x' Source: https://github.com/actions/setup-dotnet/blob/main/README.md Installs the .NET SDK version specified by dotnet-version. Unless a concrete version is set in global.json, the latest installed .NET version on the runner is used by default. ```yaml steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: '8.0.x' - run: dotnet build ``` -------------------------------- ### Install released and preview SDKs in separate steps Source: https://github.com/actions/setup-dotnet/blob/main/README.md Shows how to install a released SDK (10.0.x) and a preview SDK (11.0.x) using separate steps because `dotnet-quality` is a single input shared across all versions in one step. This avoids resolving both versions as previews. ```yaml steps: - uses: actions/setup-dotnet@v6 with: dotnet-version: '10.0.x' - uses: actions/setup-dotnet@v6 with: dotnet-version: '11.0.x' dotnet-quality: 'preview' ``` -------------------------------- ### Multiple version installation with dotnet-version list Source: https://github.com/actions/setup-dotnet/blob/main/README.md Installs multiple .NET SDK versions by providing a newline-separated list in the dotnet-version input. The example installs 8.0.x and 9.0.x. ```yml steps: - uses: actions/checkout@v7 - name: Setup dotnet uses: actions/setup-dotnet@v6 with: dotnet-version: | 8.0.x 9.0.x - run: dotnet build ``` -------------------------------- ### Setup .NET with workloads Source: https://github.com/actions/setup-dotnet/blob/main/README.md Install .NET workloads as part of the SDK setup. The action automatically runs dotnet workload update before installing the specified workloads to refresh manifests and update existing workloads. Ensure workloads are compatible with the runner's OS, architecture, and .NET SDK version; some workloads may require additional installation time. ```yaml steps: - uses: actions/checkout@v5 - name: Setup .NET with workloads uses: actions/setup-dotnet@v6 with: dotnet-version: '9.0.x' workloads: workload1, workload2 # Specify the workloads required for the project, such as wasm-tools, maui, etc. - run: dotnet build ``` -------------------------------- ### Example usage of environment variables Source: https://github.com/actions/setup-dotnet/blob/main/README.md Set environment variables like `DOTNET_INSTALL_DIR` and `NUGET_PACKAGES` in a workflow to customize installation paths and caching. ```yaml build: runs-on: ubuntu-latest env: DOTNET_INSTALL_DIR: "path/to/directory" NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: '8.0.x' cache: true ``` -------------------------------- ### Matrix testing with setup-dotnet Source: https://github.com/actions/setup-dotnet/blob/main/README.md Uses GitHub Actions matrix syntax to install multiple .NET SDK versions (8.0.x, 9.0.x, 10.0.x) via actions/setup-dotnet@v6. Unless a concrete version is specified in global.json, the latest installed .NET version is used by default. ```yaml jobs: build: runs-on: ubuntu-latest strategy: matrix: dotnet: [ '8.0.x', '9.0.x', '10.0.x' ] name: Dotnet ${{ matrix.dotnet }} sample steps: - uses: actions/checkout@v7 - name: Setup dotnet uses: actions/setup-dotnet@v6 with: dotnet-version: ${{ matrix.dotnet }} - name: Execute dotnet run: dotnet build ``` -------------------------------- ### Single version installation: get dotnet-version output Source: https://github.com/actions/setup-dotnet/blob/main/README.md Use the `dotnet-version` output to get the installed .NET SDK version when installing a single version. The output contains the exact version installed by the action. ```yaml - uses: actions/setup-dotnet@v6 id: stepid with: dotnet-version: 8.0.416 - run: echo '${{ steps.stepid.outputs.dotnet-version }}' # outputs 8.0.416 ``` -------------------------------- ### Multiple version installation: get dotnet-version output Source: https://github.com/actions/setup-dotnet/blob/main/README.md When installing multiple .NET SDK versions, the `dotnet-version` output contains the latest version installed by the action. ```yaml - uses: actions/setup-dotnet@v6 id: stepid with: dotnet-version: | 8.0.416 9.0.308 - run: echo '${{ steps.stepid.outputs.dotnet-version }}' # outputs 9.0.308 ``` -------------------------------- ### Install preview quality for a specific SDK version Source: https://github.com/actions/setup-dotnet/blob/main/README.md Uses `dotnet-quality: preview` with `dotnet-version: '8.0.x'` to install the latest preview build. Note that `dotnet-quality` is ignored for version formats other than `A.B`, `A.B.x`, `A`, `A.x`, and `A.B.Cxx` where the major version is higher than 5. ```yaml steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: '8.0.x' dotnet-quality: 'preview' - run: dotnet build ``` -------------------------------- ### Install latest LTS version using dotnet-channel input Source: https://github.com/actions/setup-dotnet/blob/main/README.md Configures setup-dotnet to install the latest LTS release by setting dotnet-version to 'latest' and dotnet-channel to 'LTS'. The dotnet-channel input is only applied when dotnet-version is 'latest'; using it with a specific version logs a warning and ignores the channel. ```yaml steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: latest dotnet-channel: LTS ``` -------------------------------- ### Reuse a preinstalled SDK with check-latest: false Source: https://github.com/actions/setup-dotnet/blob/main/README.md Configure setup-dotnet to reuse an already installed SDK matching the requested version when available, skipping resolution and download. Useful for air-gapped or preloaded runners; the cache and workloads inputs still use the network. The SDK is reused only if it matches the spec, satisfies dotnet-quality, is not older than global.json's sdk.version, and sits next to a runnable dotnet executable. ```yaml steps: - uses: actions/checkout@v7 - name: Reuse a preinstalled SDK when available uses: actions/setup-dotnet@v6 with: dotnet-version: '8.0.x' check-latest: false - run: dotnet build ``` -------------------------------- ### Installation from global.json: get dotnet-version output Source: https://github.com/actions/setup-dotnet/blob/main/README.md When using `dotnet-version` with `global-json-file`, the `dotnet-version` output contains the version resolved from the `global.json` file. ```yaml - uses: actions/setup-dotnet@v6 id: stepid with: dotnet-version: | 9.0.308 10.0.101 global-json-file: "./global.json" # contains version 8.0.416 - run: echo '${{ steps.stepid.outputs.dotnet-version }}' # outputs 8.0.416 ``` -------------------------------- ### Install latest daily build from LTS channel Source: https://github.com/actions/setup-dotnet/blob/main/README.md Combines `dotnet-version: latest`, `dotnet-channel: LTS`, and `dotnet-quality: daily` to target the latest daily build from the LTS channel. ```yaml steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: latest dotnet-channel: LTS dotnet-quality: daily ``` -------------------------------- ### Install multiple SDK versions for a specific architecture Source: https://github.com/actions/setup-dotnet/blob/main/README.md Specifies the `architecture` input to install .NET SDK versions 8.0.x and 9.0.x for x86. The architecture input accepts values like `x64`, `x86`, `arm64`, `amd64`, `arm`, `s390x`, `ppc64le`, and `riscv64`; if omitted, it defaults to the host OS architecture. ```yaml steps: - uses: actions/checkout@v7 - name: Setup dotnet (x86) uses: actions/setup-dotnet@v6 with: dotnet-version: | 8.0.x 9.0.x architecture: x86 - run: dotnet build ``` -------------------------------- ### Skip LTS pre-pass with skip-lts-prepass input Source: https://github.com/actions/setup-dotnet/blob/main/README.md Sets skip-lts-prepass: true to install only the requested dotnet-version, skipping the minimal LTS runtime pre-pass. Use when the runner image already has a dotnet executable or on a clean self-hosted/container environment to avoid extra downloads. Note that dotnet --info may report a different Host: Version than when the pre-pass runs. ```yaml steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: '9.0.x' skip-lts-prepass: true ``` -------------------------------- ### Use global-json-file input to read SDK version from global.json Source: https://github.com/actions/setup-dotnet/blob/main/README.md Specifies the path to a global.json file via the global-json-file input. If the file doesn't exist, the action fails. When both dotnet-version and global-json-file are used, versions from both inputs are installed. Supports rollForward variants like latestPatch, latestFeature, latestMinor, latestMajor; for prerelease versions the exact pinned version is always installed. The sdk.version field must be fully-qualified (e.g., 8.0.100); wildcard and runtime-style versions are not supported. ```yaml steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: global-json-file: csharp/global.json - run: dotnet build working-directory: csharp ``` -------------------------------- ### Matrix testing with temporary global.json creation Source: https://github.com/actions/setup-dotnet/blob/main/README.md Extends the matrix workflow by creating a temporary global.json that pins the SDK version to the one installed by setup-dotnet, controlling which .NET version is used. On Windows, run the command with PowerShell Core (pwsh) or bash to avoid formatting issues that could cause .NET commands to fail. ```yaml jobs: build: runs-on: ubuntu-latest strategy: matrix: dotnet: [ '8.0.x', '9.0.x', '10.0.x' ] name: Dotnet ${{ matrix.dotnet }} sample steps: - uses: actions/checkout@v7 - name: Setup dotnet uses: actions/setup-dotnet@v6 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 ``` -------------------------------- ### Cache NuGet packages in monorepos with cache-dependency-path Source: https://github.com/actions/setup-dotnet/blob/main/README.md Uses cache-dependency-path to specify a lock file in a subdirectory for monorepo setups. Sets NUGET_PACKAGES to a workspace-local path to reduce cache size. ```yaml env: NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: 8.x cache: true cache-dependency-path: subdir/packages.lock.json - run: dotnet restore --locked-mode ``` -------------------------------- ### Recommended permissions for setup-dotnet Source: https://github.com/actions/setup-dotnet/blob/main/README.md Set the `contents: read` permission in your workflow to ensure proper functionality of the `setup-dotnet` action. ```yaml permissions: contents: read # access to check out code and install dependencies ``` -------------------------------- ### Set DOTNET_INSTALL_DIR from runtime variables Source: https://github.com/actions/setup-dotnet/blob/main/README.md Set `DOTNET_INSTALL_DIR` to a value based on runtime variables like `$HOME/.dotnet` before the `setup-dotnet` step. On some self-hosted or large Linux runners, the default `/usr/share/dotnet` location may fail due to insufficient permissions; use a user-writable path. ```yaml - name: Set DOTNET_INSTALL_DIR run: echo "DOTNET_INSTALL_DIR=$HOME/.dotnet" >> $GITHUB_ENV ``` -------------------------------- ### Publish to nuget.org Source: https://github.com/actions/setup-dotnet/blob/main/README.md Publish a package to nuget.org using the setup-dotnet action and the dotnet nuget push command. Requires a NUGET_AUTH_TOKEN environment variable set to a NuGet API key. This is the only way to push to nuget.org feed for macOS/Linux machines due to API key config store limitations. ```yaml - uses: actions/setup-dotnet@v6 with: dotnet-version: 8.0.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 }} ``` -------------------------------- ### Cache NuGet packages with cache input and lock file Source: https://github.com/actions/setup-dotnet/blob/main/README.md Enables built-in caching of NuGet global packages. The action searches for packages.lock.json in the repository root, calculates its hash for the cache key, and errors if the lock file doesn't exist. Use cache-dependency-path for multiple or differently located dependency files. Caching is available since .NET SDK 2.1.500 and 2.2.100. Only the global-packages folder is restored, so NU1403 may occur; use DisableImplicitNuGetFallbackFolder to avoid it. ```yaml steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: 8.x cache: true - run: dotnet restore --locked-mode ``` ```xml true ``` -------------------------------- ### GitHub Package Registry (GPR) authentication and publish Source: https://github.com/actions/setup-dotnet/blob/main/README.md Configure authentication for GitHub Package Registry using the GITHUB_TOKEN secret. The source-url points to the GPR index for the organization. The workflow checks out the repository, sets up .NET 8.0.x, builds, packs, and pushes the package to GPR. ```yaml steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: '8.0.x' source-url: https://nuget.pkg.github.com//index.json env: NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} - run: dotnet build - name: Create the package run: dotnet pack --configuration Release - name: Publish the package to GPR run: dotnet nuget push /bin/Release/*.nupkg ``` -------------------------------- ### Reduce caching size with NUGET_PACKAGES environment variable Source: https://github.com/actions/setup-dotnet/blob/main/README.md Sets the NUGET_PACKAGES environment variable to a workspace-local path to reduce cache size. Recommended when available, as some runners already have large libraries (e.g., Xamarin). ```yaml env: NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages steps: - uses: actions/checkout@v7 - uses: actions/setup-dotnet@v6 with: dotnet-version: 8.x cache: true - run: dotnet restore --locked-mode ``` -------------------------------- ### Azure Artifacts authentication and publish Source: https://github.com/actions/setup-dotnet/blob/main/README.md Configure authentication for Azure Artifacts using a PAT stored in the NUGET_AUTH_TOKEN environment variable. The source-url points to the Azure DevOps feed. The workflow publishes the package to Azure Artifacts using dotnet nuget push. ```yaml - uses: actions/setup-dotnet@v6 with: source-url: https://pkgs.dev.azure.com//_packaging//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 /bin/Release/*.nupkg ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.