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
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
See deploy.php
git add deploy.php
git commit -m 'Added the git deployment script'
git push -u origin master
- https://github.com/oodavid/server.com/settings/hooks
- 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
Here we clone the origin repo into a chmodded /var/www/html folder, you can change the path to your own path
git clone [email protected]: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.
For me, the
if (hash_equals($signature, verify_request())){
keeps failing so I get the 403 error. I verified that the secret code in htaccess is the same as that used when setting up the webhook.I did make the change rionda had to, changing
$key = $_ENV['GIT_TOKEN'];
to$key = getenv('GIT_TOKEN');
but either way that I had it it didn't work.The weird thing is that it did work for about 8 times in the middle of about 30 times that the webhook was called, but I haven't been able to figure out what was different then.
I'm hitting a wall on troubleshooting this...any suggestions?