简体   繁体   English

使用Groovy脚本插件从Jenkins运行Groovy命令

[英]Running Groovy command from Jenkins using Groovy script plugin

I want to force following scenario in Jenkins: 我想在Jenkins中强制执行以下场景:

  1. I have job A and job B 我有工作A和工作B.
  2. I want to disable job A when job B is executed, and after execution of job B, I want to enable job A again. 我想在执行作业B时禁用作业A,并且在执行作业B之后,我想再次启用作业A.

To enable/disable job A, I've used Groovy plugin: Groovy plugin 要启用/禁用作业A,我使用了Groovy插件: Groovy插件

Groovy plugin offers two possibilities: Execute Groovy script and Execute system Groovy script . Groovy插件提供了两种可能性: 执行Groovy脚本执行系统Groovy脚本

I've added following code snippets on the start of execution of job B: 我在作业B的执行开始时添加了以下代码片段:

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

and after execution of job B: 并在执行工作B后:

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

Using Execute Groovy script : 使用Execute Groovy脚本

When I ran job B, it fails with following exception: 当我运行作业B时,它失败并出现以下异常:

Caught: groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson7198966217090520732
    at hudson7198966217090520732.run(hudson7198966217090520732.groovy:1)

Using Execute system Groovy script : 使用Execute系统Groovy脚本

When I ran job B, it fails with following exception: 当我运行作业B时,它失败并出现以下异常:

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)

Any idea what might be the problem? 知道可能是什么问题吗? Also, when running this code snippets from Jenkins Script Console, it works without any issues. 此外,从Jenkins脚本控制台运行此代码片段时,它可以正常工作。 Thanks in advance. 提前致谢。

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

import jenkins.model.Jenkins

I ran into the same problem "unable to resolve class jenkins.model.Jenkins" as commented by @iloveretards, until I realized I had tried to use the build step "Execute Groovy script". 我遇到了同样的问题“无法解析类jenkins.model.Jenkins”,如@iloveretards评论,直到我意识到我曾尝试使用构建步骤“执行Groovy脚本”。 After switching to build step "Execute system Groovy script", this actually worked as intended: 切换到构建步骤“执行系统Groovy脚本”后,这实际上按预期工作:

import jenkins.model.Jenkins
Jenkins.instance.getItem("job-name").disable()

I think if you run the System Groovy Script, Jenkins objects are available by default. 我想如果你运行System Groovy Script,Jenkins对象默认是可用的。 What you are running is the groovy script. 你正在运行的是groovy脚本。 Hence, need to import the packages/classes. 因此,需要导入包/类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM