46
votes

I have installed the ANSI-color plugin for Jenkins. In the Jobs I have activated that plugin with the default profile 'xterm'.

I cannot figure out how to colorize the output of the Console Log when printing to the log from the batch files (Windows-platform).

The documentation on

https://wiki.jenkins-ci.org/display/JENKINS/AnsiColor+Plugin

is not helpful. There are no examples how to actually print in color.

I tried several different echo/print commands, but I cannot get colors to work.

Any hint appreciated.

6
Note the Windows terminal is not an ANSI terminal, so it won't support ANSI output from batch commands by default. You can read this for some more info - If the Jenkins plug in sees ansi escaped text output in the console log, it will colourise it.ocodo
One other note for Pipeline/workflow you have to wrap the sh in an extra class according to github.com/dblock/jenkins-ansicolor-pluginMarkHu

6 Answers

15
votes

The ANSI-color plug in transforms your console output to HTML. If your output contains ansi escape sequences, then it converts these special char sequences into (colored) HTML. You can only configure the mapping via 'ANSI color map'.

Example:
\x1b[31m is transformes html color red

It looks that your program does't use Escape sequences. But if you wrote your own software or script, you can use these Escape sequences.

1st Example BASH:

echo -e "\033[31mRed\033[0m"

2nd Example BASH:

printf "\033[31mRed\033[0m"

If you need it, you have to add a newline sequence to printf:

printf "\033[31mRed\033[0m\n"

More to Escape sequences:
English: http://en.wikipedia.org/wiki/ANSI_escape_code
Deutsch: http://de.wikipedia.org/wiki/Escape-Sequenz

8
votes

There's a lot more documentation available on the Jenkins-ANSIcolor plugin here: https://github.com/dblock/jenkins-ansicolor-plugin

I wasn't seeing colors because I was using "high intensity" colors (in the 90's range) and these aren't supported. Gotta stick to colors in the 30's

8
votes
  1. Double check that the software really is outputting ANSI colours. For example, a bash script running this echo will produce colour with the AnsiColor plugin installed, but it'll produce messed-up escape sequences in the console output with no plugin.

In BASH:

echo -e '\033[35mPurple!\033[0m'
  1. Check your project configuration, check the "Build Environment" and make sure "Color ANSI Console Output" is checked.

  2. If you can't find the "Build Environment" section, go to Manage Plugins to double-check that the right plugin is really installed.
    (I recently installed the "Ansible" plugin instead of "AnsiColor"....)

5
votes

Jenkins console output is place where you can spend decent amount of time trying to figure out what went wrong (or perhaps right?).

AnsiColor plugins gives you opportunity to color monochromatic Jenkins console output.

Set-by-step guide

  1. Install AnsiColor plugin Under Build Environment section check Color ANSI Console OutputJenkins -
  2. Color ANSI Console Output -should look like

  3. TesterFenster Inside Execute shell step add something like:

set +x


info() {

echo "\033[1;33m[Info]    \033[0m $1"

}

error() {

echo "\033[1;31m[Error]   \033[0m $1"

}

success() {

echo "\033[1;32m[Success] \033[0m $1"

}



info "This is information message"

error "Houston we have a problem"

success "Great!!!"


echo "Foreground colors"

echo "\033[31m Red \033[0m"

echo "\033[32m Green \033[0m"

echo "\033[33m Yellow \033[0m"

echo "\033[34m Blue \033[0m"
siz
echo "\033[35m Magneta \033[0m"

echo "\033[36m Cyan \033[0m"


echo "Background colors"

echo "\033[41m Red \033[0m"

echo "\033[42m Green \033[0m"

echo "\033[43m Yellow \033[0m"

echo "\033[44m Blue \033[0m"

echo "\033[45m Magneta \033[0m"

echo "\033[46m Cyan \033[0m"


echo "Different combinations"

echo "\033[1;31m Red \033[0m"

echo "\033[1;4;37;42m Green \033[0m"

echo "\033[1;43m Yellow \033[0m"


set -x
your output
2
votes

This only works from the Jenkins console, and not from the actual scripts that out put to your console Output

0
votes

The OP asked explicitly about using AnsiColor Jenkins Plugin on Windows, so here is my answer:

cmd.exe is not a terminal emulator

As @ocodo wrote, cmd.exe (which is triggered by Jenkins) is not a terminal emulator, so it won't support ANSI output from batch commands by default. So this is just unsupported on Windows.

These tests were unsuccessful in Jenkins using Windows Batch build step:

echo \e[33m hello \e[0m
echo \033[33m hello \033[0m
echo \x1b[33m hello \x1b[0m
echo \27[33m hello \27[0m
echo \[33m hello \[0m
echo ←[33m hello ←[0m

Last line: To inject an ESC character in a Firefox testbox form, press alt+27, which is also the escape sequence in most editors. more info here: http://www.robvanderwoude.com/ansi.php#AnsiColor and https://groups.google.com/forum/#!topic/jenkinsci-users/0OcjlZQfqrk

A solution: Use Python print for text coloring

The build jobs which use Python for console output had no problem with AnsiColor, so if Python is available in your environment where the console output is generated by a script, you can inject ANSI codes from there. Here's how I worked around it to make it colorful.

  • Create a helper script in util/ansiGreen.py like so:
#!/usr/bin/env python
print("\033[32m");
  • Register as env var at the level of your global build configuration:
set ansiGreen=python %cdRootPath%util\ansiGreen.py
  • Add it to your scripts:
call make-some-tests.bat
if %errorlevel% gtr 0 (
    %ansiRed%
    echo [ERROR] You screwed up trunk!
    %ansiOff%
)

As you can see, %ansiOff% is also required, to end with coloring (Its simply "\033[0m" printed through python). Else everything after %ansiRed% will be in red.

This script is run on a Jenkins slave. To set and get an environment directly within Jenkins batch build step is another challenge.

It's a bit ugly but after a lot of research it seems to be the best workaround to enable AnsiColor to be returned to a Windows Jenkins master from a Windows Jenkins slave.