Skip to content

Commit ff759d8

Browse files
authored
Fix .NET caching prerequisites and test artifact workflow (#46336)
1 parent 847d995 commit ff759d8

1 file changed

Lines changed: 35 additions & 20 deletions

File tree

  • content/actions/tutorials/build-and-test-code

‎content/actions/tutorials/build-and-test-code/net.md‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,21 @@ steps:
145145

146146
### Caching dependencies
147147

148-
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`.
148+
You can cache NuGet dependencies for future workflows using the optional `cache` input. Caching requires a `packages.lock.json` file. If the action cannot find a matching lock file, it fails.
149+
150+
Before enabling caching:
151+
152+
1. In the directory containing your project or solution, generate the lock files locally.
153+
154+
```shell
155+
dotnet restore --use-lock-file
156+
```
157+
158+
1. Commit the generated `packages.lock.json` files to your repository so they are available when the workflow runs.
159+
160+
For more information, see [Locking dependencies](https://learn.microsoft.com/nuget/consume-packages/package-references-in-project-files#locking-dependencies) in the NuGet documentation.
161+
162+
By default, `setup-dotnet` looks for a lock file in the repository root. Use `cache-dependency-path` to specify lock files in subdirectories or multiple lock files. The following example caches the NuGet `global-packages` folder using lock files throughout the repository, then restores the dependencies in locked mode.
149163

150164
For more information, see [AUTOTITLE](/actions/reference/workflows-and-actions/dependency-caching).
151165

@@ -157,8 +171,9 @@ steps:
157171
with:
158172
dotnet-version: '6.x'
159173
cache: true
160-
- name: Install dependencies
161-
run: dotnet add package Newtonsoft.Json --version 12.0.1
174+
cache-dependency-path: '**/packages.lock.json'
175+
- name: Restore dependencies
176+
run: dotnet restore --locked-mode
162177
```
163178

164179
> [!NOTE]
@@ -202,23 +217,23 @@ jobs:
202217
matrix:
203218
dotnet-version: [ '3.1.x', '6.0.x' ]
204219
205-
steps:
206-
- uses: {% data reusables.actions.action-checkout %}
207-
- name: Setup dotnet
208-
uses: {% data reusables.actions.action-setup-dotnet %}
209-
with:
210-
dotnet-version: {% raw %}${{ matrix.dotnet-version }}{% endraw %}
211-
- name: Install dependencies
212-
run: dotnet restore
213-
- name: Test with dotnet
214-
run: dotnet test --no-restore --logger trx --results-directory {% raw %}"TestResults-${{ matrix.dotnet-version }}"{% endraw %}
215-
- name: Upload dotnet test results
216-
uses: {% data reusables.actions.action-upload-artifact %}
217-
with:
218-
name: {% raw %}dotnet-results-${{ matrix.dotnet-version }}{% endraw %}
219-
path: {% raw %}TestResults-${{ matrix.dotnet-version }}{% endraw %}
220-
# Use always() to always run this step to publish test results when there are test failures
221-
if: {% raw %}${{ always() }}{% endraw %}
220+
steps:
221+
- uses: {% data reusables.actions.action-checkout %}
222+
- name: Setup dotnet
223+
uses: {% data reusables.actions.action-setup-dotnet %}
224+
with:
225+
dotnet-version: {% raw %}${{ matrix.dotnet-version }}{% endraw %}
226+
- name: Install dependencies
227+
run: dotnet restore
228+
- name: Test with dotnet
229+
run: dotnet test --no-restore --logger trx --results-directory {% raw %}"TestResults-${{ matrix.dotnet-version }}"{% endraw %}
230+
- name: Upload dotnet test results
231+
uses: {% data reusables.actions.action-upload-artifact %}
232+
with:
233+
name: {% raw %}dotnet-results-${{ matrix.dotnet-version }}{% endraw %}
234+
path: {% raw %}TestResults-${{ matrix.dotnet-version }}{% endraw %}
235+
# Use always() to always run this step to publish test results when there are test failures
236+
if: {% raw %}${{ always() }}{% endraw %}
222237
```
223238

224239
## Publishing to package registries

0 commit comments

Comments
 (0)