简体   繁体   中英

Execute a script after every git push

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?

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 (ie 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

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 .

You should look into git hooks:

8.3 Customizing Git - Git Hooks

and, another site regarding this technology:

githooks.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM