如何在Jenkins管道中将变量从文件加载到windows环境变量中?

时间:2017-01-25 15:15:31

标签: windows jenkins jenkins-pipeline

我有以下Jenkinsfile

node  {
    stage 'checkout'
    deleteDir()
    checkout scm

    load 'LoadTheseVariables.txt'  

    echo "MYKEY: ${MYKEY}"
    echo "REPO: ${REPO}"

    bat 'echo MYKEY is %MYKEY%'
    bat 'echo REPO is %REPO%'        
}

文件LoadTheseVariables.txt包含:

MYKEY="ThisIsTheKey"
REPO="ThisIsTheRepo"

Jenkins构建的输出是:

<..snip..>
[Pipeline] load
[Pipeline] { (LoadTheseVariables.txt)
[Pipeline] }
[Pipeline] // load
[Pipeline] echo
MYKEY: ThisIsTheKey
[Pipeline] echo
REPO: ThisIsTheRepo
[Pipeline] bat
[test] Running batch script

D:\Jenkins\workspace\test>echo MYKEY is  
MYKEY is 
[Pipeline] bat
[test] Running batch script

D:\Jenkins\workspace\test>echo REPO is  
REPO is 
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Jenkins管道可以看到变量,但为什么我的windows cmd环境看不到变量呢?我甚至在最后的bat命令之前尝试了这一步:bat 'refreshenv'但是没有用这些变量重新加载环境。

如何从Windows作业中的文件加载变量并在运行时将它们加载到环境中?

1 个答案:

答案 0 :(得分:1)

解决方案是在bat命令周围使用双引号而不是单引号。似乎工作正常。