本文介绍了使用Groovy脚本插件从Jenkins运行Groovy命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想在Jenkins强制执行以下方案: 我有工作A和工作B 我想在执行作业B时禁用作业A,而在执行作业B之后,我想再次启用作业A. 要启用/禁用作业A,我使用了Groovy插件: Groovy插件执行Groovy脚本和执行系统Groovy脚本。 > 我在作业B的执行开始时添加了以下代码片段: Jenkins.instance.getItem(job_A)。disable() B: Jenkins.instance.getItem(job_A)。enable() 使用执行Groovy脚本: B,它跟随excep失败 抓住:groovy.lang.MissingPropertyException:没有这样的属性:Jenkins类:hudson7198966217090520732 at hudson7198966217090520732。运行(hudson7198966217090520732.groovy:1) 使用执行系统Groovy脚本: 当我运行作业B时,它失败,出现以下异常: 致命错误:没有这样的属性:Jenkins类:Script1 groovy.lang.MissingPropertyException:没有这样的属性:Jenkins类:Script1 在org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter。 java:50) at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite。 java:231)在Script1.run(Script1.groovy:1)在groovy.lang.GroovyShell.evaluate(GroovyShell.java:682)在g roovy.lang.GroovyShell.evaluate(GroovyShell.java:666) at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:80) at hudson.tasks.BuildStepMonitor $ 1.perform(BuildStepMonitor .java:19) at hudson.model.AbstractBuild $ AbstractBuildExecution.perform(AbstractBuild.java:804)$ b $ h at hudson.model.Build $ BuildExecution.build(Build.java:199)在hudson.model.Build $ BuildExecution.doRun(Build.java:160) at hudson.model.AbstractBuild $ AbstractBuildExecution.run(AbstractBuild.java:586) at hudson.model.Run。执行(Run.java:1576)在hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)在hudson.model.ResourceController.execute(ResourceController.java:88)在hudson.model.Executor.run(Executor.java:241) 任何想法可能是问题? 另外,从Jenkins脚本控制台运行此代码片段时,它的工作原理没有任何问题。 提前致谢解决方案对于未来的读者,Tim的上述评论是解决方案: import jenkins.model.Jenkins I want to force following scenario in Jenkins:I have job A and job BI want to disable job A when job B is executed, and after execution of job B, I want to enable job A again.To enable/disable job A, I've used Groovy plugin: Groovy pluginGroovy plugin offers two possibilities: Execute Groovy script and Execute system Groovy script.I've added following code snippets on the start of execution of job B:Jenkins.instance.getItem("job_A").disable()and after execution of job B:Jenkins.instance.getItem("job_A").enable()Using Execute Groovy script:When I ran job B, it fails with following exception:Caught: groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson7198966217090520732 at hudson7198966217090520732.run(hudson7198966217090520732.groovy:1)Using Execute system Groovy script:When I ran job B, it fails with following exception:FATAL: No such property: Jenkins for class: Script1groovy.lang.MissingPropertyException: No such property: Jenkins for class: Script1 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50) at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231) at Script1.run(Script1.groovy:1) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:682) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:666) at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:80) at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:19) at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:804) at hudson.model.Build$BuildExecution.build(Build.java:199) at hudson.model.Build$BuildExecution.doRun(Build.java:160) at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:586) at hudson.model.Run.execute(Run.java:1576) at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46) at hudson.model.ResourceController.execute(ResourceController.java:88) at hudson.model.Executor.run(Executor.java:241)Any idea what might be the problem?Also, when running this code snippets from Jenkins Script Console, it works without any issues.Thanks in advance. 解决方案 For future readers, Tim's comment above is the solution:import jenkins.model.Jenkins 这篇关于使用Groovy脚本插件从Jenkins运行Groovy命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!