0
votes

I have configured jenkins pipeline which has multiple string parameters for each job. The parameters are common for all build jobs. I would like to set these parameters on global level so that I can be consumed across all stages. Along with stages I have a retry logic which tries for 2 times after first failure. Once it crosses 3 tries job bails.

At the moment I have configure parameters twice, first in initial build trigger and second in retry block. This is duplicate definition. Also at any time the parameters are same across all.

I tried to define parameters at top before stages begin, but these parameters are not used by the stages even though job defined in build step require the parameters.

pipeline {
    agent {
        label 'master'
    }
    parameters
    {
    string(name: 'VERSION', defaultValue: '', description: 'VERSION No.')
    string(name: 'DEPLOY_TYPE', defaultValue: '', description: 'DEPLOY_TYPE')
    string(name: 'TICKET', RELEASE: '$RELEASE', description: 'Release No.')
    }

stage ("Stage1") {
            steps {
            script {
            def retryAttempt = 0
               try{
                build(
                    job: 'Job1',
                    parameters: [
                        [
                            $class: 'StringParameterValue',
                            name: 'VERSION',
                            value: "${VERSION}"
                        ],
                        [
                            $class: 'StringParameterValue',
                            name: 'DEPLOY_TYPE',
                            value: "${DEPLOY_TYPE}"
                        ],
                        [
                            $class: 'StringParameterValue',
                            name: 'TICKET',
                            value: "${TICKET}"
                        ]
                    ]
                )
                }
                catch(error) {
                 echo "First build failed, let's retry if accepted"
                 retry(2) {
                 build(
                    job: 'Job1',
                    parameters: [
                        [
                            $class: 'StringParameterValue',
                            name: 'VERSION',
                            value: "${VERSION}"
                        ],
                        [
                            $class: 'StringParameterValue',
                            name: 'DEPLOY_TYPE',
                            value: "${DEPLOY_TYPE}"
                        ],
                        [
                            $class: 'StringParameterValue',
                            name: 'TICKET',
                            value: "${TICKET}"
                        ]
                    ]
                )
                 }
               }
            }
          }
    }

I want to use global parameters define once in pipeline and use all parameters across all stages.

1
Global variables should define before pipeline and you will be able to use them in all stages. def YOUR_VARIABLE pipeline { agent { label 'master' }....}milanbalazs
thanks @milanbalazs. I tried your suggestion for variables which worked well. If you see my question i was trying to use the parameters in first step also in retry block for each job if there is any failure. Since the parameters are same across all the stages I would like to know if there is any feature to define only once may be beginning of pipeline and use those parameters across all stages even in retry block.user3487120
As I know you can define the global variables before pipeline. def my_var pipeline { agent any stages {.... Here is a very similar question: stackoverflow.com/questions/47716933/…milanbalazs

1 Answers

0
votes

Because your parameters are the same withing all jobs you can pass the params as a list instead of individually.

def copyOfJobParams = currentBuild.rawBuild.getAction(ParametersAction).getParameters().collect()
build( job: 'Job1', parameters: copyOfJobParams)

Note if you're using declarative pipeline you'll need to wrap this in a script{} block