2016-12-01 149 views
0

如何从sh context的sh上下文将变量导出到jenkins pipline作业的groovy上下文中?如何在jenkins管道中将变量从sh导出到groovy?

管道代码:

node { 
    echo 'Hello World' 
    sh 'export VERSION="v$(date)"' 
    echo env.VERSION 
} 

outupt:

[Pipeline] sh 
[test-pipeline] Running shell script 
++ date 
+ export 'VERSION=vThu Dec 1 12:14:40 CET 2016' 
+ VERSION='vThu Dec 1 12:14:40 CET 2016' 
[Pipeline] echo`enter code here` 
null 

我使用詹金斯版本。 2.34

更新: 有可能将变量写入临时文件并稍后阅读。这看起来完全像我的黑客。在使用并行构建时,它不是“线程安全的”,如果您需要在一次运行中导出多个变量,则不会扩展。有没有适当的方法来做到这一点?

+0

重复? http://stackoverflow.com/questions/40629924/change-groovy-variables-inside-shell-executor-in-jenkins-pipeline/40637578#40637578 – MaTePe

+0

谢谢,是的,这是类似的东西,我想! –

回答

0

我希望这会有所帮助。

node('master'){ 
    stage('stage1'){ 
    def commit = sh (returnStdout: true, script: '''echo hi 
    echo bye | grep -o "e" 
    date 
    echo lol''').split() 



    echo "${commit[-1]} " 

    } 
}