在Jenkins Pipeline中,如何放置ansiColor Jenkins插件的包装器?

24

我不确定该如何处理Jenkins声明性流水线。

按照这个例子: https://github.com/jenkinsci/ansicolor-plugin

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

以上片段应该放在哪里?

这是我的简单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
        '''
      }
    }
   }
 }

包装器是否围绕阶段?它是围绕每个阶段吗?

4个回答

40
可以像下面这样在选项块中合并配置
options {
  buildDiscarder(logRotator(numToKeepStr:'10'))
  timeout(time: 5, unit: 'MINUTES')
  ansiColor('xterm')
}

6
当在 Jenkins 2.98 和 Pipeline 2.5 上使用脚本流水线时,这种方法无法正常工作。 - gileri
5
有一个支持该功能的票据:https://github.com/jenkinsci/ansicolor-plugin/issues/118 - agabrys
2
GitHub问题目前仍然是开放状态,但这似乎现在已经正常工作了(Jenkins 2.121.1和pipeline 2.5)。 - Andy Shinn

10

我像这样把我的放在每个阶段:

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

1
我在之前的回答中已经成功配置过了。 - Eric Francis

9

我把这个放在选项部分,适用于管道中的所有阶段和步骤

pipeline {
  agent any 

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

@phani,你能否详细解释一下? - Hieu Huynh

4
在Jenkins的脚本管道中,您可以这样使用它:

在一个脚本化的Jenkins管道中,您可以像这样使用它:

stage('Pre-Checks') {  
    timeout(time: 3, unit: 'MINUTES') {
        ansiColor('xterm') {
            sh 'python scripts/eod/pre_flight_check.py'
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接