Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure pipeline ANSI colorcode support

I hope somebody can clarify this issue I am facing. I have an azure pipeline that its job is to compare 2 files and find differences, goes without saying that the pipeline works just fine and it does output the differences (I am using difflib).

for better readability I tried to implement some color code, to print the differences in different colours using this python code.

try:
    from colorama import Fore, Back, Style, init
    init()
except ImportError:  # fallback so that the imported classes always exist
    class ColorFallback():
        __getattr__ = lambda self, name: ''
    Fore = Back = Style = ColorFallback()

def color_diff(diff):
    for line in diff:
        if line.startswith('+'):
            yield Fore.GREEN + line + Fore.RESET
        elif line.startswith('-'):
            yield Fore.RED + line + Fore.RESET
        elif line.startswith('^'):
            yield Fore.BLUE + line + Fore.RESET
        elif line.startswith('!'):
            yield Fore.YELLOW + line + Fore.RESET
        else:
            yield line

if I run my script locally, I can get the desired output with specific colours declared based on specific conditions, but when I use the same file in a azure pipeline, the output is monochromatic as default.

Is this a feature not yet supported by Microsoft or I am missing something?

Thank you so much for your time and help

like image 926
Nayden Van Avatar asked Jan 26 '26 19:01

Nayden Van


1 Answers

Colors in azure devops pipelines are finicky and I don't believe they are fully supported but we can do a little hack.

azure devops formatting color in pipeline

can't post pic because rep is too low but text displays like this

I don't know python but I believe you could do the following:

def color_diff(diff):
    for line in diff:
        if line.startswith('+'):
            yield ##[section] + line
        elif line.startswith('-'):
            yield ##[error] + line
        elif line.startswith('^'):
            yield ##[command] + line
        elif line.startswith('!'):
            yield ##[warning] + line
        else:
            yield line

The pipeline will pick up the following text commands and display the proper color for each. The only downside is ##[warning], ##[error], ##[debug] will also display in your pipeline text whereas ##[section] and ##[command] just change the text color.

like image 188
jasonc Avatar answered Jan 29 '26 08:01

jasonc