问题描述
为了解释这个问题,假设我有 2 个 jenkins 工作.
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'
有时我需要按顺序运行这 2 个作业 - 所以我创建了一个单独的管道作业,如下所示.它工作得很好.
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 - 如何为所有阶段传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!