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

| improve this question | | | | |
  • Hi, if the answer satisfies your question, consider marking as accepted (tick mark) or ask if you need more detail. – Ian W Mar 9 at 7:03
  • Understood, but to complete the idea, your snippet code where should be used? A figure about that would improve your answer. – Manuel Jordan Mar 9 at 11:19
  • @IanW remember that the Jenkins instance has many folders, so consider to improve your answer: it about the code to isolate/select a specific folder and then list the Jobs from that Folder. It because exists a high likely or risk to have many Folders with Jobs with the same name – Manuel Jordan Mar 9 at 11:41
  • Answer expanded upon. – Ian W Mar 10 at 21:02
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
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.