jenkins管道:shell脚本无法获取更新的环境变量
在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
}
}
}
}
回答:
您需要在阶段之间传递变量作为环境变量,例如:
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脚本无法获取更新的环境变量 的全部内容, 来源链接: utcz.com/qa/416751.html