Groovy to list Jenkins jobs in a defined subfolder but not levels below given folder

Viewed 29

I need to get a list of jobs from a given folder in Jenkins. I do not want the jobs in subfolders that I do not specify.

If I have a folder structure with jobs "some_folder/some_subfolder/another_subfolder", the example will return all jobs in "some_subfolder", but also the jobs names in "another_subfolder".

I'm sure that I need to do some regex here?

def folderName = "some_folder/some_subfolder"
def jobsList = []
Jenkins.instance.getAllItems(Job.class).each{
  if(it.fullName.contains(folderName)) {
    jobsList << it.name
  }
}
1 Answers

The below code should get you the Jobs in a specific directory.

def folderName = "some_folder/some_subfolder"
def jobsList = []
Jenkins.instance.getAllItems(Job.class).each{
  if(it.fullName == folderName + "/" + it.name) {
    jobsList << it.name
  }
}
Related