Unable to get NodeName in pipelines for WorkflowJob

72 views
Skip to first unread message

Zeeshan Maqbool

unread,
Mar 6, 2017, 7:33:52 PM3/6/17
to Jenkins Users
Setup - Latest Jenkins with pipeline job set to run from MultiBranch Pipeline (bitbucket)
Library Function - Its replacing the block plugin where you can specify when this job should wait for other jobs running on a node

I havent seen issues with other functions but I am unable to get NodeName for jobs running through MultiBranch pipelines.

def call(pattern)
{
    def mJInstance = Jenkins.getActiveInstance()
    def mJenInstance = mExternalConst.getJenkinsInst()
    def mJenJobs = mJenInstance.getAllItems()
    def mJenJobsSize = mJenJobs.size()
    def mResult = [] as Set
    def mBoolAnswer = false
    def mJobPattern = pattern; 

    if (pattern?.trim())
    {
        println "\nTotal jobs running on Jenkins - [Size $mJenJobsSize ]\nList of Jobs with pattern [ $mJobPattern ] "
        if (mJenJobsSize > 0)
        {
            def running
            def build
            for (i = 0; i < mJenJobsSize; i++)
            //mJenJobs.each
            {
                def locJob = mJenJobs[i]

                if (locJob.getDisplayName() =~ mJobPattern)
                {
                    if (locJob.lastBuild != null)
                    {
                        if (!(locJob.getDisplayName() =~ env.JOB_BASE_NAME )) //Skip the job itself
                        {
                            running = locJob.getLastBuild().isBuilding()
                            if (running)
                            {
                                build = locJob.getLastBuild()
                                println "\t" + 'JOB: ' + "[NAME " + locJob.getName() + " - " + locJob.getClass() + "] " +
                                //"\n\t[InProgress " + build.isInProgress() + "] " +
                                "\n\t[Node " + locJob.getLastBuiltOn().getNodeName() + "] " +         //   ************ LINE WITH ISSUE ***********
                                "\n\t[Node " + build.getExecutor().getOwner().getName() + "] " +     //   ************ LINE WITH ISSUE ***********
                                "\n\t[Build# " + build.getNumber() + "] " +
                                "\n\t[EstTimeRemain " + build.getExecutor().getEstimatedRemainingTime() + "] " +
                                "\n\t[Progress " + build.getExecutor().getProgress() + "/100] "
                                mResult.add(locJob.getName())
                            }
                        }
                    }
                }
            }
        } else
            println "\tNo jobs on this Jenkins box"

        mResultSize = mResult.size()

        if (mResultSize > 0)
        {
            println "\nFound [ $mResultSize ] jobs matching [ $pattern ] on this Jenkins"
            mBoolAnswer = true
        } else
            println "\nNo jobs found matching [ $pattern ] on this Jenkins"
    } else
        println "Please provide a pattern to this function. e.g\n jobRunning('OdinPlugin.*-')"

    return mBoolAnswer
}

Here's the output I am getting :
---------------------

15:28:39 JOB: [NAME First-Test - class org.jenkinsci.plugins.workflow.job.WorkflowJob]

15:28:39 [Node ]

15:28:39 [Node ]

15:28:39 [Build# 191]

15:28:39 [EstTimeRemain N/A]

15:28:39 [Progress 99/100]

---------------------

You can see here the Node name is not getting printed, one reason I can see for Option2 [build.getExecutor().getOwner().getName()] was its class [hudson.model.Hudson$MasterComputer] which has empty nodeName.

Does anyone know how to get nodeName for a MultiBranchPipeline job, the same thing works for older style FreeFlowJobs

Zeeshan Maqbool

unread,
Mar 6, 2017, 7:40:09 PM3/6/17
to Jenkins Users
one more thing to add which is pretty important and where the issue seems to be, if I check class for [locJob.getLastBuiltOn()] it comes out to be [hudson.model.Hudson], whereas it should be Node according to this documentation http://javadoc.jenkins.io/plugin/workflow-job/org/jenkinsci/plugins/workflow/job/WorkflowJob.html#getLastBuiltOn--


Zeeshan Maqbool

unread,
Mar 6, 2017, 9:17:12 PM3/6/17
to Jenkins Users
Simplified code which is easier to read :

def mJenJobs = jenkins.model.Jenkins.getActiveInstance()..getAllItems()

def mJenJobsSize = mJenJobs.size()
def mResult = [] as Set

for (i = 0; i < mJenJobsSize; i++)
{
    def locJob = mJenJobs[i]
    def running = locJob.getLastBuild().isBuilding()
    if (running)                       
    {
        def build = locJob.getLastBuild()

        println "[Node " + locJob.getLastBuiltOn().getDisplayName()+ "] " // Option 1
        println "[Node " + build.getExecutor().getOwner().getName()+ "] " // Option 2

    }
}

On Monday, March 6, 2017 at 4:33:52 PM UTC-8, Zeeshan Maqbool wrote:

Zeeshan Maqbool

unread,
Mar 16, 2017, 6:07:57 PM3/16/17
to Jenkins Users
anyone got ideas ?
Reply all
Reply to author
Forward
0 new messages