如何通过groovy脚本获取jenkins中使用的lastbuild参数? [英] How do I get the lastbuild parameters used in jenkins through groovy script?

查看:599
本文介绍了如何通过groovy脚本获取jenkins中使用的lastbuild参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Groovy脚本获取Jenkins作业的最后一次构建中使用的构建参数。我厌倦了instance.getItem和instance.getItemMap,但两者都失败了



我尝试了什么:



我在groovy脚本下面尝试但是在构建时遇到错误。

 import jenkins.model.Jenkins 
def VAL1 = jenkins.model.Jenkins.instance.getItem('JOBNAME')。lastBuild.getBuildVariables()。get(PARAM)
def VAL2 = jenkins.model.Jenkins.instance.getItemMap()['JOBNAME']。lastBuild.getBuildVariables()。get(PARAM)









错误:构建步骤失败,异常
java.lang.NullPointerException:无法获取属性'lastBuild' on null object
at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:60)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:172)
at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)$在Script1.run的b $ b(Script1.groovy:2)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java: 623)
在groo vy.lang.GroovyShell.evaluate(GroovyShell.java:594)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:170)
at hudson.plugins。 groovy.SystemGroovy.run(SystemGroovy.java:95)
at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
at hudson.tasks.BuildStepMonitor $ 1.perform(BuildStepMonitor.java) :20)
at hudson.model.AbstractBuild $ AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.model.Build $ BuildExecution.build(Build.java:205)
at hudson.model.Build $ BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild $ AbstractBuildExecution.run(AbstractBuild.java:534)
at hudson.model.Run.execute( Run.java:1720)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson .model.Executor.run(Executor.java:410)
构建步骤'执行系统Groovy脚本'标记为b uild as failure

解决方案

1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild

AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.model.Build


BuildExecution.build(Build.java:205)
at hudson.model.Build


I need to get the build parameters used in the last build of Jenkins job through Groovy script. I tired both instance.getItem and instance.getItemMap, but both are failing

What I have tried:

I tried below groovy script but getting an error on build.

import jenkins.model.Jenkins
def VAL1 = jenkins.model.Jenkins.instance.getItem('JOBNAME').lastBuild.getBuildVariables().get("PARAM")
def VAL2 = jenkins.model.Jenkins.instance.getItemMap()['JOBNAME'].lastBuild.getBuildVariables().get("PARAM")





ERROR: Build step failed with exception
java.lang.NullPointerException: Cannot get property 'lastBuild' on null object
	at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:60)
	at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:172)
	at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:47)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
	at Script1.run(Script1.groovy:2)
	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:170)
	at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
	at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
	at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
	at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
	at hudson.model.Build$BuildExecution.build(Build.java:205)
	at hudson.model.Build$BuildExecution.doRun(Build.java:162)
	at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
	at hudson.model.Run.execute(Run.java:1720)
	at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
	at hudson.model.ResourceController.execute(ResourceController.java:98)
	at hudson.model.Executor.run(Executor.java:410)
Build step 'Execute system Groovy script' marked build as failure

解决方案

1.perform(BuildStepMonitor.java:20) at hudson.model.AbstractBuild


AbstractBuildExecution.perform(AbstractBuild.java:779) at hudson.model.Build


BuildExecution.build(Build.java:205) at hudson.model.Build


这篇关于如何通过groovy脚本获取jenkins中使用的lastbuild参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆