Where to put the wrapper for ansiColor Jenkins plugin in Jenkins Pipeline?
Asked Answered
W

4

24

I'm unsure of what to do with declarative jenkins pipeline.

Following the example here: https://github.com/jenkinsci/ansicolor-plugin

wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
  sh 'something that outputs ansi colored stuff'
}

Where does the above snippet go?

Here is my simple Jenkinsfile:

#!groovy

pipeline {
  agent any

  // Set log rotation, timeout and timestamps in the console
  options {
    buildDiscarder(logRotator(numToKeepStr:'10'))
    timeout(time: 5, unit: 'MINUTES')
  }

  stages {

    stage('Initialize') {
      steps {

        sh '''
          java -version
          node --version
          npm --version
        '''
      }
    }
   }
 }

Does the wrapper go around stages? Does it go around each stage?

Waggish answered 16/5, 2017 at 13:55 Comment(0)
W
40

Able to consolidate config in the options block like so

options {
  buildDiscarder(logRotator(numToKeepStr:'10'))
  timeout(time: 5, unit: 'MINUTES')
  ansiColor('xterm')
}
Waggish answered 16/5, 2017 at 19:35 Comment(3)
This doesn't work when used in scripted pipelines on Jenkins 2.98 and Pipeline 2.5.Miry
There is a ticket to add support for it: github.com/jenkinsci/ansicolor-plugin/issues/118Adulterant
The GitHub issue is still open at this time but this actually seems to work properly now (Jenkins 2.121.1 and pipeline 2.5).Salba
D
10

I put mine in each stage like this:

stage('Initialize') {
  ansiColor('xterm') {
    // do stuff
  }
}
Decathlon answered 16/5, 2017 at 16:34 Comment(1)
Was able to configure once in the answer I gaveWaggish
I
9

I put this in options section, apply for all stages and steps in pipeline

pipeline {
  agent any 

  options {
    ansiColor('xterm')
  }
...
}
Inconstant answered 26/3, 2018 at 9:13 Comment(1)
@phani Could you please explain more?Inconstant
C
5

In a scripted jenkins pipeline, you can use it like this:

stage('Pre-Checks') {  
    timeout(time: 3, unit: 'MINUTES') {
        ansiColor('xterm') {
            sh 'python scripts/eod/pre_flight_check.py'
        }
    }
}
Cowie answered 7/2, 2020 at 1:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.