在Jenkinsfile中定义分支特定的环境变量。

huangapple go评论59阅读模式
英文:

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不是masterstaging,则强制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'
}
}
}
}
}

huangapple
  • 本文由 发表于 2020年1月3日 23:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定