13

I'm having some issues with Jenkins and its Git plugin.

Currently, a Gitlab server is triggering the builds but I want to configure the job so that it doesn't build when a specific message is included in the commit.

I've tried using the ci-skip plugin (https://github.com/banyan/jenkins-ci-skip-plugin) but instead of not starting the job, the plugin allows it to start but then aborts it.

It does the job but I'm having aborted builds in Jenkins history and I'm trying to avoid that.

Anyone managed to do that?

Nick Tsitlakidis
  • 2,269
  • 3
  • 22
  • 26

3 Answers3

19

Jenkins git plugin itself already provides these kinds of advanced usage.

Exclude specific messages

Job config page -->Source Code Management-->Git-->Add -->Polling ignores commits with certain messages

Exclude specific commiters

Job config page -->Source Code Management-->Git-->Add -->Polling ignores commits from certain users

Don't forget to click the help at the end of each of them to learn the correct way of usage.

Larry Shatzer
  • 3,579
  • 8
  • 29
  • 36
mainframer
  • 20,411
  • 12
  • 49
  • 68
  • 9
    This works if you are using polling for jenkins jobs. The help specifically says: ```If set, and Jenkins is set to poll for changes, Jenkins will ignore any revisions committed by users in this list when determining if a build needs to be triggered.``` - It seems like if you are using git hooks instead of polling you might be stuck. Which is odd, as polling is bad and hooks are good. – NGaida Nov 07 '16 at 20:27
  • 3
    If you're using hooks you should be checking in the hook because if your hook triggers a build then it's too late to not do the build in Jenkins. You can only abort the build. – majinnaibu Jan 31 '17 at 01:22
  • 2
    So Jenkins says this only applies to polling but in our testing we found it applies to pushes from github as well. We found that if you clear your workspace after every build it will not filter pushes appropriately, you have to retain your workspace after every build. – SgtRock Apr 11 '17 at 19:11
1

If you have not yet found a solution:

  1. Configure your job to poll SCM but do not enter a timeplan for that (ignoring the warning that your job would never run.

  2. Configure the scm section to ignore commits, users, etc.

  3. Use a trigger for your jobs as follows: http://yourserver/git/notifyCommit?url=<URL of the Git repository>[&branches=branch1[,branch2]*][&sha1=<commit ID>] as seen on Git Plugin page

Whenever the URL is called, jenkins will scan for jobs that are using this repository. Then it checks for any specific settings (e. g. ignoring commits by certain users or with some commit messages) and either run the build or not. Of course the build will always somehow start to poll scm and check for commits, but if it is cancelled due to unimportant commits you will not see an aborted build in your history.

Clerenz
  • 821
  • 8
  • 23
  • is there a way to do so visa versa? build on specific commit message of a branch? – Canberk Ozcelik Aug 29 '17 at 15:03
  • @CanberkOzcelik I have not tried, but as it is a pattern that you define, maybe a negative lookahead could help you out, see examples: https://stackoverflow.com/questions/1749437/regular-expression-negative-lookahead https://stackoverflow.com/questions/44390515/exclude-jenkins-jobs-from-view-with-regular-expression – Clerenz Oct 24 '17 at 10:52
0

You will need to use external script.

Pull out the project (git clone/fetch) and then before continuing with your build check to see if the specific message is in your repository.

To check to see if your message is there you can use the git show or git log commands.

git log

git log --all --grep='<your message>'
# and you can add more flags if you want of course:
git log --all --oneline | grep '<your message>'

git grep

git grep '<your message>' $(git rev-list --all)

Outside of Jenkins

Add code to git hook which check what you want to find *messages, commit etc) and only if it find it call Jenkins via REST API and start your job.

This is the way im using Jenkins, i check for something that i looking for and then im invoking Jenkins via API

Read more here & here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Where would this script be executed? Inside the job? If so, the job is still started and the effect is the same as the ci-skip plugin. – Nick Tsitlakidis Nov 29 '15 at 10:22
  • Yes exactly. run the script and then decide if to skip or not based upon the result. – CodeWizard Nov 29 '15 at 12:43
  • 2
    That's cool but it does not solve my issue because the job will still start and then be displayed as aborted in the Jenkins history. I was wondering if its possible to configure it so that it doesn't start at all. – Nick Tsitlakidis Nov 29 '15 at 12:45
  • You start it from Jenkins, if you don't want you can set the job status to be success or something else. Another way is to use git hooks - updating my answer to support it and this is what you are looking for – CodeWizard Nov 29 '15 at 12:48
  • Setting it to success or fail would corrupt any metrics. Abort or nothing are the only viable options. I'd argue that there's nothing wrong with abort other than having to see it in your log. I think the real problem is that if the job starts, that means an agent had to be scheduled. If your org is large, you could be adding load/lag by scheduling jobs only to abort them, so ideally we'd want something that short-circuited that based on the commit log. Hooks can be tricky because you might not be able to add any logic to those in your SCM – Sinaesthetic Sep 22 '22 at 17:06