Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jenkins 在进入阶段之前设置变量的Declrative管道脚本_Jenkins_Groovy_Jenkins Pipeline_Jenkins Declarative Pipeline - Fatal编程技术网

Jenkins 在进入阶段之前设置变量的Declrative管道脚本

Jenkins 在进入阶段之前设置变量的Declrative管道脚本,jenkins,groovy,jenkins-pipeline,jenkins-declarative-pipeline,Jenkins,Groovy,Jenkins Pipeline,Jenkins Declarative Pipeline,我有一个关于声明性管道脚本的问题,我试图根据阶段开始前传递的参数动态设置变量,比如在环境块或节点块中 仅使用一个参数,我想在此时使用if条件动态构造其他变量,在两个块(环境、节点)上都进行了尝试,但没有成功,因为这需要全局,我需要在进入阶段之前初始化它 pipeline { environment { stream_name = "${stream_name}" user_id = "${user_id}" currentBuild_displayName =

我有一个关于声明性管道脚本的问题,我试图根据阶段开始前传递的参数动态设置变量,比如在环境块或节点块中

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

  pipeline {
    environment {
    stream_name = "${stream_name}"
    user_id = "${user_id}"
    currentBuild_displayName = "${currentBuild_displayName}"
    GBE_ViewTag = "${DevWorkspace_name}"
    script {
        if ( ${Stream_name} == 'Allura_Main_Infra_PreInt') {
        loadrule = "Infra-uInfra/Infra.loadrule"
        } 
    }
}
agent   {
    node {
        label 'CP'
        customWorkspace 'D:\\RTC'

    }
  }

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

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"
              }
            }
    }
}

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

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 {
  .....
 }