我有一个使用声明性管道的项目,用于 C++ 项目。 我希望有多个作业运行具有不同配置的相同管道。 例如。发布或调试版本。使用不同的编译器。一些使用地址消毒剂或线程消毒剂的作业。
其中一些作业应该在每次提交时运行。其他作业应该每天运行一次。
我的想法是通过让管道依赖于环境变量或参数来控制这一点。
我现在有一个 JenkinsFile,其中所有阶段都取决于环境变量。 不幸的是,我找不到从外部控制变量的地方。 有什么方法可以在不违反 DRY 原则的情况下在作业级别控制这些环境变量吗?
基于两个变量的简化示例。
pipeline {
agent {
label 'linux_x64'
}
environment {
CC = '/usr/bin/gcc-4.9'
BUILD_CONF = 'Debug'
}
stages {
stage('Build') {
steps {
cmakeBuild buildDir: 'build', buildType: "${env.BUILD_CONF}", installation: 'InSearchPath', sourceDir: 'src', steps: [[args: '-j4']]
}
}
stage('Test') {
sh 'test.sh'
}
}
我现在想要两个创建第二个作业,运行相同的管道,但使用 CC='/usr/bin/clang' 和 BUILD_CONF='Release'。
在我的实际项目中,我有更多的变量,我想测试大约十种组合。
答案 0 :(得分:0)
是的,它可以在一个管道中使用,但使用 Jenkins 脚本管道会更容易,如下例所示:
node {
def x=[
[CC: 10, DD: 15],
[CC: 11, DD: 16],
[CC: 12, DD: 17]
]
x.each{
stage("stage for CC - ${it.CC}") {
echo "CC = ${it.CC} & DD = ${it.DD}"
}
}
}
所以在你的情况下,你可以像下面这样装饰你的管道:
node {
// define all your expected key and value here like an array
def e = [
[CC: '/usr/bin/gcc-4.9', BUILD_CONF: 'Debug'],
[CC: '/usr/bin/clang', BUILD_CONF: 'Release']
]
// Iterate and prepare the pipeline
e.each {
stage("build - ${it.BUILD_CONF}") {
cmakeBuild buildDir: 'build', buildType: "${it.BUILD_CONF}", installation: 'InSearchPath', sourceDir: 'src', steps: [[args: '-j4']]
}
stgae("test - ${it.BUILD_CONF}") {
sh 'test.sh'
}
}
}
通过这种方式,您可以使用定义的数组重复使用您的阶段,例如 e