如何在Jenkins Pipeline中从sh导出变量到groovy?

3
如何将sh上下文中的变量导出到Jenkins Pipeline Job的Groovy上下文中?
管道代码:
node {
   echo 'Hello World'
   sh 'export VERSION="v$(date)"'
   echo env.VERSION
}

输出:

[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

我正在使用Jenkins ver. 2.34

更新: 有可能将变量写入临时文件并稍后读取。这对我来说完全像是一种hack。默认情况下,在使用并行构建时,它不具备“线程安全”性,并且在需要在一个运行中导出多个变量时无法扩展。有没有正确的方法来做到这一点?


重复的问题? http://stackoverflow.com/questions/40629924/change-groovy-variables-inside-shell-executor-in-jenkins-pipeline/40637578#40637578 - MaTePe
谢谢,是的,这正是我想要的东西! - HWM-Rocker
1个回答

4

我希望这可以帮助到您。

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



    echo "${commit[-1]} "

    }
}

好的回答。但是我讨厌那个语法。在(script:)中传递脚本字符串是必需的吗?还是可以只使用sh """<script>""" - Yarek T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接