4

Jenkins triggers whenever I push onto my remote branch. A pipeline is then run using my Jenkinsfile script.

Within the jenkinsfile I git push tags, this command however triggers Jenkins again and kicks off another build, which then in turn runs git push tags causing an infinite loop.

How can I avoid jenkins triggering on git commands within jenkinsfile?
I have spent over a week searching for a solution and nothing seems to be working, thank you.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
Nadim
  • 81
  • 1
  • 4

3 Answers3

1

I assume that you are using a Multibranch Pipeline or GitHub/Bitbucket Organisation.

You can Suppress automatic SCM triggering for all or specified branches under the Branch Sources configuration.

enter image description here

StephenKing
  • 36,187
  • 11
  • 83
  • 112
  • I am using pipeline not multibranch pipeline, therefore I do not have the option to suppress automatic SCM triggering – Nadim Jul 26 '17 at 21:25
  • Any reason why you're using that? I really don't see an advantage of the Pipeline job type. – StephenKing Jul 26 '17 at 22:18
  • Its the way my company has their current jenkins configuration set up. I have been trying to find a solution on how to stop jenkins from triggering on my tag push command from the jenkinsfile for over a week. But no luck so far. – Nadim Jul 26 '17 at 22:24
  • I decided to create a multibranch pipeline project, however How can I save configurations for a specific branch? – Nadim Jul 26 '17 at 23:44
  • Use an `if` condition in your `Jenkinsfile`. You find the branch name in `env.BRANCH_NAME`. – StephenKing Jul 27 '17 at 06:39
0

Normally, Jenkins triggers a build with a (non-Jenkinsfile) refspec like refs/tags/*

So to avoid this, you could specify refs/heads/* (as seen here), or check issue 507 and uncheck "lightweight checkout" to avoid getting tags.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • But that's completely unrelated to [tag:jenkins-pipeline] (the technique behind `Jenkinsfile`s). – StephenKing Jul 26 '17 at 07:45
  • I already have the lightweight checkout box unchecked. As for the refs/heads/* would I place that path in the branch specifier box or the refspec box of the job configuration? – Nadim Jul 26 '17 at 21:29
  • @Nadim branch specifier box (if not, test the other as well) – VonC Jul 26 '17 at 21:34
  • The trigger still happens when using refs/heads/* Do you know another method that will allow Jenkins to trigger on all pushes except tag pushes? – Nadim Jul 26 '17 at 22:13
0

You can do this with Generic Webhook Trigger Plugin.

There are many alternativs like:

  • Not trigger if commit message has a special format.
  • Not trigger if a specific user pushes commits. Like a special user used in Jenkins.

From one of the test cases:

Scenario: Trigger a job when commit is pushed and the pusher is not named "build".

Given the following generic variables are configured:
  | variable   | expression    | expressionType  | defaultValue | regexpFilter  |
  | user       | $.pusher.name | JSONPath        |              |               |

Given filter is configured with:
  | text   | expression   |
  | $user  | ^((?!build)) |

Given received post content is:
"""
{
  "pusher": {
    "name": "baxterthehacker",
  }
}
"""
Then the job is triggered

Given received post content is:
"""
{
  "pusher": {
    "name": "build",
  }
}
"""
Then the job is not triggered
Tomas Bjerre
  • 3,270
  • 22
  • 27