Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Jenkins to skip a downstream job if a new build is waiting in the pipeline?

Tags:

jenkins

My build pipeline in Jenkins is broken down into three jobs:

  1. Build code
  2. Deploy code to environment
  3. Run automated functional tests

I have set it up so that concurrent builds can occur and the build pipeline will stop a build from entering #2, if #2 or #3 is currently running for another build.

What I want to be able to do is set up Jenkins to handle when there is more than one build waiting, and #2 and #3 finish, for only the LATEST build to enter into #2 and #3.

Is there a way to do this out of the box? IF you have the book Continuous Delivery, what I'm trying to do is implement what's on p. 118 - p. 119

like image 612
BestPractices Avatar asked Oct 10 '12 18:10

BestPractices


People also ask

Can I fail a Jenkins pipeline and continue the execution?

You can even fail the build and continue the execution of the pipeline. Just make sure your Jenkins is up to date, since this feature is only available since "Pipeline: Basic Steps" 2.16 (May 14, 2019). Before that, catchError is still available but without parameters:

How to use triggers in Jenkins pipeline?

Triggers follow the same logic as the “build after other projects are built” option in the configuration of Jenkins projects. Most teams work with this model because it’s the default option of Jenkins. When we want to use triggers in the pipeline syntax, we simple define the upstream project that we want to monitor and specify our threshold.

What is freestyle project in Jenkins?

Freestyle Project: Freestyle Project in Jenkins is an improvised or unrestricted build job or task with multiple operations. Operations can be a build, pipeline run, or any script run. Maven Project: Maven project is selected for managing as well as building the projects which contain POM files.

How do I link jobs in Jenkins?

However, there are two ways to link Jobs: triggers and the build command. Both have their own strengths and weaknesses and if you only use one of these, you’re definitely missing out. Triggers follow the same logic as the “build after other projects are built” option in the configuration of Jenkins projects.


1 Answers

Should try one of those, under Advanced Project Options:

  • Block build when upstream project is building
    (should make sure it does not cause steps 2 and 3 to get stuck in queue)

  • Block build when downstream project is building
    (I know this one sounds like the opposite to your request,
    but the actual result is that you accumulate changes to a single build-cycle,
    preventing extra runs)

If this causes unwanted builds to pile-up,
please review the following links that should help you
empty the queue or kill running jobs:

  • Stopping Jenkins job in case newer one is started (by malenkiy_scot)

  • Purge Build Queue Plugin

  • Signal killer

  • Kill All the Builds (courtesy of Kohsuke Kawaguchi)

    import hudson.model.*;
    
    Hudson.instance.computers.each { c ->
      c.executors.each { e ->
         e.interrupt();
      } 
    }
    

Cheers

like image 149
Gonen Avatar answered Oct 27 '22 05:10

Gonen