jenkins管道:shell脚本无法获取更新的环境变量 [英] jenkins pipelines: shell script cannot get the updated environment variable

查看:572
本文介绍了jenkins管道:shell脚本无法获取更新的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Jenkins, I want to get a user input and pass to a shell script for further use.
I tried to set as environment variable, but the shell script failed to get the latest value and the old value is 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

            }
        }
    }
}

推荐答案

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

You need to pass the variables between your stages as environment variables, e.g. like this:

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

这篇关于jenkins管道:shell脚本无法获取更新的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆