├── simple-deploy.php ├── deploy.php └── README.md /simple-deploy.php: -------------------------------------------------------------------------------- 1 | head_commit->message ; // get the commit from Github 10 | 11 | # if you don't use Bitbucket, uncomment the next line 12 | //$theCommitMessage = json_decode($_POST['payload'])->commits[0]->message; // get the commit from Bitbucket 13 | 14 | # retrieve the command 15 | $pattern = '/\[(.*?)\]/'; 16 | preg_match( $pattern, $theCommitMessage, $match ); 17 | 18 | if (!empty($match)) { // if we have somthing like "my commit [rm file]" 19 | $theCommand = $match[1]; // get the command 20 | shell_exec($theCommand); // Execute the command 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # autodeploy-github-bitbucket 2 | A simple PHP script to auto-deploy from Github and Bitbucket, and executing a command like `composer install` 3 | 4 | ###Installation : 5 | 6 | 0. 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. 7 | 0. Create a new repo on **Github** or **Bitbucket**, and push all your of code. 8 | 0. Add your server's SSH key to **Github** or **Bitbucket**. 9 | 10 | 0. In your server, clone the repo from **Github** or **Bitbucket**, for example : `git clone git@github.com:user/repo.git` 11 | 0. Change the repository's owner on your server using : `chown -R your_user:www-data repo`. Note that you may have to `sudo` that command ;) 12 | 0. You may, also, have to check the permissions for subdirectories and files: _files => 644, folders => 755_. 13 | 0. Add a hook to **Github** or **Bitbucket** : 14 | * **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 15 | ![alt text](http://i.imgur.com/9eOL0qp.png) 16 | * **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"_ 17 | ![alt text](http://i.imgur.com/ePCZBkp.png ) 18 | 19 | 0. That's all! For the first time, you should check that the `git pull` command is working. Echo the command `sudo -u www-data git pull`. If you get some errors like `error: cannot open .git/FETCH_HEAD: Permission denied` you should check the permissions for the repository's _".git"_ folder. 20 | 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 : 21 | ![alt text](http://i.imgur.com/RHLLHbe.png ) 22 | 23 | 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` 24 | 25 | 26 | --- 27 | 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._ 28 | _For github, goto : https://github.com/user/repo/settings/keys, click on "Add deploy key"._ 29 | _For Bitbucket, goto : https://bitbucket.org/user/repo/admin/deploy-keys, click on "Add key" button._ 30 | 31 | --- 32 | 33 | 34 | ###For Laravel users 35 | To avoid route exception you need to disable Laravel routing for the webhook route/url : 36 | 37 | 1. open public/.htaccess 38 | 39 | 40 | 2. add before the redirect trailing slashes rule 41 | 42 | ``` 43 | #Exclude directory deploy from rewriting eg "http://your_url/deploy.php" 44 | RewriteRule ^(deploy.php) - [L] 45 | ``` 46 | 47 | ###How to execute a command after the `git pull` ? 48 | 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`. 49 | 50 | 51 | 52 | 53 | ###More things: 54 | - 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 = '/\{(.*?)\}/';`. 55 | - 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 the `key` variable. But sure, you can imagine other methods. 56 | - This solution is inspired from [@jondavidjohn's](https://twitter.com/jondavidjohn) article [Git pull from a php script, not so simple.](http://jondavidjohn.com/git-pull-from-a-php-script-not-so-simple) 57 | - If you have any problem or contribution do not hesitate. 58 | - Make sure to report any issues [here](https://github.com/kossa/autodeploy-github-bitbucket/issues) ;) 59 | --------------------------------------------------------------------------------