4

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
Matthias M
  • 12,906
  • 17
  • 87
  • 116

3 Answers3

3

For me it worked when putting

export FORCE_COLOR=1

at the top of my script.

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

Raphael Schweikert
  • 18,244
  • 6
  • 55
  • 75
3

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.

Andi
  • 1,172
  • 1
  • 11
  • 16
1

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...)

BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105