Auto git deploy php script
Katz Ueno
Posted on August 8, 2019
I've made a sample PHP script to auto deploy git using webhook feature which GitHub, Bitbucket and other servives provide.
Please read the comments inside of deployments.php for the option and how to set-it up.
This deployment script requires certain level of git and server knowledge.
This script is intended for single server use.
Please check it out and test drive, and send me issue or PR to improve the script.
GitHub Repo
https://github.com/katzueno/git-Webhooks-Auto-Deploy-PHP-Script
How to set it up
1. Set up git
Prepare your git repo
2. Set-up server
It is highly recommended to prepare two different domain or subdomains within the same server.
Set-up where you set git deployment script and public area where your actual git deployment.
You must make sure git deploy script is protected with basic auth and SSL.
Obtain all necessary information such as server paths.
3. Git clone to the server
Regular way
- Login via SSH
- cd to the path
-
git clone [GIT PATH]
- Make sure you git clone as the same user as web server. If you're using shared host, you may not have to worry about.
- e.g.,)
sudo -u (nginx|apache) git clong [GIT PATH]
- Make sure that /.git directory is NOT publicly visible to the web.
- Apache: add this line to .htaccess:
RedirectMatch 404 /\.git
(mod_rewrite is required) - Nginx: add this line to your Nginx config
location ~ /\.git { return 404; }
- Apache: add this line to .htaccess:
Separate Git Directory and Work Directory (more secure)
Is is very secure way of place git repository outside of publicly visible www root.
- Login via SSH
- cd to the path
-
git clone --mirror [GIT PATH]
to the directory GIT_WORK_TREE=[www_path] git checkout -f [your desired branch]
3. Set your config & upload the file
- Set all necessary config setting inside of deployement.php
- Read carefully from line 1-45. You won't need to change below
Main Section
- Read carefully from line 1-45. You won't need to change below
- Upload your
deployment.php
to where you stage your script- Make sure to upload in secure area.
- You may rename filename.
4. Set-up a webhook and test drive
Your web hook URL will be like this.
Set it up as webhook of GitHub, Bitbucket, Gitlab or whatever other git services which supports webhook.
https://[Basic Auth ID]:[Basic Auth Pass]@example.com/deployments.php?key=YourSecretKeyHere
and enjoy the rest of auto deployment.
Posted on August 8, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.