Ninja Boy
Ninja Boy

Reputation: 1170

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?

Upvotes: 11

Views: 20206

Answers (4)

Dan Bolan
Dan Bolan

Reputation: 11

I'm late to the party but wanted to add some information in case someone else wants to do this.

If you're running your own server as a git remote, you probably want to look into the post-receive githook.

If you're using GitHub, you'll need to use GitHub Actions. The actions/starter-workflows repository has a lot of good examples of the YAML files you'll need for that.

Upvotes: 0

MaNKuR
MaNKuR

Reputation: 2704

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

Upvotes: 9

Chris
Chris

Reputation: 5876

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.

Upvotes: 4

Travis Rodman
Travis Rodman

Reputation: 637

You should look into git hooks:

8.3 Customizing Git - Git Hooks

and, another site regarding this technology:

githooks.com

Upvotes: 2

Related Questions