我有一个groovy脚本,它在脚本控制台中运行,但在我称之为groovy脚本(它来自我的共享库)时不在我的管道中:
#!/usr/bin/env groovy
import jenkins.model.*
import hudson.model.*
import jenkins.*
import hudson.*
import java.lang.*
def call(String jobname, String build_string) {
echo "Hello, ${jobname}."
echo "Hello, ${build_string}."
//Jenkins.instance.getAllItems(AbstractItem.class).each { println(it.fullName) };
def job = Jenkins.instance.items.find { it.name == 'test' }
echo "Hello, ${job}."
for (build in job.builds) {
def log = build.log
if (log.contains('hey')) {
println "${job.name}: ${build.id}"
}
}
}
我使用一些硬编码变量来确定,我有一个名为' test'它包含输出中的('嘿')。然后打印jobname和buildid,这在脚本控制台中运行良好。
但是当我在管道中调用它时:filterOutput env.JOB_NAME , 'a'
我得到了:
[test] Running shell script
+ echo hey
hey
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (getOutput)
[Pipeline] echo
Hello, test.
[Pipeline] echo
Hello, a.
[Pipeline] echo
Hello, false.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: builds for class: java.lang.Boolean
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:33)
我做错了什么?为什么这在我的控制台中工作而不在我的构建中?
答案 0 :(得分:0)
正如您在印刷品中看到的那样
.....
[Pipeline] echo
Hello, false.
....
然后你尝试做
for (build in job.builds) {
def log = build.log
if (log.contains('hey')) {
println "${job.name}: ${build.id}"
}
}
您的job = false
,即您尝试false.builds
,但不存在。
之前证明job
,你应该没问题。