Search code examples
gitjenkinsjenkins-pipelinegit-commitcommit-message

Skip jenkins stage if tag appears in the commit message


I'm trying to skip some stages in a Jenkins pipeline if something like #no_build or #no_unittest is in the commit message.

I wan't to skip the Unittest stage if #no_unittest is present in the commit message.

I saw that there is a solution using a script but is it possible to do it in a declarative way?

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'npm build'
            }
        }
        stage('Unittest') {
            when{
                branch 'master'
            }
            steps {
                sh 'python unittest.py'
            }
        }
    }
}

P.S. The tags should be case sensitive, also "-" and "_" should be treated the same.

I tried putting changelog: #no_build but that didn't produce what was expected. I'm not sure how to resolve the "-" and "_" being equal.


Solution

  • You can use the changeLog with the when condition. Example below.

    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    sh 'npm build'
                }
            }
            stage('Unittest') {
                when{
                    when { not { changelog '^.*#no_unittest.*$'} }
                }
                steps {
                    sh 'python unittest.py'
                }
            }
        }
    }