Groovy code to access full path to a job, ie "Org/Project/branch" name

22 views
Skip to first unread message

Greg Smith

unread,
Aug 10, 2016, 1:28:39 PM8/10/16
to Jenkins Users

Hi,

I am using a groovy script to walk through all jobs, and find the multibranch jobs in a github project.  I want to get the full path name to the job, and I'm having problems figuring out what property to pull, even though I can see that the object knows its full path.

Here's what I mean -- Here's a sample of the code:

for (item in Jenkins.instance.items) {
  println("Item " + item)
  if(item.hasProperty("items")) {
    for(item1 in item.items) {
      println("Item " + item1)
      if(item1.hasProperty("items")) {
        for(item2 in item1.items) {
          println("Item " + item2)
        }
      }
    }
  }
}


If I run this, I can see that the jobs found known their full path.  This outputs this:

Item jenkins.branch.OrganizationFolder@41f1ea07[MyOrg]
Item org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject@6d24ed54[MyOrg/MyProject]
Item org.jenkinsci.plugins.workflow.job.WorkflowJob@5e6f7f42[MyOrg/MyProject/master]

I want to get "MyOrg/MyProject/master" -- obviously the WorkflowJob knows that's what it is.

But if I just look at item2.name, its just "master". No property value seems to be the full path, which is what I need. I can calculate it pretty easy with getParent() calls, but it just feels like its already there, and I'm just not pulling the correct value...

Thanks for the help,
Greg

Cuong Tran

unread,
Aug 11, 2016, 11:52:35 AM8/11/16
to Jenkins Users
Try getFullName()

Greg Smith

unread,
Aug 11, 2016, 1:29:29 PM8/11/16
to Jenkins Users
Thanks!  That worked for exactly what I wanted. 

On Thursday, August 11, 2016 at 11:52:35 AM UTC-4, Cuong Tran wrote:
Try getFullName()
Reply all
Reply to author
Forward
0 new messages