Programmatic access to job names in CloudBees folders

634 views
Skip to first unread message

Ioannis Moutsatsos

unread,
Dec 2, 2015, 7:29:37 AM12/2/15
to Jenkins Developers
I need some help accessing programmatically the job names inside a CloudBees folder (using CloudBees Folders Plugin).
I used to use code like this to get all the job names

// Access to the Hudson Singleton
hudsonInstance
= hudson.model.Hudson.instance


// Retrieve all Jobs
allItems
= hudsonInstance.items
choices
=[]


// Create choices list
allItems
.each { job ->
    choices
.add(job.name)
}
return choices


However, now all I get is the list of Cloudbees folders. I would like to get the job names inside these folders.
Thanks for any feedback.
Ioannis

Victor Martinez

unread,
Dec 2, 2015, 7:55:39 AM12/2/15
to Jenkins Developers
You can easily filter by type: http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html#getAllItems(java.lang.Class)

Folder class: https://github.com/jenkinsci/cloudbees-folder-plugin/blob/master/src/main/java/com/cloudbees/hudson/plugins/folder/Folder.java#L72


See the below script:

jenkins.model.Jenkins.instance?.getAllItems(com.cloudbees.hudson.plugins.folder.Folder).each { folder ->
  println "Folder - ${folder}"
  folder.getItems().each {
    println "\t job - ${it}"
  } 
}

It's worth if you don't use deprecated methods/classes

Cheers

Ioannis Moutsatsos

unread,
Dec 2, 2015, 1:15:24 PM12/2/15
to Jenkins Developers
Awesome! This is exactly what I was looking for! Much appreciated Victor!

Best regards
Ioannis

Jesse Glick

unread,
Dec 2, 2015, 4:36:38 PM12/2/15
to Jenkins Dev
On Wed, Dec 2, 2015 at 7:55 AM, Victor Martinez
<victormar...@gmail.com> wrote:
> jenkins.model.Jenkins.instance?.getAllItems(com.cloudbees.hudson.plugins.folder.Folder).each
> { folder ->
> println "Folder - ${folder}"
> folder.getItems().each {
> println "\t job - ${it}"
> }
> }

Or more simply, depending on your needs:

Jenkins.instance.allItems.each {println it}
Reply all
Reply to author
Forward
0 new messages