Nice Thomas
Nice Thomas

Reputation: 11

Jenkinsfile Build Job parameters

In jenkinsfile, I have around 10 jobs and to each Job I am passing parameters as below

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 ],
] } }

Is there a way I can define this section outside and use it to pass to the jobs

I am trying the same query @ Pass (same) parameters to multiple build jobs in a Jenkins pipeline

Thank you

Upvotes: 1

Views: 5398

Answers (1)

Thiru
Thiru

Reputation: 2709

We could pass a map with parameters to build job

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 would look like this:

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

The stage config will look like this:

     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
            }
        }
    }

Upvotes: 1

Related Questions