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

18,439

Solution 1

Able to consolidate config in the options block like so

options {
  buildDiscarder(logRotator(numToKeepStr:'10'))
  timeout(time: 5, unit: 'MINUTES')
  ansiColor('xterm')
}

Solution 2

I put mine in each stage like this:

stage('Initialize') {
  ansiColor('xterm') {
    // do stuff
  }
}

Solution 3

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

pipeline {
  agent any 

  options {
    ansiColor('xterm')
  }
...
}

Solution 4

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'
        }
    }
}
Share:
18,439

Related videos on Youtube

Eric Francis
Author by

Eric Francis

LinkedIn

Updated on September 15, 2022

Comments

  • Eric Francis
    Eric Francis almost 2 years

    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?

  • Eric Francis
    Eric Francis about 7 years
    Was able to configure once in the answer I gave
  • gileri
    gileri over 6 years
    This doesn't work when used in scripted pipelines on Jenkins 2.98 and Pipeline 2.5.
  • agabrys
    agabrys over 6 years
    There is a ticket to add support for it: github.com/jenkinsci/ansicolor-plugin/issues/118
  • Andy Shinn
    Andy Shinn about 6 years
    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).
  • Hieu Huynh
    Hieu Huynh over 4 years
    @phani Could you please explain more?