我有一个
Jenkinsfile
,其中包含一些全局变量和一些阶段。
我可以从阶段更新全局变量吗?
一个例子:
pipeline {
agent any
environment {
PASSWD = "${sh(returnStdout: true, script: 'python -u do_some_something.py')}"
ACC = "HI"
}
stage('stage1') {
when { expression { params.UPDATE_JOB == false } }
steps{
script {
def foo= sh( returnStdout: true, script: 'python -u do_something.py ')
env.ACC = foo
println foo
print("pw")
println env.PASSWD
}
}
}
}
是否可以使用 foo 中的值更新
ACC
变量,以便我可以在下一阶段使用 ACC
变量?
有一种方法可以覆盖
environment {}
块中定义的环境变量 - 您可以使用 withEnv()
块,它允许您覆盖现有的环境变量。它不会更改定义的环境的值,但会在 withEnv()
块内覆盖它。看看下面的例子:
pipeline {
agent any
stages {
stage("Test") {
environment {
FOO = "bar"
}
steps {
script {
withEnv(["FOO=newbar"]) {
echo "FOO = ${env.FOO}" // prints: FOO = newbar
}
}
}
}
}
}
或者,您可能想使用一个技巧。您可以通过两种方式引用
ACC
环境变量:
env.ACC
ACC
env.ACC
的值一旦在environment {}
块内设置就无法更改,但ACC
的行为方式如下:当变量ACC
未设置时,env.ACC
的值将被访问(如果存在课程)。但是当 ACC
变量在任何阶段被初始化时,ACC
都会在任何阶段引用这个新设置的值。考虑以下示例:
pipeline {
agent any
environment {
FOO = "initial FOO env value"
}
stages {
stage("Stage 1") {
steps {
script {
echo "FOO is '${FOO}'" // prints: FOO is 'initial FOO env value'
env.BAR = "bar"
}
}
}
stage("Stage 2") {
steps {
echo "env.BAR is '${BAR}'" // prints: env.BAR is 'bar'
echo "FOO is '${FOO}'" // prints: FOO is 'initial FOO env value'
echo "env.FOO is '${env.FOO}'" // prints: env.FOO is 'initial FOO env value'
script {
FOO = "test2"
env.BAR = "bar2"
}
}
}
stage("Stage 3") {
steps {
echo "FOO is '${FOO}'" // prints: FOO is 'test2'
echo "env.FOO is '${env.FOO}'" // prints: env.FOO is 'initial FOO env value'
echo "env.BAR is '${BAR}'" // prints: env.BAR is 'bar2'
script {
FOO = "test3"
}
echo "FOO is '${FOO}'" // prints: FOO is 'test3'
}
}
}
}
正如您在上面的示例中看到的,该规则的唯一例外是环境变量在
environment {}
块之外初始化。例如,本例中的 env.BAR
在 Stage 1
中初始化,但 env.BAR
的值可以在 Stage 2
中更改,并且 Stage 3
会看到更改后的值。
我也鼓励您观看我的视频“Jenkins 管道环境变量解释”。
每个阶段都可以读取、写入或附加到脚本,以帮助在阶段之间传递环境变量。
pipeline {
agent any
stages {
stage('Set A and B') {
steps {
sh '''#!/bin/bash -xe
echo "" > jobinfo.sh
A=3
echo "A=${A}" >> jobinfo.sh
B=4
echo "B=${B}" >> jobinfo.sh
'''
}
}
stage('Set C') {
steps {
sh '''#!/bin/bash -xe
. jobinfo.sh
((C=A+B))
echo "C=${C}" >> jobinfo.sh
'''
}
}
stage('Result') {
steps {
sh '''#!/bin/bash -xe
. jobinfo.sh
echo "Result is ${C}" # 7
'''
}
}
}
}
loggedInUser = ""
node ("cm-windows") {
withEnv(["UserLoggedIn='Vrishali'"]) {
echo env.UserLoggedIn
stage('Setup'){
def buildCause = currentBuild.getBuildCauses()[0]
def buildPrincipal = [type:"unknown", name:""]
def buildUserCause =
currentBuild.getRawBuild().getCause(hudson.model.Cause.UserIdCause)
buildPrincipal = [type:"user", name:buildCause.userId]
print "Prining.."
print buildCause.userId
print "Checking the env var"
print env.UserLoggedIn
echo "[*] Starting build (id: ${env.UserLoggedIn}) on ${env.UserLoggedIn}"
loggedInUser = buildCause.userId
echo "Loggedin user"
print loggedInUser
echo loggedInUser
}
}
stage ('override env'){
withEnv(["UserLoggedIn=${loggedInUser}"]) {
echo "inside 2 stgae"
echo loggedInUser
print loggedInUser
echo "env"
echo env.UserLoggedIn
}
}
}