Automatically deploy the code using PHP and Git.
git
,rsync
, andtar
binaries are required on the server that's running the script (server machine).- The system user running PHP (e.g.
www-data
) needs to have the necessary access permissions for theTMP_DIR
andTARGET_DIR
locations on the server machine. - If the Git repo you wish to deploy is private, the system user running PHP also needs to have the right SSH keys to access the remote repository.
- Configure
deploy.php
and put it somewhere that's accessible from the Internet. - Configure your git repository to call this script when the code is updated. The instructions for GitHub and Bitbucket are below.
- (This step is only needed for private repositories) Go to
https://github.com/USERNAME/REPOSITORY/settings/keys
and add your server SSH key - Go to
https://github.com/USERNAME/REPOSITORY/admin/hooks
- Select the WebHook URLs service hook
- Enter the URL to your deployment script e.g.
http://example.com/deploy.php?sat=YourSecretAccessTokenFromDeployFile
- Click Update Settings
- (This step is only needed for private repositories) Go to
https://bitbucket.org/USERNAME/REPOSITORY/admin/deploy-keys
and add your server SSH key - Go to
https://bitbucket.org/USERNAME/REPOSITORY/admin/services
- Add POST service
- Enter the URL to your deployment script e.g.
http://example.com/deploy.php?sat=YourSecretAccessTokenFromDeployFile
- Click Save
- Configure the SSH keys
- Add a executable
.git/hooks/post_receive
script that calls the script e.g.
#!/bin/sh
echo "Triggering the code deployment ..."
wget -q -O /dev/null http://example.com/deploy.php?sat=YourSecretAccessTokenFromDeployFile
Next time you push the code to the repository that has a hook enabled, it's going to trigger the deploy.php
script which is going to pull the changes and update the code on the server machine.
For more info, read the source of deploy.php
.
Inspired by a Gist by oodavid