Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally enable Jenkins declarative pipeline options?

Does Jenkins provide any functionality to achieve the following pipeline described below?

pipeline
{
    agent any
    options
    {
        when {
            branch 'master'
        }
        disableConcurrentBuilds()
    }
    stages { ... }
}

I have two states for repositories which this single pipeline must manage:

  1. Build for commits to merge-requests branches (pre-merge), allow builds to be run concurrently
  2. Build the master branch on merge of merge-requests (post-merge), do not allow builds to be run concurrently.
like image 682
Sean Pianka Avatar asked Nov 26 '18 22:11

Sean Pianka


People also ask

Can we run a step conditionally in Jenkins?

The Conditional BuildStep plugin lets users add conditional logic to Freestyle jobs from within the Jenkins web UI. It does this by: Adding two types of Conditional BuildStep ("Single" and "Multiple") - these build steps contain one or more other build steps to be run when the configured condition is met.

What are the 3 types of pipelines in Jenkins?

Declarative versus Scripted Pipeline syntax Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which: provides richer syntactical features over Scripted Pipeline syntax, and.


1 Answers

You can use the Lockable Resources Plugin to guarantee that the problematic steps do not run in parallel when on the master branch.

Something like:

stage('on master') {
    when {
        branch 'master'
    }
    steps {
        lock(label: 'choose_a_label') {
            // your steps
        }
    }
}


stage('not on master') {
    when {
        not {
            branch 'master'
        }
    }
    steps {
        // your steps
    }
}
like image 133
Chadi Avatar answered Oct 15 '22 06:10

Chadi