fduff
fduff

Reputation: 3811

Jenkinsfile - a way to skip the whole pipeline?

I use the declarative syntax for developing the (multibranch) pipeline script and I'm looking for a way to skip the whole pipeline based on some condition, without having to modify the when on every single stage.

Current use case: I'm setting up a cron to trigger builds at night, but I only want let's say the branches release/v1 and develop to go through the pipeline at night, not the dozen of other branches.

triggers {
  cron('H 21 * * 1-5')
}

// SKIP PIPELINE if triggered by timer AND branch not 'release/v1' OR 'develop'

stages {
  stage('build') {
    when { ... }
  }
  stage('UT') {
    when { ... }
  }
etc...
}

any hints would be appreciated.

Upvotes: 7

Views: 5544

Answers (2)

Moshisho
Moshisho

Reputation: 2981

You can use the SCM Skip plugin. Then use it in the pipeline like so:

scmSkip(deleteBuild: true, skipPattern:'.*#skip-ci.*')

This also enables to delete the build and also use a regex expression easily. The problem is, it aborts the next builds, and then ignores them for the relevant PR if you're connected with GitHub organization.

Upvotes: 2

SilverNak
SilverNak

Reputation: 3381

You can nest stages, if you have the pipeline-definition-plugin 1.3 or later (changelog).

Using that, you can nest your whole job in a parent stage, and use a when directive on the parent stage. All child stages will be skipped if the parent stage is skipped. Here is an example:

pipeline {
    agent any
    stages {
        stage('Parent') {
            when {
                //...
            }
            stages {
                stage('build') {
                    steps {
                        //..
                    }
                }
                stage('UT') {
                    steps {
                        //...
                    }
                }
            }
        }
    }
}

Upvotes: 12

Related Questions