webforward / gitdeploy
Easy to use automatic git deployment for PHP projects with composer and npm support
dev-master
2020-12-02 19:48 UTC
Requires
- php: >=7.0
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
andshell_exec
functions enabled. git
andrsync
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).
- Optionally,
- The system user running PHP (e.g.
www-data
) needs to have the necessary access permissions for thetemp_dir
andtarget_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
- Start by running the command
composer require webforward/gitdeploy:dev-master
. - 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();
- 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. - Make sure these files also exist within your git repository else (depending on your configured options) they could be removed on the next deployment.
- Test the deployment by visiting the script url in your browser.
- Once happy, follow the steps of setting up a hook. See GitHub, BitBucket or Generic Git for examples.
Usage - Standalone
- Download a copy of this repository into a directory within your website, for example /deploy.
- 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();
- 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. - Make sure these files also exist within your git repository else (depending on your configured options) they could be removed on the next deployment.
- Test the deployment by visiting the script url in your browser.
- Once happy, follow the steps of setting up a hook. See GitHub, BitBucket or Generic Git for examples.
GitHub
- (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/settings/hooks
. - Click Add webhook in the Webhooks panel.
- Enter the URL for your deployment script e.g.
https://domain.com/deploy.php?secret=changeme
. - Optional Choose which events should trigger the deployment.
- Make sure that the Active checkbox is checked.
- Click Add webhook.
Bitbucket
- (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.
https://domain.com/deploy.php?secret=changeme
. - Click Save.
Generic Git
- Configure the SSH keys.
- 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
.