Justin
Justin

Reputation: 45410

Automatically Deploy From GitHub To Server On Push

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.

Upvotes: 42

Views: 42372

Answers (6)

VonC
VonC

Reputation: 1330102

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.

Upvotes: 4

eQ19
eQ19

Reputation: 10711

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.

Upvotes: 1

hdev
hdev

Reputation: 465

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

Upvotes: 3

jesal
jesal

Reputation: 7958

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.

Upvotes: 3

Karl
Karl

Reputation: 171

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

Upvotes: 17

Kristian Glass
Kristian Glass

Reputation: 38540

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.

Upvotes: 30

Related Questions