Deploy your site with git securely
This gist assumes:
- you have a local git repo
- you have an online remote repository (github)
- you have a server running Apache with git already installed
you should be able to do the same with Java, Perl, RoR, JSP etc. however you'll need to recreate the (rather simple) PHP script
1 - On your local machine
Here we add the deployment script and push it to the origin, the deployment script runs git commands to PULL from the origin thus updating your server
Grab a deployment script for your site
Add, commit and push this to github
git add deploy.php git commit -m 'Added the git deployment script' git push -u origin master
2 - On GitHub
Set up service hook
- Select Add webhook
- Enter the URL to your deployment script - http://server.com/deploy.php into Payload URL
- Generate a highly random string for the Secret
- You can keep the default for events that trigger the webhook (push event) or you can customize it yourself
- Click Add webhook
3 - On the Server
Here we clone the origin repo into a chmodded /var/www/html folder, you can change the path to your own path
Pull from origin
git clone email@example.com:you/server.git /var/www/html
You need to check that the permission for your deployment script is correct (644)
Now you're ready to go :-)
- It would be trivial to setup another repo on your server for different branches (develop, release-candidate etc) - repeat most of the steps but checkout a branch after pulling the repo down
- If you came from the fork you would notice that much had been removed and you will no longer be able to trigger deployment manually. This is changed to fit with github's recommended way of setting up webhooks but with php.
- You will need to store the random key generated above in your .htaccess file as an environment variable.
- Never hard code the random key into the code
- You will not need to add the SSH key to github, that is not neccessary.
- See Webhooks for more details.