Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I tried to get the git commit message in jenkinsfile and prevent the build based on commit message.

env.GIT_COMMIT doesn't return the commit details in jenkinsfile.

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

like image 631
Yahwe Raj Avatar asked Dec 14 '16 10:12

Yahwe Raj


People also ask

What happens if commit without message?

If an empty message is specified with the option -m of git commit then the editor is started. That's unexpected and unnecessary. Instead of using the length of the message string for checking if the user specified one, directly remember if the option -m was given.

How do I change commit message after commitment?

On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter. In your text editor, edit the commit message, and save the commit.


1 Answers

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.groovy  def 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

like image 126
csalazar Avatar answered Oct 05 '22 01:10

csalazar