How to skip a build after a "git push" done by a Jenkinsfile

12,559 views
Skip to first unread message

Idan Adar

unread,
Mar 23, 2017, 9:10:51 AM3/23/17
to Jenkins Users
Hi all,

In a declarative pipeline (a Jenkinsfile in the repo), how can I stop a job from getting re/triggered due to a push event in GitHub?
For example:

stage ("Publish to npm") {
   steps
{
      sh
'''
         npm publish
         npm --no-git-tag-version version minor
      '''

     
      sh
'''
         git commit -am '
Bumped version number'
         git push
      '''

   
}
}

The "git push" will trigger the push webhook in GitHub that makes a call to Jenkins, which will in turn start the same Jenkinsfile and again bump the version, commit, push and again... a loop.
How can we not-trigger the job in this scenario?


Idan Adar

unread,
Mar 23, 2017, 9:43:41 AM3/23/17
to Jenkins Users
I should add that the job is of multibranch type.

Mark Waite

unread,
Mar 23, 2017, 9:59:07 AM3/23/17
to Jenkins Users
The git plugin as used in pipeline doesn't support ignoring commits from certain users, or ignoring commits with certain messages.  Until it does, you will probably need to "short circuit" and exit from the pipeline after using git commands to extract the latest commit message from the repository.  It's not a pretty solution, but it can work now.

Another alternative is to fork the git plugin and add the "ignore commits from specific user" support to the pipeline.

Mark Waite

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/af0d3836-ae98-4e09-93bf-9dfa4d65b39f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Idan Adar

unread,
Mar 23, 2017, 10:09:09 AM3/23/17
to Jenkins Users
Unfortunately I am not versed enough in programming to add this functionality... 

I think that the workaround will add lots of "red rows" in the job view, because it first has to checkout the repo, check the messages and then abort...

Idan Adar

unread,
Mar 23, 2017, 10:21:03 AM3/23/17
to Jenkins Users
Could it be that this is the solution? http://stackoverflow.com/questions/42798006/how-to-disable-automatic-build-from-scm-change-in-jenkinsfile
The flow in my case is:

PR to GHE > GHE sends pull_request webhook to Jenkins > Jenkins uses the Jenkinsfile to run unit tests > Jenkins makes a call to merge the PR to the master branch.
The merge to the master branch is essentially a push webhook, so: GHE sends a push webhook to Jenkins > Jenkins uses the same Jenkinsfile to run again unit tests > there is logic to not-merge in case the branch is the master branch > Instead publish to npm > bump the version number > commit & push to git.

This time because the the whole thing worked from a checkout of the master branch and not based on a PR, the option to "Suppress automatic SCM triggering" should hopefully stop the loop from starting...

Idan Adar

unread,
Mar 25, 2017, 2:28:18 AM3/25/17
to Jenkins Users
The above is not the solution. It simply makes the job not run...

Idan Adar

unread,
Mar 25, 2017, 4:33:36 AM3/25/17
to Jenkins Users
This answer is promising: http://stackoverflow.com/a/41164841/1530814

The problem is that when using Branch Source as Git (so that these options are available in the Jenkins UI, $CHANGE_ID is not available... (my code depends on it).
When using Branch Source as GitHub, these options are not available...

Idan Adar

unread,
Mar 25, 2017, 5:46:28 AM3/25/17
to Jenkins Users
As Mark mentioned, the alternative is to trigger an exception... (which will in turn show the row in red color).
I do it as follows. After checking out, if a git commit includes "[ci skip]" I use a "success" keyword (this keyword does not exit) hence the build stops.

I would love for this keyword to exist, so that the job will stop with a green row instead of a red row... Is this possible?

stage ("Checkout SCM") {
    steps
{
        script
{
           STAGE_NAME
= "Checkout SCM"
               
           checkout scm
           result
= sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true)
           
if (result == 0) {
              echo
("'ci skip' spotted in git commit. Aborting.")
              success
("'ci skip' spotted in git commit. Aborting.")
           
}
       
}
   
}
}

Stephen Connolly

unread,
Mar 25, 2017, 6:01:15 AM3/25/17
to jenkins...@googlegroups.com
Skipping polling from the git plugin is not an option for branch sources

The branch source tracks last revision built for each branch and fires a build of the current revision is not equal to the last.

What you want is a build step that stops the build with an "ignore" status of some sort.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Sent from my phone

Idan Adar

unread,
Mar 25, 2017, 6:33:20 AM3/25/17
to Jenkins Users
Thus far I have tried:

* success ("...") -- build stops with "failed" status
* sh "exit 0" -- build doesn't stop (continues to the next stage in the pipeline)

I think "exit 0" is the way to go, but didn't yet figure out exactly how... 

Idan Adar

unread,
Mar 25, 2017, 11:19:06 AM3/25/17
to Jenkins Users
I am now attempting to solve this use the environment, when and expression blocks.

Idan Adar

unread,
Mar 25, 2017, 3:53:34 PM3/25/17
to Jenkins Users
At last, resolved using parameters, when and expression blocks.

na...@algo-logic.com

unread,
Jul 24, 2017, 8:51:25 PM7/24/17
to Jenkins Users
I am currently running into a similar issue as you Idan. Within my jenkins file I push git tags, which sets off a trigger in Jenkins, building another build, and the continues in an infinite loop. I looked into the [ci skip] plugin but it does not work. I looked at your link on stack overflow but it doesn't look like it is a solution to my problem. 
Reply all
Reply to author
Forward
0 new messages