我有下一个 Groovy 代码,我尝试在 Jenkins Pipeline 中运行它:
@Grab('io.github.http-builder-ng:http-builder-ng-core:1.0.3')
import static groovyx.net.http.HttpBuilder.configure
def astros = configure {
request.uri = 'http://api.open-notify.org/astros.json'
}.get()
println "There are ${astros.number} astronauts in space right now."
astros.people.each { p->
println " - ${p.name} (${p.craft})"
}
但每次我得到 java.lang.NullPointerException: Cannot invoke method get() on null object
错误。
当我从我的桌面运行它时,一切都按预期工作:
There are 6 astronauts in space right now.
在詹金斯:
There are null astronauts in space right now.
调试输出:
<groovyx.net.http.UriBuilder$Basic@4bc2413c scheme=http port=-1 host=api.open-notify.org path=/astros.json query=[:] fragment=null userInfo=null parent=groovyx.net.http.UriBuilder$ThreadSafe@69c6847a useRawValues=null>
我应该怎么做才能让它发挥作用?
原文由 user54 发布,翻译遵循 CC BY-SA 4.0 许可协议
我认为您创建了一个共享库并试图在管道中使用它?
我现在有同样的问题,我认为这可能是 Jenkins 上的 Groovy 解释器的限制,类似于 each loop didn’t work until some time ago 的情况。
我现在使用这个版本的 http-builder 来规避它。如果我找到合适的解决方案,我会更新这个(如果您发现任何问题,也请发布答案)。