Skip to content

Commit 4a9e01f

Browse files
authored
Add section on audit errors on CI only (#3491)
1 parent 49e5e25 commit 4a9e01f

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

docs/concepts/Auditing-Packages.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,39 @@ Alternatively, if you want to keep low and moderate vulnerabilities as warnings,
115115
> [!NOTE]
116116
> MSBuild properties for message severity such as `NoWarn` and `TreatWarningsAsErrors` are not supported for packages.config projects.
117117
118-
## Ensure restore audited projects
118+
## Running NuGet Audit in CI
119+
120+
### Separating Errors from Warnings with a Dedicated Auditing Pipeline
121+
122+
You can use MSBuild's conditional statements to configure a dedicated CI pipeline for running audits, without audit warnings being treated as errors in other pipelines or on local builds.
123+
Depending on your CI system and team processes, you can have failed runs of the audit pipeline email the team, or you may have a dashboard where you can show a badge of the most recent run of the pipeline.
124+
125+
Like many things in programming, there are multiple ways to achieve the outcome.
126+
One option is to treat NuGet Audit warnings as errors only in an audit pipeline.
127+
128+
```xml
129+
<PropertyGroup>
130+
<NuGetAuditCodes>NU1900;NU1901;NU1902;NU1903;NU1904;NU1905</NuGetAuditCodes>
131+
<WarningsAsErrors Condition=" '$(AuditPipeline)' == 'true' ">$(WarningsAsErrors);$(NuGetAuditCodes)</WarningsAsErrors>
132+
<WarningsNotAsErrors Condition=" '$(AuditPipeline)' != 'true' ">$(WarningsNotAsErrors);$(NuGetAuditCodes)</WarningsNotAsErrors>
133+
</PropertyGroup>
134+
```
135+
136+
Then in your pipeline, you run restore specifying the property used by the condition.
137+
For example, using GitHub Actions syntax:
138+
139+
```yml
140+
- name: Restore with NuGet Auditing
141+
run: dotnet restore -p:AuditPipeline=true
142+
```
143+
144+
The property name `AuditPipeline` is only an example, and you can customize it as you wish, as long as the name is the same in both the MSBuild condition and the command line.
145+
MSBuild also uses environment variables when reading a property that has not yet been defined, so an environment variable is an alternative to the command line parameter.
146+
147+
By using conditions to selectively cause NuGet Audit warnings to fail a restore, you can have a dedicated pipeline to check packages for known vulnerabilities, while preventing new security advisories from blocking your bug fixes at inconvenient times.
148+
Keeping NuGet Audit warnings enabled for local builds allows developers to get a non-blocking notification about new security advisories and can encourage upgrading package versions to fix the vulnerabilities more quickly than waiting for someone to check the audit pipeline status.
149+
150+
### Ensure restore audited projects
119151

120152
NuGet in MSBuild 17.13 and .NET 9.0.200 added output properties `RestoreProjectCount`, `RestoreSkippedCount` and `RestoreProjectsAuditedCount` on the restore task.
121153
This can be used to enforce that audit ran during a restore.

0 commit comments

Comments
 (0)