How to get the git latest commit message and prevent the jenkins build if the commit message contains [ci skip]? How to get the git latest commit message and prevent the jenkins build if the commit message contains [ci skip]? jenkins jenkins

How to get the git latest commit message and prevent the jenkins build if the commit message contains [ci skip]?


I had the same issue. I'm using pipelines. I solved this issue by implementing a shared library.

The code of the library is this:

// vars/ciSkip.groovydef call(Map args) {    if (args.action == 'check') {        return check()    }    if (args.action == 'postProcess') {        return postProcess()    }    error 'ciSkip has been called without valid arguments'}def check() {    env.CI_SKIP = "false"    result = sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true)    if (result == 0) {        env.CI_SKIP = "true"        error "'[ci skip]' found in git commit message. Aborting."    }}def postProcess() {    if (env.CI_SKIP == "true") {        currentBuild.result = 'NOT_BUILT'    }}

Then, in my Jenkinsfile:

pipeline {  stages {    stage('prepare') { steps { ciSkip action: 'check' } }    // other stages here ...  }  post { always { ciSkip action: 'postProcess' } }}

As you can see, the build is marked as NOT_BUILT. You can change it to ABORTED if you prefer, but it cannot be set to SUCCESS because a build result can only get worse


The build will pass when [ci skip] is provided in the last git log, but will not run the actual build code (the replacement to the first echo statement)

node {  checkout scm  result = sh (script: "git log -1 | grep '\\[ci skip\\]'", returnStatus: true)   if (result != 0) {    echo "performing build..."  } else {    echo "not running..."  }}


I think you could easily do that in multi branch pipeline job configurationBranch Sources > Additional Behaviours > Polling ignores commits with certain messagesmulti branch pipeline job configuration