I want to force following scenario in Jenkins:
To enable/disable job A, I've used Groovy plugin: Groovy plugin
Groovy 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()
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)
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)
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.
Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name.
Groovy is a very powerful language which offers the ability to do practically anything Java can do including: Create sub-processes and execute arbitrary commands on the Jenkins controller and agents. It can even read files in which the Jenkins controller has access to on the host (like /etc/passwd )
For future readers, Tim's comment above is the solution:
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". After switching to build step "Execute system Groovy script", this actually worked as intended:
import jenkins.model.Jenkins
Jenkins.instance.getItem("job-name").disable()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With