Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Github push to a remote server when it receives updates

What is the set up for having Github automatically push any updates to a remote server?

This is useful for maintaining a codebase on Github, and having a website run off that codebase.

  1. I have my repo on my own computer, this is where I work.

  2. I commit my changes on my local repo, and push them to my Github repo.

  3. I want my Github repo to then push these changes to my remote server.

I've been researching all day, and using the 'hooks' sounds reasonable. Maybe using a 'post-receive' hook on Github which then runs a push command to my remote server.

Any suggestions?

like image 574
Don P Avatar asked Mar 04 '13 07:03

Don P


People also ask

How do I push committed changes to remote?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.

How do I push updated code to GitHub repository?

At the top of your repository on GitHub.com's Quick Setup page, click to copy the remote repository URL. In the Command prompt, add the URL for the remote repository where your local repository will be pushed. Push the changes in your local repository to GitHub.com.

How do I push changes from local to remote branch?

Push a new Git branch to a remote repo Clone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch.

Does git push push to remote?

The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. It's the counterpart to git fetch , but whereas fetching imports commits to local branches, pushing exports commits to remote branches.


2 Answers

As I understand github doesn't allow you to define "true" hooks. Like post-receive. Instead they provide something called a webhook to developers. what you can do with this is issue a web request to any URL specified by you whenever there's a push to your repository.

So what you can do is: setup a webserver on you remote git server and configure github to make an http call to it on post-receive. Whenever github notifies your remote server do a pull on it from github.

See here on how to use webhooks: https://help.github.com/articles/post-receive-hooks

P.S. A true hook mechianism whould have been a possible security vulnerability for github cause it allows you to execute custom code on their servers. So they have made something that does not allow you to execute anything but still allows you to do anything you want.

like image 81
Yervand Aghababyan Avatar answered Nov 15 '22 22:11

Yervand Aghababyan


To illustrate Yervand's answer (upvoted), consider this peligangit as an example of a simple HTTP server (that you can install on your amazon-ec2 instance), which will:

  • start a simple HTTP server.
  • listen for a POST from a GitHub webhook
  • it will pull down the new commits

workflow

That library would fetch, and then reset the main branch on origin/master.
That is one way to do it. (see githook.py)

def hard_reset_repos(self):
    self.server.source_repo.fetch([self.server.source_repo.origin])
    self.server.source_repo.reset(['--hard', self.server.source_repo.originMaster])
like image 43
VonC Avatar answered Nov 15 '22 23:11

VonC