A PHP script to automatically pull from a GitHub repository when it is updated.
You can configure which branch to pull from, files to be deleted after this pull (e.g. .gitignore, LICENSE, etc.) and integrate with Slack to know when the pull has been successful.
Generate an SSH key and add it to your account so that git pull
can be run on private repos and without a password.
Check out the GitHub documentation for detailed instructions.
-
Copy this repo into a publically accessible directory on your server (e.g. www, public_html, etc.)
-
Rename
config.sample.php
toconfig.php
and update each variable to the desired value. For example:define('SECRET', 'sTrOnG_sEcReT'); define('DIR', '/var/www/example.com/'); define('BRANCH', 'refs/heads/master'); define('LOGFILE', 'git-deploy.log'); define('GIT', '/usr/bin/git'); define('DELETION', array('README.md', '.gitattributes')); define('SLACK_HOOK', 'https://hooks.slack.com/services/SLACK_KEY';
The
SLACK_HOOK
andDELETION
variables can be empty if not required. -
Adjust the permissions for the directory so that it is accessible by the webserver user (e.g. www, www-data, apache, etc.)
- Open the termial and navigate to the directory containing the repository on the server.
- Run
sudo chown -R yourusername:webserverusername git-deploy
to change the group. - Run
sudo chmod -R g+s git-deploy
to ensure that permissions are inherited by all files and directories. - Run
sudo chmod -R 775 git-deploy
to set read and write permissions.
You need to configure GitHub to notify your endpoint when the repository is updated.
In your repository, navigate to Settings → Webhooks → Add webhook, and use the following settings:
- Payload URL: https://www.yoursite.com/location-of/deploy.php
- Content type: application/json
- Secret: The value of
SECRET
inconfig.php
- Which events would you like to trigger this webhook?: 🔘 Just the push event
- Active: ☑️ Selected
Click 'Add webhook' to save your settings, and allow the script to start working.
You need to configure a Slack app so that the script can post messages to your workspace.
-
Sign in to your Slack workspace using a web browser.
-
At the top right, navigate to ⚙️ → Add an app, then type "Incoming Webhook" into the text field.
-
Click on the Incoming Webhook app and create a new configurarion.
-
Select the channel you wish the script to post to when the repository is updated, then click Add Incoming Webhooks Integration.
-
Copy the Webhook URL and set it as the value of
SLACK_HOOK
inconfig.php
The script will now post the status of each pull to the Slack channel. You can customise the integration's settings such as appearence and channel as you see fit.
Once set up, the script will work automatically as the repository is updated. You are able to update the configuration as you wish.
To check the status of the script you can:
- View the output of the log at the
LOGFILE
location provided withinconfig.php
- Head to your repository on GitHub. Navigate to Settings → Webhooks → Edit, and then view the recent deliveries section.
- View the message that have been posted by the integration on your Slack channel.
For exmaple, Slack will show you this when setup correctly:
Distributed under the MIT Licence. More information is available at LICENSE.
- Daniel Turner - turnerdaniel
- Vicente Guerra - For their original work on the git deploy script which this project is based on.