15

I want to force following scenario in Jenkins:

  1. I have job A and job 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.

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()

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: 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.

Bakir Jusufbegovic
  • 2,806
  • 4
  • 32
  • 48
  • 21
    Have you imported `import jenkins.model.Jenkins` at the top of your script? – tim_yates Jul 02 '13 at 15:20
  • 1
    It worked with combination of using Execute system Groovy script option. Not sure why it isn't working with Execute Groovy script option where this import is not recognized, but the previous option works for me. Appreciated. Thanks – Bakir Jusufbegovic Jul 03 '13 at 07:54
  • System groovy commands run on the Jenkins master JVM, non-system groovy commands run on the node/agent in a forked JVM. Non-system groovy commands don't have access to Jenkins or any internal objects. That is why this won't work unless its a system groovy command – Brandon Aug 02 '19 at 17:10

4 Answers4

40

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

import jenkins.model.Jenkins

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
agentgonzo
  • 3,473
  • 3
  • 25
  • 30
6

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()
Max Spring
  • 1,111
  • 2
  • 11
  • 18
3

I think if you run the System Groovy Script, Jenkins objects are available by default. What you are running is the groovy script. Hence, need to import the packages/classes.

Vijay
  • 384
  • 4
  • 16
0

For me the above given solutions were not working, but I tried in the following way and it works fine :)

import jenkins.*
import jenkins.model.*
Jenkins.instance.getItemByFullName('folder/Sub_Folder/jobName').setDisabled(false)
Ashwaq
  • 431
  • 7
  • 17