webforward/gitdeploy

Easy to use automatic git deployment for PHP projects with composer and npm support

dev-master 2020-12-02 19:48 UTC

This package is auto-updated.

Last update: 2024-04-29 04:21:31 UTC


README

Easy to use automatic git deployment for PHP projects with composer and npm support.

Requirements

  • PHP 7.0 or above with proc_open and shell_exec functions enabled.
  • 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, npm is required for composer functionality (use_npm option).
  • The system user running PHP (e.g. www-data) needs to have the necessary access permissions for the temp_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 installed to access the remote repository. See GitHub or BitBucket for examples.

Usage - In your own project though composer

  1. Start by running the command composer require webforward/gitdeploy:dev-master .
  2. Use the following code in your project and configure options:
use GitDeploy\GitDeploy; // PSR-4 - This line must be at the top of a script, not inside a function
$gd = new GitDeploy();
$gd->remote_repository = 'https://github.com/williammalone/Simple-HTML5-Drawing-App.git';
$gd->target_dir = '/home/user/public_html';
// Your configurable options here, see deploy.php
$gd->deploy();
  1. Setup a check/method in which the above code is not publicly accessible by anyone, the code should be placed behind an admin php session, or the requirement to enter a secret key or password to run the deploy() function. Check deploy.php for an easy method of how this can be implemented.
  2. Make sure these files also exist within your git repository else (depending on your configured options) they could be removed on the next deployment.
  3. Test the deployment by visiting the script url in your browser.
  4. Once happy, follow the steps of setting up a hook. See GitHub, BitBucket or Generic Git for examples.

Usage - Standalone

  1. Download a copy of this repository into a directory within your website, for example /deploy.
  2. Edit deploy.php or make your own php file and use the following code with configure options:
use GitDeploy\GitDeploy; // This line must be at the top of a script, not inside a function
require 'path/to/src/GitDeploy.php'; 
$gd = new GitDeploy();
$gd->remote_repository = 'https://github.com/williammalone/Simple-HTML5-Drawing-App.git';
$gd->target_dir = '/home/user/public_html';
// Your configurable options here, see deploy.php
$gd->deploy();
  1. Setup a check/method in which the above code is not publicly accessible by anyone, you can use the example already written in deploy.php. For security reasons, your script should require the input of a secret key or password to run the deploy() function.
  2. Make sure these files also exist within your git repository else (depending on your configured options) they could be removed on the next deployment.
  3. Test the deployment by visiting the script url in your browser.
  4. Once happy, follow the steps of setting up a hook. See GitHub, BitBucket or Generic Git for examples.

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 URL for your deployment script e.g. https://domain.com/deploy.php?secret=changeme.
  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. https://domain.com/deploy.php?secret=changeme.
  5. Click Save.

Generic Git

  1. Configure the SSH keys.
  2. Add an executable .git/hooks/post_receive script that calls the script e.g.
#!/bin/sh
wget -q -O /dev/null https://domain.com/deploy.php?sat=changeme

You're 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 and src/GitDeploy.php.

If you find this script useful, consider donating to PayPal richard@webfwd.co.uk.