parallel remote trigger builds in a pipeline

310 views
Skip to first unread message

Chris Overend

unread,
Mar 15, 2017, 10:28:10 AM3/15/17
to Jenkins Users
#!groovy
import hudson.model.*

stage('Testing') {
    parallel (
        ['windows_remote': {build job: 'remote_trigger_30', parameters: [string(name: 'branch_name', value: branch_name)]},
         'linux_remote': {build job: 'remote_trigger_60', parameters: [string(name: 'branch_name', value: branch_name)]}
        ]
    )
}

If I set "Block until the remote triggered projects finish their builds." True
'remote_trigger_30' - blocks until complete
'remote_trigger_60' - blocks until complete
 This is not parallel.
 
 If I set "Block until the remote triggered projects finish their builds." False
'remote_trigger_30' - starts remote build then returns complete
'remote_trigger_60' - starts remote build then returns complete
 Both remote builds are still running but Pipeline returns complete

Is there a way to not block but wait? 

Eric Pyle

unread,
Mar 15, 2017, 12:08:52 PM3/15/17
to jenkins...@googlegroups.com
I did something similar by defining a 'done' variable for each parallel project, initialized to false, and set to true when the project is complete. Then in the downstream steps I used the waitUntil step to block the downstream step from starting until the 'done' variable was true for the corresponding upstream project. Not the most elegant but it did give the desired flow of execution.

#!groovy
import hudson.model.*
done_win = false
done_lin = false

stage('Testing') {
    parallel (
        ['windows_remote': {
            build job: 'remote_trigger_30', parameters: [string(name: 'branch_name', value: branch_name)]
            done_win = true},
         'linux_remote': {
            build job: 'remote_trigger_60', parameters: [string(name: 'branch_name', value: branch_name)]
            done_lin = true}
        ]
    )
    waitUntil {
        done_win && done_lin
    }
}
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/d029d9be-d81e-4b4c-9210-396a4add6d46%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Eric Pyle
Siemens PLM Software
Lebanon, NH
+1 603-277-3060 (T)
+1 603-359-8670 (M)
eric...@siemens.com
http://www.siemens.com/plm

Chris Overend

unread,
Mar 15, 2017, 12:26:16 PM3/15/17
to Jenkins Users
I assume you have "Block until the remote triggered projects finish their builds." = False?
  • Does it not just set your 'done' variable to true as soon as the remote build starts, since it does not block or wait.
  • Also with this set to False it does not get the log of the remote build.

Eric Pyle

unread,
Mar 15, 2017, 5:06:43 PM3/15/17
to jenkins...@googlegroups.com
I played around with this a bit, with a couple of freestyle jobs that do nothing except sleep a while and then finish. I confirmed that, whether I set the 'wait' property or not, both freestyle jobs are more or less immediately triggered if they are in a parallel block. If wait is set to true (or not set), 'build' returns a RunWrapper object which can be queried for build result or other things. If you do getRawBuild() on the RunWrapper object you get a standard Run object, with access to all Run methods such as getLog().

If wait is set to false, then indeed the pipeline job rushes on to completion, and the waitUntil step is no help, at least the way it is used in my example. I looked back on my use of waitUntil in the past, and it was for a different purpose. So you do need to set wait: true, but it does not prevent the jobs from running in parallel. My test pipeline script is below.

Eric

node {
    wrap ([$class: 'TimestamperBuildWrapper']) {

        stage('Testing') {
            parallel (
                ['windows_remote': {
                    winx = build \
                        job: 'dev_compile_win',

                        parameters: [string(name: 'branch_name', value: branch_name)],
                        wait: true
                    echo 'Windows done'},
                 'linux_remote': {
                    linx = build \
                        job: 'dev_compile_lin',

                        parameters: [string(name: 'branch_name', value: branch_name)],
                        wait: true
                    echo 'Linux done'}
                ]
            )
            echo 'Both are done!'
            println "Linux result: ${linx.result}\n"
            println "Windows log:"
            winLog = winx.getRawBuild().getLog(20)
            logOut = ''
            for (i = 0; i < winLog.size(); i++) {
                logOut += winLog[i] + '\n'
            }
            println logOut
        }
    }
}
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages