Use node.js and ANSIcolor plugin in Jenkins

1.3k Views Asked by At

I want to display colored output in jenkins which is produced by node.js

Both work separately, but not combined:

Node Script My test script test.js:

console.log(require("chalk").red("Node Red"))

Calling the test script in the shell works:

node test.js => OK

Calling a colored shell script in jenkins works:

echo -e "\033[31mShell Red\033[0m" => OK

But calling the node script in jenkins does not display any colors:

node test.js => No Color, when executed in jenkins
3

There are 3 best solutions below

0
On

The answer of Raphael pointed me in the right direction. Here my complete solution for a Jenkins Pipeline Script (Scripted Pipeline): :

node {
    ansiColor('xterm') {
        withEnv(['FORCE_COLOR=3']) {
            ...
            sh "some-node-script-using-chalk.js"
            ...
        }
    }
}

If you are using the Declarative Pipeline see https://jenkins.io/doc/pipeline/tour/environment/ how to set environment variables in a Declarative Pipeline Script.

0
On

For me it worked when putting

export FORCE_COLOR=1

at the top of my script.

See https://github.com/chalk/supports-color#info

0
On

I just found the problem in my case :

  • In The Job Configuration
  • Look at the Bindings
  • Check the checkbox named "Color ANSI Console Output"

And it works (for me...)