The Frontview Icon

The Frontview

Today I Learned

How to handle ANSI colors in Jenkins

Oct 02, 2023
1 min read

Uninterpreted ANSI Escape Codes

I wrote some JavaScript code to run Cypress on a Jenkins server. Inspecting the console output of the Jenkins job that executed my code revealed a mess of uninterpreted ANSI escape codes:

Uninterpreted ANSI escape codes in Jenkins console output

With this amount of junk, it is almost impossible to understand what is actually being printed to the console. Searching for a solution, I found this GitHub issue which pointed me in the right direction.

Fixing the problem

In order for Jenkins to interpret the color codes correctly, the AnsiColor plugin needs to be installed. However, it does not work by default. It also needs to be enabled. There are two ways to do this, documented in the plugin’s README, but you will probably want to use the method that enables the plugin for the entire pipeline job:

diff
pipeline {
// ...
+ options {
+ ansiColor('xterm')
+ }
// ...
}

Adding this little snippet to the pipeline configuration will make Jenkins interpret ANSI escape codes correctly. The console output now looks much better and can actually be read:

Interpreted ANSI escape codes in Jenkins console output