0

I have a GitLab project with a WebHook which triggers a Jenkins job. As of now, I have enabled Push Events to trigger the Jenkins job. So, whenever I push code from my local computer to remote repository, the Jenkins job is automatically triggered and the job successfully builds my codebase by checking out the code from GitLab's master branch.

Is there an option using which I can have the Webhook trigger Jenkins build only on such push events whose commit message contains a specified string like "build jenkins"?

I assume the answer to above to be No as the Webhook has already been configured and can't be stopped. However, I read slightly about creating custom hooks on the remote repository which can help me in above. If that is true, how can I do that and where do I need to write the custom hook and how to use it?

Thanks

letsbondiway
  • 470
  • 3
  • 18
  • Are you using gitlab hook plugin ? You can read the commit message in webhook payload and decide to run your build or not. https://github.com/jenkinsci/gitlab-hook-plugin/blob/master/README.md – ben5556 Nov 22 '18 at 20:11
  • Thanks @ben5556. I had a quick look and looks like I am not using gitlab hook plugin build_now. Will try that out. However, what I wanted to know was that will this trigger the Jenkins job to start or not. Would it be like it will start the job and then read the commit message in webhook payload and abort the job or would it be like it will not start the job at all? – letsbondiway Nov 24 '18 at 04:41
  • What I’d recommend is have a single job which is triggered by the gitlab hook and then trigger your actual job based on the commit message. You can also extend this to trigger any of your other jobs based on the repo, branch and commit message from which the webhook came. – ben5556 Nov 24 '18 at 06:01
  • 1
    We use a different plugin - Generic Webhook Trigger Plugin and it works great! Check it out https://wiki.jenkins.io/plugins/servlet/mobile?contentId=106267244#content/view/106267244 – ben5556 Nov 24 '18 at 06:02
  • Perfect. That worked @ben5556. Thanks! – letsbondiway Nov 26 '18 at 18:50
  • Can you please elaborate on what worked so I can post it as an answer to close out this thread ? :) – ben5556 Nov 26 '18 at 20:18
  • Oh sure! I installed Generic Webhook Trigger plugin on Jenkins. Configured its Post content parameters, specifically the commit_message. I set that as the Variable, Expression as - $.commits[0].message. Under Optional filter I evaluated the Text - $commit_message with Expression set to Regex of commit message format - Deploy\sTo\sUAT. Whenever I pushed to GitLab with a commit message containing substring "Deploy To UAT", it triggered the Jenkins job. If commit message did not contain that, it just ignored and didn't trigger the job. – letsbondiway Nov 29 '18 at 15:32
  • And yeah off course, I added a webhook on GitLab - http://jenkins-url/generic-webhook-trigger/invoke?token=xxxxx. I generated a 16 character string online and used that as token value. I also set this token value in the Token field under GWT plugin's configuration on Jenkins. I dont know how secure this token generation and usage mechanism is and if there is a better way for the token thing. If you can shed some light on that, that will be an additional help. Thanks again! – letsbondiway Nov 29 '18 at 15:33
  • The token feature is good, we use it sort of like an API token for security. Using 16 characeter random string like you already did is good enough. – ben5556 Nov 30 '18 at 05:15
  • Added as an answer. Mark it if it helped! – ben5556 Nov 30 '18 at 05:17

1 Answers1

2

Recommend using Generic Webhook Trigger Plugin - https://wiki.jenkins.io/display/JENKINS/Generic+Webhook+Trigger+Plugin

ben5556
  • 2,915
  • 2
  • 11
  • 16