Jenkins 在文件中定义特定于分支的环境变量
我希望根据multi-piepline作业正在处理的分支,按发布阶段将环境变量传递给 虽然下面的这个例子很有效,但我不喜欢这样的结局:每个分支有一个额外的阶段。因为我正在使用Jenkins 在文件中定义特定于分支的环境变量,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我希望根据multi-piepline作业正在处理的分支,按发布阶段将环境变量传递给 虽然下面的这个例子很有效,但我不喜欢这样的结局:每个分支有一个额外的阶段。因为我正在使用with CREDENTIALS插件,所以我需要在该块之前设置MY_CREDENTIALS变量。有没有更优雅的方法来解决这个问题 pipeline { agent { dockerfile true } stages { stage("Config vars for
with CREDENTIALS
插件,所以我需要在该块之前设置MY_CREDENTIALS
变量。有没有更优雅的方法来解决这个问题
pipeline {
agent {
dockerfile true
}
stages {
stage("Config vars for staging") {
when {
branch 'staging'
}
environment {
MY_CREDENTIALS = 'creds-staging'
MY_ENVIRONMENT = 'production'
}
steps {
sh "echo MY_ENVIRONMENT: $MY_ENVIRONMENT"
}
}
stage("Config vars for production") {
when {
branch 'master'
}
environment {
MY_CREDENTIALS = 'creds-prod'
MY_ENVIRONMENT = 'staging'
}
steps {
sh "echo MY_ENVIRONMENT: $MY_ENVIRONMENT"
}
}
stage("Publish") {
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: env.MY_CREDENTIALS, accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
sh 'make takecareofit'
}
}
}
}
}
使用switch语句。在下面的示例中,如果分支不是
master
或staging
,则强制失败:
pipeline {
agent {
dockerfile true
}
stages {
stage("Config vars") {
steps {
script {
switch(branch) {
case 'staging':
MY_CREDENTIALS = 'creds-staging'
MY_ENVIRONMENT = 'production'
break
case "master":
MY_CREDENTIALS = 'creds-prod'
MY_ENVIRONMENT = 'staging'
break
default:
println("Branch value error: " + branch)
currentBuild.getRawBuild().getExecutor().interrupt(Result.FAILURE)
}
}
}
}
stage("Publish") {
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: env.MY_CREDENTIALS, accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
sh 'make takecareofit'
}
}
}
}
}
也许使用脚本块和if-else语句?谢谢,这就是我最后要做的(除了在同一阶段)