nightroman / Invoke-Build

Build Automation in PowerShell

Home Page:https://github.com/nightroman/Invoke-Build/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Color and formatting text in Azure DevOps Pipelines

kahnman opened this issue · comments

When I use Invoke-Build on my local machine, the "Task" and "Done" console output lines are colored light blue, which is very helpful when reading the output. When using Invoke-Build as part of a build pipeline in Azure Devops, all of that coloration is lost.

Is there a way for Invoke-Build to utilize the Azure DevOps formatting options?

steps:
- 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]"

Output Example

If this isn't currently an option, can this issue be considered a feature request? Thanks!

I found this Pester issue talking about a similar request to mine. Looks like the Pester developers opted to use ANSI escape codes for output. Maybe a similar route could be taken for Invoke-Build if not already an option?

IB uses escape sequences when it runs in PS v7.2+
See GH action tests output -- https://github.com/nightroman/Invoke-Build/actions/runs/3298362700/jobs/5440381359

What is the PS version in Azure DevOps Pipelines?
And does Azure DevOps Pipelines support escape sequences?

As for your request, I think you can do this using Custom task headers and footers. Instead of typical Write-Build with color codes use Write-Host "##[color]..." as Azure requires.

If it works, please share your solution here, so that I add this to the IB sample, to help others.

Thanks for the quick response @nightroman! My build server is using PowerShell 7.1.3, so let me upgrade to PS 7.2+ and see if that fixes my issue.

I primarily want coloration, which sounds like is already supported in PS 7.2+ and that should be fine for me, but if I decide to use those specific Azure DevOps formatting options, I'll try using Custom task headers and footers as you suggested and comment with my results here to help other.

Thanks!

@kahnman Please let me know how it worked for you.

@nightroman I haven't had time to update PowerShell to 7.2 on my build server to test the built in colorization of Write-Build, but I hope to have time to do that this week.

I did, however, test using the Azure DevOps formatting options in a custom task header and footer and that worked like a charm.

Example:

Set-BuildHeader {
    param($Path)
    Write-Host "##[section]Task $Path"
}

I will update this issue again once I have updated my build server to PowerShell 7.2+.

Cheers!

@nightroman I have confirmed that after updating PowerShell to 7.2.7 on my build server, the output in my Azure Pipeline is colorized without any changes being made to by build scripts or Invoke-Build itself. Thank you for your guidance and great work!