Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a script after every git push

Tags:

git

linux

bash

There is a server running and has a git instance on it. I want a script to run everytime a user does git push to the server. I want my script to be executed then git push to continue. Any work arounds?

like image 945
Ninja Boy Avatar asked Aug 25 '16 04:08

Ninja Boy


2 Answers

You've tagged this GitHub so I'm assuming that you are referring to public GitHub and not GitHub enterprise.

You cannot run a script "server-side" on GitHub's servers because that would obviously be a massive vulnerability but you can set up a web hook to trigger a script on another server.

Basically whenever someone does a push, a specific URL will be sent data about the push. You can then trigger a script from this. For more information on web hooks, see the GitHub API docs.

like image 62
Chris Avatar answered Sep 27 '22 21:09

Chris


I am not sure If you want a scipt to run prior to push or after. So here is my answer for pre-push. But if you want post-push (i.e after push) you have to change the pre-push hooks accordingly to check if pushed successfully and then you can do post push thing.

As suggested by @Travis, git hooks is the one that you are looking for. So to execute a script pre-push, you have to do is to create a pre-push file in the .git/hooks. So in this case put your bunch of code in the pre-post script file .git/hooks/pre-push and save it. Then make it executable by chmod +x .git/hooks/pre-push. After you done with this successfully you will be able to see the script gets executed each time you do run push command.

PS: Please note that I haven't tested this whole but expected to work in this way.

In Short, assuming you(Linux user) are in the project directory

vim .git/hooks/pre-push # then add your code and save the file
                        # Also put the shebang on top to identify the interpreter
chmod +x .git/hooks/pre-push # make it executable
like image 43
MaNKuR Avatar answered Sep 27 '22 20:09

MaNKuR