在进入阶段之前就设置变量的Dedecrative Pipeline脚本

时间:2019-01-17 10:31:51

标签: jenkins groovy jenkins-pipeline jenkins-declarative-pipeline

我对声明性管道脚本有疑问,我试图动态设置变量取决于阶段开始之前传递的参数,例如在环境块或节点块中

仅使用一个参数,我想在此时通过if条件动态构造其他变量,在两个块(环境,节点)上尝试但没有运气,因为这需要全局,因此我需要在进入阶段之前进行初始化

let button: UIButton = {
  let button = UIButton(type: .system)
  button.titleLabel?.font = UIFont.systemFont(ofSize: 20)
  ...
  return button
}()

2 个答案:

答案 0 :(得分:1)

您好,您可以在管道级别或每个阶段级别使用environment {}块。在环境块中,您可以设置变量检查以下示例:

pipeline {
    agent {label 'master'}
    environment{
        env1 = 'value0' // these are environment variables for all stages
    }
    stages{
        stage('stage 1') {
            environment{
                    env1 = 'value1' // these are environment variables for 'stage 1'
                    }
            steps{

                echo "$env1"
              }
            }
        stage('stage 2') {
            environment{
                    env1 = 'value2' // these are environment variables for 'stage 2'
                    }
            steps{

                echo "$env1"
              }
            }
        stage('stage 3') {
            steps{
                echo "$env1"
              }
            }
    }
}

答案 1 :(得分:0)

如果我将所有逻辑条件移出管道并且变量在所有阶段都是全局可用的,那么它也起作用

def user_id = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()
def full_name = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserName()
DevWorkspace_name = "${Developer_workspace}"
if ( DevWorkspace_name ==~ /(?s).*Allura_Main_Infra_PreInt.*/) {
        loadrule = "Infra-uInfra/Infra.loadrule"
        subsystem = "Infra"
        stream_name = "Allura_Main_Infra_PreInt"  
 } 
 pipeline {
  .....
 }