A simple PHP script to auto-deploy from Github and Bitbucket, and executing a command like composer install
###Installation :
-
Very simple, download the
deploy.php
file and put it in the root of your code directory. If you're working with Bitbucket, uncomment line 12 and comment line 9. -
Create a new repo on Github or Bitbucket, and push all your of code.
-
Add your server's SSH key to Github or Bitbucket.
-
In your server, clone the repo from Github or Bitbucket, for example :
git clone git@github.com:user/repo.git
-
Change the repository's owner on your server using :
chown -R your_user:www-data repo
. Note that you may have tosudo
that command ;) -
You may, also, have to check the permissions for subdirectories and files: files => 644, folders => 755.
-
Add a hook to Github or Bitbucket :
- Github : goto
https://github.com/user/repo/settings/hooks/new
, in "Payload URL", put the url to deploy.php from your server click on "Add webhook" button
- Bitbucket: goto https://bitbucket.org/user/repo/admin/hooks, select "POST" option in the select box, click on "Add hook", parte your URL to deploy.php file and click on "save"
- That's all! For the first time, you should check that the
git pull
command is working. Echo the commandsudo -u www-data git pull
. If you get some errors likeerror: cannot open .git/FETCH_HEAD: Permission denied
you should check the permissions for the repository's ".git" folder.
You will also, if you're doing this for the first time, be prompted add Github or Bitbucket to your~/.ssh/known_hosts
file, like this :
When you commit and you push automatically to Github or Bitbucket, it'll send a post request to http://www.yourwebsite.com/deploy.php
, and this will execute a git pull
Note: In Step 3 I had a problem with SSH key when I added the default SSH key, so if you have the same problem, you have to generate a SSH key for www-data using : sudo -u www-data ssh-keygen -t rsa
and then add it to your account.
For github, goto : https://github.com/user/repo/settings/keys, click on "Add deploy key".
For Bitbucket, goto : https://bitbucket.org/user/repo/admin/deploy-keys, click on "Add key" button.
###For Laravel users To avoid route exception you need to disable Laravel routing for the webhook route/url :
-
open public/.htaccess
-
add before the redirect trailing slashes rule
#Exclude directory deploy from rewriting eg "http://your_url/deploy.php"
RewriteRule ^(deploy.php) - [L]
###How to execute a command after the git pull
?
Very simple, in your commit message put the command between a "[ ]". For example : git commit -m "first commit [composer install]"
, when the server (deploy.php) detects the "[ ]" symbol, it extracts the text between them, and executes the included command, ex : composer install
.
###More things:
- You can change the "[ ]" with other symbols, to do so, goto line 15 and change the '[' and ']' with the symbol you want to use, for example if you want to use "{ }" instead of the default one, the pattern will be
$pattern = '/\{(.*?)\}/';
. - You can secure your POST request using a key in the hook. For example add a key like
http://www.yourwebsite.com/deploy.php?key=123456
and the deploy.php file will check for thekey
variable. But sure, you can imagine other methods. - This solution is inspired from @jondavidjohn's article Git pull from a php script, not so simple.
- If you have any problem or contribution do not hesitate.
- Make sure to report any issues here ;)