7

Is there a way to have a Webhook fire whenever a branch is updated in Git/Gitlab?

I am trying to set up a Development Workflow. The original idea was to have a repository for each coding phase - ex: a repository for development, a repository for testing, a repository for production.

The thing is, I need for actions to take place once the Master branch changes. For example, if the Development repository is updated, the Development code is to be "cloned", a .exe file is generated and the file is then placed into a "Development" directory for testing purposes.

Ideally, once development goes well, you "promote" the code to "testing". If "testing" goes well, you then promote the code to "production".

The problem is that there is no way to "promote" code between repositories. The only thing available is being able to merge a lower branch with a higher one.

All of the examples I have seen refer to development branches, test branches and a "production" branch is actually the master branch of the repository.

OK - I can accept this; however, in this case, I would need to compile development code based on the development branch, test code based on the test branch, etc.

For this to take place, a Webhook needs to fire so that appropriate actions can be taken to do the tasks above (hence the reason for asking my question).

So - once more - how can one get a Webhook to fire when a particular ~branch~ changes in the repository?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Casey Harrils
  • 2,793
  • 12
  • 52
  • 93

4 Answers4

2

how can one get a Webhook to fire when a particular ~branch~ changes in the repository?

That looks like issues 1176

If you look at the posted data, you have

:ref => "refs/heads/master",

Webhooks works sending a POST request to a script, with a payload describing the push.

You need a service listening to that webhook though.

If you use a Jenkins server, you can have a Jenkins GitLab plugin "branch filtering" for your webhook to point to.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi and thanks for the response. Is it absolutely necessary to use Jenkins for the processing of Webhooks? Jenkins seems to require a number of plugins in order for the User Interface to work properly (ex: a myriad of plugins are needed to do processing for PHP). Also, it does not do deployment (forcing one to locate some kind of deployment tool to work with Jenkins). From what I have seen, one type of deployment tool would work for PHP, another type would be needed for JAVA and a third one would be needed for "C/C++", etc. – Casey Harrils Mar 24 '17 at 15:33
  • Would it not be easier/faster/safer to just process the POST event using some kind of scripting language and then running the shell commands needed to do the processing? I am not really seeing the need to go through the configuration headache to use Jenkins. – Casey Harrils Mar 24 '17 at 15:33
  • To my knowledge, Jenkins requires only one plugin to interpret the GitLab webhook and trigger a job. That job in Jenkins can do whatever you need, Jenkins is just a job scheduler, whatever the nature of the job is. – VonC Mar 24 '17 at 16:45
0

Build trigger --> Build when a change is pushed to GitLab. --> Advanced --> Allowed branches --> Filter branches by name --> In the include box write the branch name for which you want to trigger the build job (Please note, write the branch name without the */ )

I have noticed that even after you type in the correct branch name it still throws the message "Following patterns don't match any branch in source repository: " You can ignore this and test your webhook. It worked for me.

0

maybe little late but for those still looking for a solution here you go. 2 branches master (production) and development 1 or 2 web server(s) , doesn't matter. In gitlab under integration set 2 webhook(s) with filter for each branch , one goes to prod URL and other to dev URL. I use script at specific address (dir) o those server url end to checkout to the right branch and pull changes. thus url will be something like this https://devurl.example.com/dir/deployment.php end in script i am calling this command e.g. 'git pull origin master or development'

Simple but works perfectly. Hoop that helps

0

I'm having the same issue with Jenkins. I want to trigger build only for branch that have changes. On gitlab documentation i didn't found explanation about branch filtering..

In my case if you add master in the filed for push triggers, it will do the job.

Vesko Vujovic
  • 362
  • 1
  • 5
  • 23