gpt4 book ai didi

Jenkins 管道 : Set environment variable based on parameter

转载 作者:行者123 更新时间:2023-12-02 00:07:49 24 4
gpt4 key购买 nike

我正在尝试根据传递到作业中的参数在 Jenkins 管道作业中设置环境变量;这样我就可以在需要它的管道的每个阶段使用环境变量。我尝试在环境 block 中使用 switch 语句:

parameters {
choice(name: 'ENVIRONMENT', choices: 'dev\nst\nprod', description: 'Environment')
}
environment {
script {
switch(env.ENVIRONMENT) {
case 'dev':
BRANCH = master
break
case 'st':
BRANCH = 2020Q1
break
case 'prod':
BRANCH = 2019Q4
break
}
}
}

但是这不起作用,该作业尝试评估等号之前的所有行作为 KEY 名称:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 9: "script {
switch(env.ENVIRONMENT) {
case 'dev':
BRANCH" is not a valid identifier and cannot be used for an environment variable. Identifiers must start with a letter or underscore and can contain only letters, numbers or underscores. @ line 9, column 7.
script {

我如何让它工作?

最佳答案

实现这一点的方法很少。这是其中之一:

parameters {
choice(name: 'ENVIRONMENT', choices: 'dev\nst\nprod', description: 'Environment')
}

stages(){
stage("some stage"){
steps {
script{
switch(env.ENVIRONMENT) {
case 'dev':
env.BRANCH = "master"
break
case 'st':
env.BRANCH = "2020Q1"
break
case 'prod':
env.BRANCH = "2019Q4"
break
}
withEnv(["BRANCH=${env.BRANCH}"]) {
...................................
...................................
...................................

}
}
}
}
}

关于 Jenkins 管道 : Set environment variable based on parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60067980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com