Jenkins管道保留变量的旧值

时间:2018-09-23 20:03:35

标签: jenkins groovy jenkins-pipeline

你好,我在Jenkinsfile中有以下代码用于声明性管道,但这具有奇怪的行为,例如:

  1. 如果在第一次运行此作业时,变量“ VERDICT”具有某个值,例如:FAIL(可以),则此变量可以具有3种可能的值,即:FAIL PASS,NOT_RUN

  2. 如果第二次运行此作业,则变量“ VERDICT”保持先前的值

    stage('run some code'){
        steps{
            script{
                VERDICT = sh (
                    script: '''#!/bin/bash
                    status=$(python ${SCRIPT})
                    echo ${status}
                    ''', returnStdout: true
                ).trim()
                echo "The VERDICT is: ${VERDICT}"
                if (VERDICT == 'FAIL') {
                    currentBuild.result = 'FAILURE'
                }
                if (VERDICT == 'PASS') {
                    currentBuild.result = 'SUCCESS'
                }
                if (VERDICT == 'NOT_RUN') {
                    currentBuild.result = 'SUCCESS'
                }
            }
        }
     }
    

欢迎在这里提供任何帮助:)

1 个答案:

答案 0 :(得分:0)

使用'env'解决此问题:

tage('run some code'){
steps{
    script{
        env.VERDICT = sh (
            script: '''#!/bin/bash
            status=$(python ${SCRIPT})
            echo ${status}
            ''', returnStdout: true
        ).trim()
        echo "The VERDICT is: ${env.VERDICT}"
        if (env.VERDICT == 'FAIL') {
            currentBuild.result = 'FAILURE'
        }
        if (env.VERDICT == 'PASS') {
            currentBuild.result = 'SUCCESS'
        }
        if (env.VERDICT == 'NOT_RUN') {
            currentBuild.result = 'SUCCESS'
        }
    }
}

}