From upstream pipeline, how to know on which node has been executed downstream pipeline in parallel job execution

19 views
Skip to first unread message

Vincent SCHMITT

unread,
Apr 23, 2018, 10:54:26 AM4/23/18
to Jenkins Users
Hello

I have an upstream scripted pipeline calling in parallel downstream script pipeline.
I would like after the run, that the upstream pipeline know on which slave they have been executed.

Would you know how can we do this?


Upstream pipeline ("pipeline 123"):
import hudson.Util;

node ("master") {
    cleanWs()
    def tc_job_name
    def tc=["tc002","tc003"]
    def branches=[:]  
       
    stage ("phase")   {
        tc_job_name = "subpipeline"   
       
        for (int i = 0; i <= tc.size()-1; i++) {   
            def index = i //if we tried to use i below, it would equal tc.size()-1 in each job execution.
            branches["${tc[index]}"] = {
                    def first_run,job_hudson,build_hudson,build_env
                    first_run = build job: tc_job_name, parameters: [string(name:'dummy', value: "${index}")], description:'${tc[index]}', propagate: false
                   
                    def rawbuild = first_run.getRawBuild()
                    def build_properties = rawbuild.getProperties()
                    def log = build_properties['log']
                   
                    def node = log.tokenize('\n').findAll {it.contains 'Running on'}.each {it}[0].split(" ")[2]
                    echo node
                   
               } // end of branches["${tc[index]}"] definition
        } // for (int i = 0; i <= tc.size()-1; i++)
        parallel branches
    } // stage phase
} // node ("master")


Downstream pipeline ("subpipeline"):
node('only_tc') {
    stage ("test") {
        echo env.NODE_NAME
    }
}

What has been tried:
* In upstream pipeline I have tried to get the owner of the executor launching the downstream pipeline:

tc_job_name ='subpipeline'

first_run=build job: job_name

def build_hudson = first_run.getRawBuild()

def build_properties = build_hudson.getProperties()

def executor = build_properties['executor']

println "Node: "+ executor.getOwner().getCaption()


But it doesn't work with parallelized job execution , it outputs "Master" which is not the jenkins slave executor


* Getting NODE_NAME environment parameter:

tc_job_name ='subpipeline'

first_run=build job: job_name

def build_hudson = first_run.getRawBuild()

 def build_env = build_hudson.getEnvironment()

           println build_env.get("NODE_NAME","no node name")


But the environment variables of a pipeline is not the same as a freestyle job and it has not any NODE_NAME


* Parsing the log environment variable: (see first sample), but it's not the right way to do I guess.


Thanks in advanced !!


Vincent

Reply all
Reply to author
Forward
0 new messages