How can I break the build with dotnet list --deprecated in a Azure Pipeline

Viewed 682

I want to check my solution for the use of deprecated NuGet-Packages.

So I added

- task: dotNetCoreCLI@2
  name: checkDeprecatedNuGet
  inputs:
    command: 'custom'
    projects: '**/*.sln'
    custom: 'list'
    arguments: 'package --deprecated'

Now it lists the deprecated packages but the build is successfull.

Is there a possibility to break the build in this case?

2 Answers

This is the solution I use now:

$projectDirectory = "$(Agent.BuildDirectory)/s/$(RepoName)"
$solutions = Get-ChildItem -Path $projectDirectory/** -Name -Include *.sln
foreach ($solution in $solutions)
{
  $output = dotnet list $projectDirectory/$solution package --deprecated
  $errors = $output | Select-String '>'
  
  if ($errors.Count -gt 0)
  {
    foreach ($err in $errors)
    {
      Write-Host "##vso[task.logissue type=error]Reference to deprecated NuGet-package $err"
    }
    exit 1
  }
  else
  {
    Write-Host "No deprecated NuGet-package"
    exit 0
  }
}

If you mean you want to break the build when there are deprecated packages, I don't think you can achieve it in dotNetCoreCLI task. When this task runs successfully, it will be treated as "pass".

You could try to use powershell task to run dotNetCore commands, and write an error to fail the build if there are deprecated packages:

# Writes an error to build summary and to log in red text
Write-Host  "##vso[task.LogIssue type=error;]This is the error"

If you want this error to fail the build, then add this line:

exit 1
Related