问题描述
要解释这个问题,请考虑我有2个詹金斯工作.
To explain the issue, consider that I have 2 jenkins jobs.
工作1:PARAM_TEST1
Job1 : PARAM_TEST1
它接受名为"MYPARAM"的参数化值
it accepts a parameterized value called 'MYPARAM'
工作2:PARAM_TEST2
Job2: PARAM_TEST2
它还接受名为"MYPARAM"的参数化值
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.
它还接受名为"MYPARAM"的参数化值,以将其简单地传递到构建作业步骤.
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}")]
}
}
}
}
我的问题:
这个例子很简单.其实我有20个工作.我不想在每个阶段都重复parameters: [string(name: 'MYPARAM', value: "${params.MYPARAM}")]
.
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?
推荐答案
您可以做的是将通用参数放在管道级别上,并将特定的参数添加到阶段中
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
}
}
}
}
}
这篇关于Jenkinsfile-如何为所有阶段传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!