5

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.

Andrew
  • 585
  • 1
  • 7
  • 17

2 Answers2

7

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

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 2
    That worked super well thanks so much! Only thing I had to change is the url from `GET https://{instance}/{collection}/{project}/_apis/build/builds/$(build.buildid)/timeline?api-version=5.0` to `http://{instance}/tfs/{collection}/{project}/_apis/build/builds/$(build.buildid)/timeline?api-version=5.0`. I think the GET may have been a mistake but the rest is probably just my specific situation. (Also needed to get an actual PAT) – Andrew Dec 24 '20 at 17:34
3

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

Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36