Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline Execute a closure (like in "parallel" step)

I have a very long and complex pipeline that I'm rewriting after a major jenkins upgrade.

What I'd like to to is declaring my stages as variables,then execute them in the main node body: I can do this easily for the parallel stages, but I want to have the same style also for the sequential ones.

After a lot of tests, the only way I found to make this work was using "fake" parallel calls around all single sequential stages (ugly), I'm sure there is a better solution but seems like I can't find the proper step...shame on me.

Here's my example:

stage1 = { stage("one") {
            println "stage one"
         } }

stage2 = { stage("two") {
            println "stage two"
         } }

stage3 = { stage("three") {
            println "stage three"
         } }

node {
    parallel (
        "one" : stage1 ,
        "two" : stage2
    )
    HERE I WANT TO CALL stage3 Closure, possibly giving a map like in the parallel above
}
like image 721
Cristiano Di Giacomo Avatar asked Oct 02 '17 23:10

Cristiano Di Giacomo


People also ask

How does Jenkins get parallel execution?

This plugin adds a tool that lets you easily execute tests in parallel. This is achieved by having Jenkins look at the test execution time of the last run, split tests into multiple units of roughly equal size, then execute them in parallel.

Can Jenkins run parallel builds?

Jenkins parallel builds can be based on static information and decisions, in which case the declarative approach works well. But when there is a need for dynamic decisions, a new approach is required for a more advanced way of doing Jenkins parallel builds.

Can we run parallel jobs in Jenkins?

Parallel Job Execution in Jenkins In Jenkins, there are several ways to implement parallel job execution. One of the common approaches is the parent-child build model. In this model - a parent (upstream) job is triggering child (downstream) jobs.


1 Answers

You should be able to do this with the run method.

stage3.run()

I do not know if this is safe to use.

like image 145
Sunvic Avatar answered Sep 28 '22 06:09

Sunvic