Automatically deploy code using PHP and Git.
This script is largly based on https://github.com/markomarkovic/simple-php-git-deploy/tree/1.3.1 version 1.3.1 but modified for my usecases
gitandrsyncare required on the server that's running the script (server machine).- Optionally,
taris required for backup functionality (BACKUP_DIRoption). - Optionally,
composeris required for composer functionality (USE_COMPOSERoption).
- Optionally,
- The system user running PHP (e.g.
www-data) needs to have the necessary access permissions for theTMP_DIRandTARGET_DIRlocations on the server machine. - If the Git repository 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 the script and put it somewhere that's accessible from the
Internet. The preferred way to configure it is to use
deploy-config.phpfile. Renamedeploy-config.example.phptodeploy-config.phpand edit the configuration options there. That way, you won't have to edit the configuration again if you download the new version ofdeploy.php. - 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/keysand add your server SSH key. - Go to
https://github.com/USERNAME/REPOSITORY/settings/hooks. - Click Add webhook in the Webhooks panel.
- Enter the Payload URL for your deployment script e.g.
https://example.com/deploy.php. - Configure the option Content type with
application/x-www-from-urlencoded. - Add the value from
SECRET_ACCESS_TOKENas to the field Secret. - Optional Choose which events should trigger the deployment usually thats just
push. - Make sure that the Active checkbox is checked.
- Click Add webhook.
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.
- Because
rsyncis used for deployment, theTARGET_DIRdoesn't have to be on the same server that the script is running e.g.define('TARGET_DIR', 'username@example.com:/full/path/to/target_dir/');is going to work as long as the user has the right SSH keys and access permissions. - You can have multiple scripts with different configurations. Simply rename
the
deploy.phpto something else, for exampledeploy_master.phpanddeploy_develop.phpand configure them separately. In that case, the configuration files need to be nameddeploy_master-config.phpanddeploy_develop-config.phprespectively.