0

I want to schedule a job to clean old build so I have configure the below scripts but its working at Jenkins script console and not for Jenkins job configuration. While running the job I am getting below error:

Processing provided DSL script ERROR: (script, line 5) No such property: Jenkins for class: script Finished: FAILURE.my scripts is"

Code:

MAX_BUILDS = 2
for (job in Jenkins.instance.items) {
  println job.name

  def recent = job.builds.limit(MAX_BUILDS)

  for (build in job.builds) {
    if (!recent.contains(build)) {
      println "Preparing to delete: " + build
      build.delete()
    }
  }
}
daspilker
  • 8,154
  • 1
  • 35
  • 49

1 Answers1

0

Jenkins script console automatically imports the packages jenkins.*, jenkins.model.*, hudson.*, and hudson.model.*. But Job DSL does not automatically import these packages, you have to do that at the beginning of your script

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*

// your script

But it would be cleaner to just import the classes that you need, e.g. jenkins.model.Jenkins

import jenkins.model.Jenkins

// your script
daspilker
  • 8,154
  • 1
  • 35
  • 49
  • thanks daspilker can u pls help me on above script how to exclude multibranch project because now my scripts is fine but multi branch project throwing error – Ashish Palecha Jan 29 '17 at 04:57