Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 975
fduff Avatar asked Mar 05 '23 04:03

fduff


2 Answers

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 {
                        //...
                    }
                }
            }
        }
    }
}
like image 96
SilverNak Avatar answered Mar 15 '23 20:03

SilverNak


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.

like image 34
Moshisho Avatar answered Mar 15 '23 21:03

Moshisho