Search code examples
jenkinscloudbeesjenkins-pipeline

Accessing Jenkins Job attributes from another job


I have a Jenkins groovy template script that is looping through a list of auxiliary models called services. Each auxiliary model has a job called reportableJob as an attribute. These "reportable jobs" all have an attribute called branch. How can I access this attribute?

Here's my general thinking:

for (i=0; i< services.size(); i++) {
    def job = services[i].reportableJob
    def branch = job.branch
    echo "${branch}"
}

This results in the following error:

groovy.lang.MissingPropertyException: No such property: branch for class: org.jenkinsci.plugins.workflow.job.WorkflowJob

Solution

  • Got it figured out, good to know:

    for (i=0; i< services.size(); i++) {
        def job = services[i].reportableJob
        def instance = com.cloudbees.hudson.plugins.modeling.impl.entity.EntityInstance.from(job)
        echo "Branch: ${instance.getValue("branch")}"
    }