【问题标题】:Is there way to set variable from a stage local variable to Jenkins global variable?有没有办法将变量从阶段局部变量设置为 Jenkins 全局变量?
【发布时间】:2020-06-04 16:02:02
【问题描述】:

有没有办法访问 jenkins 全局管道中的阶段局部变量,我正在尝试在 post always 块中使用来自 Example stagevar1 值。

// Declarative //
pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                def var1 = sh 'ssh yourname@yourmachine 'grep uploadRate= /root/yourscript' '
            }
        }
    }
    post { 
        always { 
            echo 'Reading a Var1 Value' + var1
        }
    }
}

错误:

Error when executing always post condition:
groovy.lang.MissingPropertyException: No such property: var1 for class: WorkflowScript

【问题讨论】:

    标签: jenkins groovy jenkins-pipeline jenkins-groovy jenkins-job-dsl


    【解决方案1】:

    您不能在后期操作中直接调用在构建步骤中分配的变量。 作为一种解决方案,您可以将“示例”阶段结果传递给文件,然后使用Environment Inject Plugin 您可以在后期操作中访问该值。

    安装插件后,在作业配置中设置文件名。 plugin setup

    pipeline {
        agent any
        stages {
            stage('Example') {
                steps{
                script {
                    sh 'date > output.txt'    
                }
              }
            }
        }
        post { 
            always { 
                script {
                curDate = readFile 'outFile.txt'
                echo "The current date is ${curDate}"    
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2023-01-05
      • 1970-01-01
      • 2017-09-01
      • 2011-09-09
      • 1970-01-01
      • 2019-07-24
      相关资源
      最近更新 更多