Andrew
Andrew

Reputation: 737

Change Azure DevOps pipeline build status when warnings are thrown

We have an Azure DevOps pipeline which uses self hosted Windows agents with Azure DevOps server 2019. The pipeline runs our front-end tests without any problems. However, occasionally our linting step will find problems that it throws as warnings (such as unused variables). This is what we want it to do but the issue is that these warnings were not being elevated. So the only way to see them was to look in the build execution.

This we were able to resolve by adding a vso formatter to the linting command: npm run nx run-many -- --target="lint" --all --skip-nx-cache=true --parallel --format=vso. So now the warnings are thrown like this:

Warning screenshot

As shown in the green box the warnings are displaying properly. However, in the red circles the status of the build, job, and linting task are success. Is there a way I can mark this build, job, and task as warning so we know to take a further look? Thank you for any help, please let me know if I can provide additional information.

Upvotes: 9

Views: 10232

Answers (2)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35334

You can add a powershell task at the end of the pipeline, and then run the Rest API(Timeline - Get) to traverse the warning messages in the previous task. Finally, you can use the logging command to set the pipeline status

Here is PowerShell Sample:

$token = "PAT"

$url=" https://{instance}/{collection}/{project}/_apis/build/builds/$(build.buildid)/timeline?api-version=5.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$count = 0

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json

ForEach( $issues in $response.records.issues )
{
  if($issues.type -eq "warning")
  {
    echo $issues.Message
    $count ++
  }
  
} 

echo $count

if($count -ne 0 )
{ 

  Write-Host "##vso[task.complete result=SucceededWithIssues;]"
   
}

Result:

enter image description here

Upvotes: 10

Janusz Nowak
Janusz Nowak

Reputation: 2848

There is Azure DevOps Extension/Task created by Microsoft "Build Quality Checks" So you can use it to set up an additional option to force specific build qulity. enter image description here

Upvotes: 4

Related Questions