相关疑难解决方法(0)

如何在Jenkins Groovy中的多行shell脚本中设置变量?

假设我在Jenkins中有一个包含多行shell脚本的Groovy脚本.如何在该脚本中设置和使用变量?正常方式会产生错误:

sh """
    foo='bar'
    echo $foo
"""
Run Code Online (Sandbox Code Playgroud)

抓住:groovy.lang.MissingPropertyException:没有这样的属性:foo for class:groovy.lang.Binding

groovy jenkins jenkins-workflow

47
推荐指数
2
解决办法
5万
查看次数

从Jenkins管道中的shell步骤中访问Groovy变量

使用Jenkins 2.x中Pipeline插件,如何在sh步骤中访问在阶段或节点级某处定义的Groovy变量?

简单的例子:

node {
    stage('Test Stage') {
        some_var = 'Hello World' // this is Groovy
        echo some_var // printing via Groovy works
        sh 'echo $some_var' // printing in shell does not work
    }
}
Run Code Online (Sandbox Code Playgroud)

在Jenkins输出页面上给出以下内容:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test Stage)
[Pipeline] echo
Hello World
[Pipeline] sh
[test] Running shell script
+ echo

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)

可以看出,echo …

jenkins jenkins-pipeline

28
推荐指数
3
解决办法
5万
查看次数