Repeat build n times in Jenkins

2020-06-18 03:11发布

问题:

Is there any way to repeat a build N times? (doesn't matter the status of the last build). The build is parametrized and for the moment I am using Jenkins Parameterized Trigger plugin which is set to trigger the same build, but this is of course an infinite loop.

I would like to be able to specify how many times to repeat the build with the same parameters.

回答1:

It is possible to create a repeated loop (not infinite) of a Jenkins job, by adding a conditional step that evaluates a $JOB_COUNTER parameter, which simply being decreased on each iteration.

To do so, first create a new String parameter "JOB_COUNTER" with default value = 1.

Then use EnvInject plugin, and check "Prepare an environment for the run" + "Override Build Parameters", and add in "Evaluated Groovy script":

def map  = [:]
int newJobCounter = JOB_COUNTER.toInteger() - 1
println "Decreasing JOB_COUNTER from " + JOB_COUNTER + " to " + newJobCounter  
map.put("JOB_COUNTER", newJobCounter)
return map

Finally, with Conditional BuildStep plugin + Parameterized Trigger plugin (and optionally with PostBuildScript plugin, if you'd like to start next iteration only after build has completed), set the following:

UPDATE:

Another way to loop, is to decrease JOB_COUNTER in the predefined parameters (instead of inside EnvInject):

JOB_COUNTER=${JOB_COUNTER}-1

Then, to correctly update JOB_COUNTER on each iteration, use evaluate() method instead of toInteger(), in the EnvInject groovy:

int newJobCounter = evaluate(JOB_COUNTER)
println "Evaluating JOB_COUNTER: " + JOB_COUNTER + " => " + newJobCounter  
map.put("JOB_COUNTER", newJobCounter)

And finally, the Conditional action should be:

$JOB_COUNTER > Greater than 1


回答2:

Well, there is no easy solution for this problem, but it can be easily scripted. And there are couple of ways to do that:

  1. Using a Build Flow Plugin

    This plugin will let you script the way that the project build flow is being executed. You can script you builds to run in parallel, to retry the failed builds for as many times as you desire, and much more. I would give it a try and create a script with desired number of retries. I think it'll be your best option to try.

  2. You can try using Fail the Build Plugin and either Retry Failed Builds or Periodic Reincarnation Plugin

    It will enable you to break the build on purpose and then the other plugin will try to build it again and again. It will not be easy to control the number of retries here - you would have to change the status of the build based on a variable from script in your build process or something. But hey, maybe it is a solution as well.

  3. More complex solutions you can base on RabbitMQ - you can put more than one message in a queue to build the project numerous times and then build the project based on those messages.

  4. You can base your build on BPM: JBPM Plugin with JBPM Workflow Plugin or Jenkow

I can probably come up with more solutions, but the first one above is probably a good starting point...



标签: jenkins