1

I read these links:

First of all I work around Folders and these contains Views and these Jobs respectively.

Consider the following (to keep it simple):

  • Folder: jenkins-01 View: alpha Job: Execute
  • Folder: jenkins-02 View: betax Job: Execute

From above two important observations:

  • The jenkins-01 and jenkins-02 folders are for two different projects.
  • The Execute Job , can be repeated in many different Folders, but only about the name.

Now consider the following:

I can execute the Job through:

No custom View - therefore the default All default/generic view:

  • http://localhost:9090/job/jenkins-01/job/Execute/

Through many Views, let's say

  • http://localhost:9090/job/jenkins-01/view/alpha/job/Execute/
  • http://localhost:9090/job/jenkins-01/view/betax/job/Execute/

And all the history or track of builds are in this path location:

  • /Users/username/ci/jenkins/Contents/jobs/jenkins-01/jobs/Execute

Script Console

Through the following:

def jobName = ???

println jobName;

//I used any of them
//def job = Jenkins.getInstance().getItemByFullName(jobName, Job.class)
//def job = Jenkins.instance.getItem(jobName)

if(job != null) {
     job.getBuilds().each { println it }  
   //job.getBuilds().each { it.delete() }  
   //job.nextBuildNumber = 1   
   //job.save()
}
else
    println 'job is NULL'

I tried many combinations about jobName (it about ???) and always I get null. So what should be the correct value for jobName?

Command Line

  • java -jar jenkins-cli.jar -s http://localhost:9090 delete-builds jobName=??? '1-56'

Again: what should be the correct value jobName?.

Remember: each Job is declared in a Folder and the Job's name (just the name) can be repeated in many Folders

Manuel Jordan
  • 417
  • 2
  • 8
  • 20

1 Answers1

2

There is only only authoritative path for a job - it is job.fullName; that is

http://localhost:9090/job/jenkins-01/job/Execute/

This will give you the fullName of all your jobs:

Jenkins.instance.allItems.findAll() 
    { it instanceof AbstractProject }.each 
    {println it.fullName}

Views are merely filtered lists of jobs to be displayed. You should see the same job history in every View.

NB: It is unfortunate Jenkins allows jobs with the same name as if you "move" a job, you could clobber another in another folder with the same name.

Update: The following code will select all "top-level" folders and iterate thru them, listing the contents of each. Conditions to (or not) recursively process are noted. There is only ever one handle to a job and that is it.fullName.

You can add additional conditions to limit the scope to specific folders or job name patterns.

import com.cloudbees.hudson.plugins.folder.*

Folders =  Jenkins.instance.allItems.findAll() 
    { it instanceof com.cloudbees.hudson.plugins.folder.Folder && !it.fullName.contains("/")  
    // remove the && condition to retrieve nested folders
}

Folders.collect { folder ->
  println "::: " + folder.fullName
    // use folder.getItems().findAll() below to not recurse into nested folders
    folder.allItems.findAll()
    { it instanceof AbstractProject }.each 
    { println folder.fullName + ' - ' + it.fullName }
}
return
Ian W
  • 353