使用 Groovy 脚本插件从 Jenkins 运行 Groovy 命令 [英] Running Groovy command from Jenkins using Groovy script plugin

查看:161
本文介绍了使用 Groovy 脚本插件从 Jenkins 运行 Groovy 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Jenkins 中强制以下场景:

I want to force following scenario in Jenkins:

  1. 我有工作 A 和工作 B
  2. 我想在执行作业 B 时禁用作业 A,并且在执行作业 B 后,我想再次启用作业 A.

为了启用/禁用作业 A,我使用了 Groovy 插件:Groovy 插件

To enable/disable job A, I've used Groovy plugin: Groovy plugin

Groovy 插件提供了两种可能性:执行 Groovy 脚本执行系统 ​​Groovy 脚本.

Groovy plugin offers two possibilities: Execute Groovy script and Execute system Groovy script.

我在作业 B 开始执行时添加了以下代码片段:

I've added following code snippets on the start of execution of job B:

Jenkins.instance.getItem("job_A").disable()

并在执行作业 B 后:

and after execution of job B:

Jenkins.instance.getItem("job_A").enable()

使用执行 Groovy 脚本:

当我运行作业 B 时,它失败并出现以下异常:

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)

使用执行系统 ​​Groovy 脚本:

当我运行作业 B 时,它失败并出现以下异常:

Using Execute system Groovy script:

When I ran job B, it fails with following exception:

FATAL: No such property: Jenkins for class: Script1
groovy.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)

知道可能是什么问题吗?此外,当从 Jenkins 脚本控制台运行此代码片段时,它可以正常工作.提前致谢.

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.

推荐答案

对于未来的读者,Tim 上面的评论是解决方案:

For future readers, Tim's comment above is the solution:

导入 jenkins.model.Jenkins

这篇关于使用 Groovy 脚本插件从 Jenkins 运行 Groovy 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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