Jenkinsfile Build Job参数

时间:2019-10-23 07:20:25

标签: jenkins jenkins-pipeline

在jenkinsfile中,我大约有10个工作,我向每个工作传递以下参数

stage('Test1 )
steps {
  script {
      echo 'Starting "test1"'
      build job: './test1
parameters: [
  [$class: 'StringParameterValue', name: 'INSTANCE_NAME', value: params.INSTANCE_NAME ],
  [$class: 'StringParameterValue', name: 'WORKSPACE', value: params.WORKSPACE ],
  [$class: 'StringParameterValue', name: 'APP_NAME', value: 'test' ],
  [$class: 'StringParameterValue', name: 'GIT_BRANCH', value: params.GIT_BRANCH ],
] } }
stage('Test2 )
steps {
  script {
      echo 'Starting "test2"'
      build job: './test2' ,
parameters: [
  [$class: 'StringParameterValue', name: 'INSTANCE_NAME', value: params.INSTANCE_NAME ],
  [$class: 'StringParameterValue', name: 'WORKSPACE', value: params.WORKSPACE ],
  [$class: 'StringParameterValue', name: 'APP_NAME', value: 'test' ],
  [$class: 'StringParameterValue', name: 'GIT_BRANCH', value: params.GIT_BRANCH ],
] } }

有没有一种方法可以在外部定义此部分并将其传递给工作

我正在尝试使用同一查询@ Pass (same) parameters to multiple build jobs in a Jenkins pipeline

谢谢

1 个答案:

答案 0 :(得分:0)

我们可以传递带有参数的地图来构建作业

def jobParameters = [:]
jobParameters['INSTANCE_NAME'] = params.INSTANCE_NAME
jobParameters['WORKSPACE'] = params.WORKSPACE
def paramsObjects = []
jobParameters.each {
  key, value ->
    paramsObjects.push([$class: 'StringParameterValue', name: key, value: value])
}    

paramsObjects看起来像这样:

[
    [$class:StringParameterValue, name:param1, value:value1],
    [$class:StringParameterValue, name:param2, value:value2]
]

舞台配置如下:

     stages {
        stage('Test1')
        steps {
            script {
                echo 'Starting "test1"'
                build job: './test1'
                parameters:
                paramsObjects
            }
        }
        stage('Test2')
        steps {
            script {
                echo 'Starting "test2"'
                build job: './test2',
                        parameters: paramsObjects
            }
        }
    }