4

I'm trying to trigger a job based on a GitHub PUSH to a certain branch.

I set up a webhook to JENKINS_URL/git/notifyCommit?url=REPO_URL and Poll SCM without schedule.

The job is being triggered for every push but I cannot filter out the branch - I'd like it to only happen when there's a push to master.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Tal Auslander
  • 97
  • 1
  • 8
  • Possible duplicate of [Is it possible to trigger Jenkins from one specific branch only?](https://stackoverflow.com/questions/20713157/is-it-possible-to-trigger-jenkins-from-one-specific-branch-only) – Makoto Dec 20 '18 at 16:16

3 Answers3

0

You have to use git hook for this purpose.

The git hook get the branch name as parameter.

Hook code

#!/bin/sh

while read oldrev newrev refname
do
    branch=`echo $refname | cut -d'/' -f3`

    ###
    # Do whatever you want to do here with the branch name
    ###

done
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • What do you mean by using git hook? Isn't that what I was doing? Also, my job is a Build Flow job that orchestrates calls to multiple jobs, I cannot use shell scripts in that job – Tal Auslander May 09 '16 at 10:47
  • I showed you how to filter the branch and then decide which URL to run based upon the branch name – CodeWizard May 09 '16 at 11:10
  • This means it has to be done from a freestyle job with "Execute shell", correct? I would like to avoid it as it would pull the repository which is very big, that would result in a long pull/fetch time – Tal Auslander May 09 '16 at 13:49
  • Vice versa, you put the code on the git server which calls the build server. The commit execute the hook which call Jenkins with the branch name ad param – CodeWizard May 09 '16 at 13:55
  • The git server is GitHub, its sending the information to the webhook but the Git plugin in Jenkins doesn't process it for some reason – Tal Auslander May 09 '16 at 14:00
0

You can refer this answer : Trigger build in jenkins on specific branch in bitbucket

I just discovered that Bitbucket does not allow to choose specific hook on pushing to any branch. It just calls all the hooks, then starts all Jenkins jobs.

My solution was to create an specific file on my machine which Jenkins is installed and set a Bitbucket hook to this file. (e.g.

http://{jenkins url}:{apache port}/check.php)

Note that this apache port is not the same of Jenkins', but Apache's. In my case, Jenkins was running at 8080 and Apache at 7777.

It did this to run php script, but not in Jenkins' directory.

Since Bitbucket hook sends a json file, I was able to verify in check.php which branch has been pushed on. Reference: POST hook

management

After the verification using a simple 'if', I just called the right url to start the right job with exec_curl, like:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, http://{jenkins url}:{jenkins port}/job/{job name}/build?token={job token});
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);

And voilà.
karan kapoor
  • 91
  • 1
  • 7
0

I initially looked for some plugins but I didn't find anything to achieve as your requirements.

Initially I used to create a separate job and detect the branch there and than only build another jobs as downstream.

This is what I have done and achieved it successfully. The feature is already available in Jenkins and we don't have to install any plugin for it.

I think, the image bellow will make every thing clear.

enter image description here

So if we want to trigger the job only when the changes is pushed or merged to TEST branch we do this.

The easiest solution of all. No script , no plugins. Click on the advance button to add refspec which is hidden by default while adding the reposiotory url in Source Code Management Section

For full information read my blog on it

Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76
  • 1
    this still poke all branches, which mean if there is a new commit on any branches it will build – Idothisallday May 01 '19 at 10:22
  • @Idothisallday Its working with charm on my jenkin-server. Why don't you show the settings, just specify the branches in Refspec and it will trigger for only that branch – Tara Prasad Gurung May 02 '19 at 05:35