Add Build Output Messages to Azure DevOps

Azure has a tool that is used in many DevOps YAML scripts. It has many capabilities – to the point of wondering why they crammed so many unrelated functions into one construct. All of this information is from a Microsoft Azure DevOps Services page: Logging commands. Check it out because I will not list these options: Show percentage and/or time to complete,

Log Information with Pretty Colors to Build Output

These formatting commands allow you to write logging messages with colors that are indicative of certain types of information such as Error or Debug.

- powershell: |
    Write-Host "##[group]Beginning of a group"
    Write-Host "##[warning]Warning message"
    Write-Host "##[error]Error message"
    Write-Host "##[section]Start of a section"
    Write-Host "##[debug]Debug text"
    Write-Host "##[command]Command-line being run"
    Write-Host "##[endgroup]"

Results in the following text in the pipeline build step.

Initial display – group is closed.
Log Information with Pretty Colors to Build Output
Display after opening group.

Create Error and Warning Messages

You can also create error and warning messages that show up on the Build Summary window.

- powershell: |
    Write-Host "##vso[task.logissue type=warning;sourcepath=consoleapp/main.cs;linenumber=1;columnnumber=1;code=100;]Warning with all possible parameters."
    Write-Host "##vso[task.logissue type=error]Something went very wrong."

Will generate this display.

Error display.
Warning display.
View of pipeline build step.

Stop Build Execution in PowerShell

Note that if you identify an error in the PowerShell script, you can set the task.complete status to Failed. In PowerShell tasks, it must be done with a Write-Output or Azure may not detect the failure! These messages will show up

- powershell: |
    # Write error message and fail script.
    $msg = "Error message that appears on build "
    Write-Error "##vso[task.logissue type=error] $msg"
    Write-Output "##vso[task.complete result=Failed;]Failed"