英文:
Define branch-specific environment variables in Jenkinsfile
问题
以下是翻译好的内容:
pipeline {
agent {
dockerfile true
}
stages {
stage("配置变量用于暂存") {
when {
branch 'staging'
}
environment {
MY_CREDENTIALS = 'creds-staging'
MY_ENVIRONMENT = 'production'
}
steps {
sh "echo MY_ENVIRONMENT: $MY_ENVIRONMENT"
}
}
stage("配置变量用于生产") {
when {
branch 'master'
}
environment {
MY_CREDENTIALS = 'creds-prod'
MY_ENVIRONMENT = 'staging'
}
steps {
sh "echo MY_ENVIRONMENT: $MY_ENVIRONMENT"
}
}
stage("发布") {
steps {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: env.MY_CREDENTIALS, accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
sh 'make takecareofit'
}
}
}
}
}
注意:我已经将原始代码中的HTML实体编码转换为了文本字符。
英文:
I'd like to pass environment variables by publish stage based on the branch that the multi-pipeline job is processing.
While this example below works, I don't like how I'm ending up with additional stages, one per branch. Because I'm using the withCredentials
plugin, I need to have the MY_CREDENTIALS
variable set before that block. Is there a more elegant approach to solve this?
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'
}
}
}
}
}
答案1
得分: 2
Use switch语句。在下面的示例中,如果branch不是master
或staging
,则强制FAILURE:
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'
}
}
}
}
}
英文:
Use switch statement. In example below I forced FAILURE if branch is not master
or 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'
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论