Jenkinsfile - How to pass parameters for all the stages

帅比萌擦擦* 提交于 2019-12-08 00:29:29

问题


To explain the issue, consider that I have 2 jenkins jobs.


Job1 : PARAM_TEST1

it accepts a parameterized value called 'MYPARAM'


Job2: PARAM_TEST2

it also accepts a parameterized value called 'MYPARAM'


Sometimes I am in need of running these 2 jobs in sequence - so i created a separate pipeline job as shown below. It works just fine.

it also accepts a parameterized value called 'MYPARAM' to simply pass it to the build job steps.

pipeline {
    agent any
    stages {
        stage("PARAM 1") {
            steps {
                build job: 'PARAM_TEST1', parameters: [string(name: 'MYPARAM', value: "${params.MYPARAM}")]
            }
        }
        stage("PARAM 2") {
            steps {
                build job: 'PARAM_TEST2', parameters: [string(name: 'MYPARAM', value: "${params.MYPARAM}")]
            }
        }     
    }
}

My question:

This example is simple. Actually I have 20 jobs. I do not want to repeat parameters: [string(name: 'MYPARAM', value: "${params.MYPARAM}")] in every single stage.

Is there any way to set the parameters for all the build job steps in one single place?


回答1:


What you could do is place the common params on the pipeline level and add specific ones to those in the stages

pipeline {
    agent any
    parameters {
        string(name: 'PARAM1', description: 'Param 1?')
        string(name: 'PARAM2', description: 'Param 2?')
    }
    stages {
        stage('Example') {
            steps {
                echo "${params}"
                script {
                    def myparams = params + string(name: 'MYPARAM', value: "${params.MYPARAM}")
                    build job: 'downstream-pipeline-with-params', parameters: myparams
                }    
            }
        }
    }
}


来源:https://stackoverflow.com/questions/54772043/jenkinsfile-how-to-pass-parameters-for-all-the-stages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!