Deploy script for PHP or Jekyll sites
Clone or download
Pull request Compare This branch is 10 commits ahead of markomarkovic:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
.gitignore
LICENSE
README.md
background_runner.php
deploy.php
functions.php
loggers.php

README.md

Simple PHP Git deploy script

Automatically deploy code using PHP and Git.

  • Deploys Jekyll and/or Composer projects
  • Multi-site support
  • Background execution mode for long-running deploys
  • Multi-process safe: ready to serve as an on-push webhook, even for projects with frequent pushes
  • CloudFlare cache-purging

This is a significantly enhanced fork of Marko Marković's original project.

Requirements

  • git and rsync are required on the server that's running the script (server machine).
    • Optionally, tar is required for backup functionality (BACKUP_DIR option).
    • Optionally, composer is required for composer functionality (USE_COMPOSER option).
    • Optionally, jekyll is required for jekyll functionality (USE_JEKYLL option).
  • The system user running PHP (e.g. www-data) needs to have the necessary access permissions for the TMP_DIR and TARGET_DIR locations 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.

Usage

Initial Setup

  • Put the scripts somewhere that's accessible from the Internet.

Configure a New Deployment

  • Copy config/default.json to <site-name>.json and edit the configuration options there. If you'd like to have shared configuration across multiple sites, add them to config/shared.json (leave default as-is to make future updates easier).

  • Ensure that you have a unique secret key set in the configuration file.

  • Configure your git repository to call deploy.php when the code is updated. The instructions for GitHub and Bitbucket are below.

    Note that you can have as many configuration files/sites as you like.

GitHub

  1. (This step is only needed for private repositories) Go to https://github.com/USERNAME/REPOSITORY/settings/keys and add your server SSH key.
  2. Go to https://github.com/USERNAME/REPOSITORY/settings/hooks.
  3. Click Add webhook in the Webhooks panel.
  4. Enter the Payload URL for your deployment script e.g. http://example.com/deploy.php?sat=YourSecretAccessTokenFromDeployFile?site=<site-name>.
  5. Optional Choose which events should trigger the deployment.
  6. Make sure that the Active checkbox is checked.
  7. Click Add webhook.

Bitbucket

  1. (This step is only needed for private repositories) Go to https://bitbucket.org/USERNAME/REPOSITORY/admin/deploy-keys and add your server SSH key.
  2. Go to https://bitbucket.org/USERNAME/REPOSITORY/admin/services.
  3. Add POST service.
  4. Enter the URL to your deployment script e.g. http://example.com/deploy.php?sat=YourSecretAccessTokenFromDeployFile?site=<site-name>.
  5. Click Save.

Generic Git

  1. Configure the SSH keys.
  2. 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?site=<site-name>

Done!

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.

Tips'n'Tricks

  • Because rsync is used for deployment, the TARGET_DIR doesn'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.