git-deploy
A PHP script to automatically pull from a repository to a web server (using a webhook on GitHub, GitLab, or Bitbucket).
You can configure which branch this script pulls from. This script is useful for both development and production servers.
On your server
SSH
Generate an SSH key and add it to your account so that git pull
can be run without a password.
Configuration
Copy the git-deploy folder and its contents in to your public folder (typically public_html). Note that you can change the name of the folder if desired.
Rename git-deploy/config.sample.php to git-deploy/config.php, and update each variable to a value that suits your needs. An example of a live configuration is below.
define("TOKEN", "secret-token");
define("REMOTE_REPOSITORY", "git@github.com:username/custom-project.git");
define("DIR", "/var/www/vhosts/repositories/custom-project");
define("BRANCH", "refs/heads/master");
define("LOGFILE", "deploy.log");
define("GIT", "/usr/bin/git");
define("AFTER_PULL", "/usr/bin/node ./node_modules/gulp/bin/gulp.js default");
On GitHub | GitLab | Bitbucket
GitHub
In your repository, navigate to Settings β Webhooks β Add webhook, and use the following settings:
- Payload URL: https://www.yoursite.com/git-deploy/deploy.php
- Content type: application/json
- Secret: The value of TOKEN in config.php
- Which events would you like to trigger this webhook?: π Just the push event
- Active: βοΈ
Click "Add webhook" to save your settings, and the script should start working.
GitLab
In your repository, navigate to Settings β Integrations, and use the following settings:
- URL: https://www.yoursite.com/git-deploy/deploy.php
- Secret Token: The value of TOKEN in config.php
- Trigger: βοΈ Push events
- Enable SSL verification: βοΈ (only if using SSL, see GitLab's documentation for more details)
Click "Add webhook" to save your settings, and the script should start working.
Bitbucket
In your repository, navigate to Settings β Webhooks β Add webhook, and use the following settings:
- Title: git-deploy
- URL: https://www.yoursite.com/git-deploy/deploy.php?token=secret-token
- Active: βοΈ
- SSL / TLS: β¬ Skip certificate verification (only if using SSL, see Bitbucket's documentation for more details)
- Triggers: π Repository push
Click "Save" to save your settings, and the script should start working.
I appreciate the collaboration of @JacobDB