在Jenkins声明式管道中设置环境变量

时间:2019-06-20 07:40:27

标签: jenkins-pipeline

下面是我在jenkinsfile中部署阶段的代码

stage('Deploy') {  
    node('slave1') {
    if ("${env.Build_testapp1}" == 'true') {
    script {
                    env.packageid = "Applications/testapp1/revesion1"
                    env.environmentId = "Environments/SysTest1/machine1"


                }
      xldDeploy serverCredentials: 'developer', environmentId: env.environmentId, packageId: env.packageid
    }

但是如何根据环境使它可变?

我正在寻找这样的东西

if ("${env.Build_EVN}" == 'dev'){
env.environmentId = "Environments/Dev/machine1"
}
if ("${env.Build_EVN}" == 'systest1'){
env.environmentId = "Environments/SysTest1/machine1"
}

然后在阶段(“部署”)中使用“ env.environmentId”

1 个答案:

答案 0 :(得分:0)

您可以在Deploy之前添加一个阶段来处理env.environmentId,以供后续阶段使用。

stage('Prepare env') {
  steps {
    script {
      if ("${env.Build_EVN}" == 'dev'){
        env.environmentId = "Environments/Dev/machine1"
      }
      if ("${env.Build_EVN}" == 'systest1'){
        env.environmentId = "Environments/SysTest1/machine1"
      }
    }
  }
}

stage('Deploy') {
  ...
}