一尘不染

jenkins管道:shell脚本无法获取更新的环境变量

jenkins

在Jenkins中,我希望获得用户输入并将其传递给Shell脚本以供进一步使用。
我尝试将其设置为环境变量,但是shell脚本无法获取最新值,而旧值是echo。

pipeline {
    agent none
    environment{
        myVar='something_default'
    }

    stages {
        stage('First stage') {
            agent none
            steps{
                echo "update myVar by user input"
                script {
                    test = input message: 'Hello',
                    ok: 'Proceed?',
                    parameters: [
                        string(name: 'input', defaultValue: 'update it!', description: '')
                    ]
                    myVar = "${test['input']}"

                }
                echo "${myVar}"  // value is updated
            }
        }
        stage('stage 2') {
            agent any
            steps{
                echo "${myVar}" // try to see can myVar pass between stage and it output expected value
                sh("./someShell.sh") // the script just contain a echo e.g. echo "myVar is ${myVar}"
                                     // it echo the old value. i.e.something_default

            }
        }
    }
}

阅读 522

收藏
2020-07-25

共1个答案

一尘不染

您需要在阶段之间传递变量作为环境变量,例如:

stage("As user for input") {
    steps {
        env.DO_SOMETING = input (...)
        env.MY_VAR = ...
    }
}
stage("Do something") {
    when { environment name: 'DO_SOMETING', value: 'yes' }
    steps {
        echo "DO_SOMETING has the value ${env.DO_SOMETHING}"
        echo "MY_VAR has the value ${env.MY_VAR}"
    }
}
2020-07-25