40

We have a VPS on Linode, and code hosted on GitHub. How do we setup so when we push to GitHub, it also pushes automatically to our Linode server. We are using PHP on the Linode server.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Justin
  • 42,716
  • 77
  • 201
  • 296

6 Answers6

28

You probably want to use GitHub's post-receive hooks.

In summary, GitHub will POST to a supplied URL when someone pushes to the repo. Just write a short PHP script to run on your linode VPS and pull from GitHub when it receives said POST.

Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
  • This sounds great, but how do you "pull from github" from PHP? – Brian FitzGerald Jun 17 '14 at 13:54
  • 1
    I assume some library exists, but in the worst case, more use of `exec` than ideal – Kristian Glass Jun 17 '14 at 17:04
  • 1
    If I am not mistaken, in order for you to "pull from github" from yuor server, you need to install git on your server. I hope I am right – CodeGodie Sep 09 '14 at 18:31
  • @BrianFitzGerald Create a PHP file containing ``. Then, point a post-receive hook at the URL for your PHP file. Every time you `push` to GitHub, the post-receive hook will "call" (request via HTTP) your PHP file and pull the latest code. Neat! – rinogo May 03 '19 at 21:49
15

I wrote a small Github-Auto-Deploy server in python, that does exactly what you want.

  • Enter your domain to a new post-receive service hook on Github
  • Match local repository paths with repository urls in the config file
  • The server will receive requests from github and run git pull in local repository path
  • It also runs a shell script for deployment afterwards if you provide one
Karl
  • 151
  • 1
  • 4
  • 2
    your lib inspired me to start on an implementation of my own. https://github.com/danneu/captain-githook. good stuff, dude. – danneu Sep 13 '13 at 18:57
  • #sigh if it was in PHP it would be in context with the question... it may as well be in turbo pascal – Dawesi Apr 06 '18 at 10:49
  • @Dawesi: How is the language used in the *deployment script* relevant for your app? Is there something preventing the python script mentioned from deploying your PHP/HTML/CSS/JPG files? – rinogo May 03 '19 at 21:10
4

Using any kind of webhook involves deploying a listener for that hook, and then triggering, from your listener server host, the action.

You can take a shortcut now (oct. 2018) with GitHub Actions (Oct. 2018).

GitHub Actions allows you to connect and share containers to run your software development workflow. Easily build, package, release, update, and deploy your project in any language—on GitHub or any external system—without having to run code yourself.

See Actions: pushing is only one of the possibilities behind Actions!

Workflows can be triggered by GitHub platform events (i.e. push, issue, release) and can run a sequence of serial or parallel actions in response. Combine and configure actions for the services you know and love built and maintained by the community.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

I ended up creating my own rudimentary deployment tool (much like Karl but in PHP) which would automatically pull down new updates from the repo - https://github.com/jesalg/SlimJim - Basically it listens to the github post-receive-hook and uses a proxy to trigger an update script.

jesal
  • 7,852
  • 6
  • 50
  • 56
2

Maybe i'm out of context but i prefer to manually choose where to push from my command line eg: git push linode

To do this i create a repository container in my linode server and created a post-receive hook that checkouts my folder to the last pushed commit

Create a git repo container mkdir /var/repo && cd /var/repo git --bare init

Create the post-receive hook in /var/repo/hooks/ touch post-receive nano post-receive chmod +x post-receive

post-receive content #!/bin/sh git --work-tree=/var/www/ --git-dir=/var/repo checkout -f

On your local repository git remote add linode root@<linode ip|domain>:/var/repo git push linode

your code is now deployed

hdev
  • 430
  • 1
  • 3
  • 10
  • Interesting. Thank you for that. I would follow Linode's recommandation not to use @root user. This requires looser permissions on the git repo directory and on /var/www. I found relevant infos on how to do that here: http://stackoverflow.com/a/6448326/2112538 – François Romain May 11 '17 at 20:57
1

You may refer to this tutorial:
Automatically Updating Your Website Using GitHub's Service Hooks:

In short it explains the following steps:

Create a php file in .git folder on your server with the following contents.

<?php `git pull`;?>

Setup your server for the SSH keys to exist. Something like:

key. cat ~/.ssh/id_rsa.pub

Setup the service hook on GitHub . Enter WebHook URL:

http://your.domain.com/path/to/yourfile.php

When all is set. The file is going deploy all the work on your server each time you push to GitHub.

eQ19
  • 9,880
  • 3
  • 65
  • 77