├── .travis.yml ├── README.md ├── codeigniter ├── README.md ├── config │ └── github.php └── controllers │ └── deploy.php ├── composer.json ├── phpunit_mysql.xml ├── src └── Web │ ├── Deploy.php │ └── config.php └── tests ├── Web └── Test │ └── DeployTest.php └── bootstrap.php /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | # aliased to a recent 5.3.x version 4 | - 5.3 5 | # aliased to a recent 5.4.x version 6 | - 5.4 7 | 8 | env: 9 | - DB=mysql 10 | 11 | before_script: 12 | - curl -s http://getcomposer.org/installer | php 13 | - php composer.phar install 14 | - if [[ "$DB" == "mysql" ]]; then mysql -e "create database IF NOT EXISTS test;" -uroot; fi 15 | 16 | # omitting "script:" will default to phpunit 17 | script: phpunit --configuration phpunit_$DB.xml --coverage-text 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP-Git-Deploy 2 | ====================== 3 | 4 | Using `Post-Receive Hooks` to deploy muliple projects automatically. [![Build Status](https://secure.travis-ci.org/appleboy/PHP-Git-Deploy.png)](http://travis-ci.org/appleboy/PHP-Git-Deploy) 5 | 6 | Ref: https://developer.github.com/webhooks/ 7 | 8 | Requirements 9 | ====================== 10 | 11 | PHP-Git-Deploy works with PHP 5.3 or later. 12 | 13 | Installation via Composer 14 | ====================== 15 | 16 | Create a composer.json file in your project root and use it to define simply your dependencies: 17 | 18 | ``` 19 | { 20 | "require": { 21 | "appleboy/php-git-deploy": "1.0.*" 22 | } 23 | } 24 | ``` 25 | 26 | Then install Composer in your project (or download the composer.phar directly): 27 | 28 | $ curl -s http://getcomposer.org/installer | php 29 | 30 | And finally ask Composer to install the dependencies: 31 | 32 | $ php composer.phar install 33 | 34 | Installation/Usage 35 | ====================== 36 | 37 | Download files 38 | ---------------------- 39 | 40 | Download and drag the following files into your `application/` folder. 41 | 42 | $ cp -r src/Web your_www/ 43 | 44 | Configure your profile 45 | ---------------------- 46 | 47 | open `src/Web/config.php` file with your editor. Following is config formats. 48 | 49 | ```php 50 | $config['github'] = array( 51 | 'project_name' => array( 52 | 'branch_name' => array('base_path' => 'folder_path') 53 | ) 54 | ); 55 | ``` 56 | 57 | `project_name` must be the same with your github project name, for example: 58 | 59 | Your github project URL is https://github.com/appleboy/PHP-Git-Deploy 60 | 61 | The `project_name` value is `PHP-Git-Deploy`, don't case sensitive. 62 | 63 | Please refer the following example profiles. 64 | 65 | ##### Single project, Multi branch profile 66 | 67 | ```php 68 | array( 69 | 'php-git-deploy' => array( 70 | 'master' => array('base_path' => '/path/PHP-Git-Deploy_1/'), 71 | 'develop' => array('base_path' => '/path/PHP-Git-Deploy_2/') 72 | ) 73 | ); 74 | ``` 75 | 76 | ##### Multi project, Multi branch profile 77 | 78 | ```php 79 | array( 80 | 'php-git-deploy' => array( 81 | 'master' => array('base_path' => '/path/PHP-Git-Deploy_1/'), 82 | 'develop' => array('base_path' => '/path/PHP-Git-Deploy_2/') 83 | ), 84 | 'codeigniter-my-model' => array( 85 | 'master' => array('base_path' => '/path/CodeIgniter-MY-Model_1/'), 86 | 'develop' => array('base_path' => '/path/CodeIgniter-MY-Model_2/') 87 | ) 88 | ); 89 | ``` 90 | 91 | Create New index.php 92 | ---------------------- 93 | 94 | Create new file `your_www/Web/index.php`, copy the following source code and paste into index.php file. 95 | 96 | ```php 97 | index(); 101 | ``` 102 | 103 | Setting Webhook URL 104 | ---------------------- 105 | 106 | Please refer the [Post-Receive Hooks Helper](https://help.github.com/articles/post-receive-hooks) page 107 | 108 | ![Webhook](http://farm8.staticflickr.com/7115/7836097364_d7629b427c_z.jpg "Webhook") 109 | 110 | Author 111 | ====================== 112 | 113 | Bo-Yi Wu (appleboy) 114 | -------------------------------------------------------------------------------- /codeigniter/README.md: -------------------------------------------------------------------------------- 1 | CodeIgniter-Git-Deploy 2 | ====================== 3 | 4 | Using `Post-Receive Hooks` to deploy muliple projects with CodeIgniter automatically. 5 | 6 | Ref: https://help.github.com/articles/post-receive-hooks 7 | 8 | Installation/Usage 9 | ====================== 10 | 11 | Download files 12 | ---------------------- 13 | 14 | Download and drag the following files into your `application/` folder. 15 | 16 | $ cp config/github.php application/config/ 17 | $ cp controllers/deploy.php application/controllers/ 18 | 19 | Configure your profile 20 | ---------------------- 21 | 22 | open `application/config/github.php` file with your editor. Following is config formats. 23 | 24 | ```php 25 | $config['github'] = array( 26 | 'project_name' => array( 27 | 'branch_name' => array('base_path' => 'folder_path') 28 | ) 29 | ); 30 | ``` 31 | 32 | `project_name` must be the same with your github project name, for example: 33 | 34 | Your github project URL is https://github.com/appleboy/PHP-Git-Deploy 35 | 36 | The `project_name` value is `PHP-Git-Deploy`, don't case sensitive. 37 | 38 | Please refer the following example profiles. 39 | 40 | ##### Single project, Multi branch profile 41 | 42 | ```php 43 | array( 44 | 'php-git-deploy' => array( 45 | 'master' => array('base_path' => '/path/PHP-Git-Deploy_1/'), 46 | 'develop' => array('base_path' => '/path/PHP-Git-Deploy_2/') 47 | ) 48 | ); 49 | ``` 50 | 51 | ##### Multi project, Multi branch profile 52 | 53 | ```php 54 | array( 55 | 'php-git-deploy' => array( 56 | 'master' => array('base_path' => '/path/PHP-Git-Deploy_1/'), 57 | 'develop' => array('base_path' => '/path/PHP-Git-Deploy_2/') 58 | ), 59 | 'codeigniter-my-model' => array( 60 | 'master' => array('base_path' => '/path/CodeIgniter-MY-Model_1/'), 61 | 'develop' => array('base_path' => '/path/CodeIgniter-MY-Model_2/') 62 | ) 63 | ); 64 | ``` 65 | 66 | Setting Webhook URL 67 | ---------------------- 68 | 69 | Please refer the [Post-Receive Hooks Helper](https://help.github.com/articles/post-receive-hooks) page 70 | 71 | ![Webhook](http://farm8.staticflickr.com/7115/7836097364_d7629b427c_z.jpg "Webhook") 72 | 73 | Author 74 | ====================== 75 | 76 | Bo-Yi Wu (appleboy) 77 | -------------------------------------------------------------------------------- /codeigniter/config/github.php: -------------------------------------------------------------------------------- 1 | array( 20 | * 'branch_name' => array('base_path' => 'folder_path') 21 | * ) 22 | * ); 23 | * 24 | * If your github project url path: 25 | * https://github.com/appleboy/CodeIgniter-Git-Deploy 26 | * 27 | * For example: Single project, multi branch 28 | * array( 29 | * 'codeigniter-git-deploy' => array( 30 | * 'master' => array('base_path' => '/path/CodeIgniter-Git-Deploy_1/'), 31 | * 'develop' => array('base_path' => '/path/CodeIgniter-Git-Deploy_2/') 32 | * ) 33 | * ); 34 | * 35 | * For example: Multi project, multi branch 36 | * array( 37 | * 'codeigniter-git-deploy' => array( 38 | * 'master' => array('base_path' => '/path/CodeIgniter-Git-Deploy_1/'), 39 | * 'develop' => array('base_path' => '/path/CodeIgniter-Git-Deploy_2/') 40 | * ), 41 | * 'codeigniter-my-model' => array( 42 | * 'master' => array('base_path' => '/path/CodeIgniter-MY-Model_1/'), 43 | * 'develop' => array('base_path' => '/path/CodeIgniter-MY-Model_2/') 44 | * ) 45 | * ); 46 | * 47 | */ 48 | 49 | $config['git_path'] = '/usr/local/bin/git'; 50 | $config['github'] = array( 51 | 'php-git-deploy' => array( 52 | 'master' => array('base_path' => '/home/git/test/PHP-Git-Deploy/') 53 | ) 54 | ); 55 | -------------------------------------------------------------------------------- /codeigniter/controllers/deploy.php: -------------------------------------------------------------------------------- 1 | config->load('github'); 22 | } 23 | 24 | /** 25 | * 26 | * Post-Receive Hooks 27 | * 28 | * @author appleboy 29 | */ 30 | public function receive() 31 | { 32 | // receive git payload 33 | $payload = $this->input->get_post('payload'); 34 | // load git command path config 35 | $git_config = $this->config->item('github'); 36 | $git_path = $this->config->item('git_path'); 37 | 38 | if ($payload and is_array($git_config)) { 39 | $payload = json_decode($payload); 40 | 41 | log_message('debug', 'Post-receive hook initial'); 42 | 43 | foreach ($git_config as $key => $val) { 44 | 45 | // check repository name exist in config 46 | $repository_name = strtolower($payload->repository->name); 47 | if ($repository_name != strtolower($key)) { 48 | continue; 49 | } 50 | 51 | foreach ($val as $k => $v) { 52 | // check if match payload ref branch 53 | $head = 'refs/heads/' . $k; 54 | if ($payload->ref != $head) { 55 | continue; 56 | } 57 | 58 | // git reset head and pull origin branch 59 | if (isset($v['base_path']) and !empty($v['base_path'])) { 60 | $base_path = realpath($v['base_path']) . '/'; 61 | 62 | $shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" reset --hard HEAD', 63 | $git_path, $base_path, $base_path); 64 | log_message('debug', '$shell value ' . $shell); 65 | $output = shell_exec(escapeshellcmd($shell)); 66 | 67 | $shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" clean -f', 68 | $git_path, $base_path, $base_path); 69 | log_message('debug', '$shell value ' . $shell); 70 | $output = shell_exec(escapeshellcmd($shell)); 71 | 72 | $shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" pull origin %s', 73 | $git_path, $base_path, $base_path, $k); 74 | log_message('debug', '$shell value ' . $shell); 75 | $output = shell_exec(escapeshellcmd($shell)); 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appleboy/php-git-deploy", 3 | "type": "library", 4 | "description": "Git Deployment with PHP", 5 | "keywords": ["php", "deploy"], 6 | "homepage": "https://github.com/appleboy/PHP-Git-Deploy", 7 | "type": "library", 8 | "license": "BSD", 9 | "authors": [ 10 | { 11 | "name": "Bo-Yi Wu", 12 | "email": "appleboy.tw@gmail.com", 13 | "homepage": "http://blog.wu-boy.com/", 14 | "role": "Developer" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.3.0" 19 | }, 20 | "autoload": { 21 | "psr-0": { "Web": "src/" } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /phpunit_mysql.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | ./tests/ 13 | 14 | 15 | 16 | 17 | 18 | ./src/ 19 | 20 | ./tests/ 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/Web/Deploy.php: -------------------------------------------------------------------------------- 1 | config = $config; 23 | } 24 | 25 | public function set_config($config = array()) 26 | { 27 | $this->config = $config; 28 | } 29 | 30 | public function index() 31 | { 32 | if (!is_array($this->config) or !isset($this->config['github']) 33 | or !isset($this->config['git_path']) or !is_array($this->config['github'])) { 34 | return false; 35 | } 36 | 37 | if (!isset($_POST['payload']) or empty($_POST['payload'])) { 38 | return false; 39 | } 40 | 41 | // check git config 42 | (!isset($git_config)) and $git_config = $this->config['github']; 43 | (!isset($git_path)) and $git_path = $this->config['git_path']; 44 | 45 | $payload = json_decode($_POST['payload']); 46 | 47 | foreach ($git_config as $key => $val) { 48 | // check repository name exist in config 49 | $repository_name = strtolower($payload->repository->name); 50 | if ($repository_name != strtolower($key)) { 51 | continue; 52 | } 53 | 54 | foreach ($val as $k => $v) { 55 | // check if match payload ref branch 56 | $head = 'refs/heads/' . $k; 57 | if ($payload->ref != $head) { 58 | continue; 59 | } 60 | 61 | // git reset head and pull origin branch 62 | if (isset($v['base_path']) and !empty($v['base_path'])) { 63 | $base_path = realpath($v['base_path']) . '/'; 64 | 65 | $shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" reset --hard HEAD', 66 | $git_path, $base_path, $base_path); 67 | $output = shell_exec(escapeshellcmd($shell)); 68 | 69 | $shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" clean -f', 70 | $git_path, $base_path, $base_path); 71 | $output = shell_exec(escapeshellcmd($shell)); 72 | 73 | $shell = sprintf('%s --git-dir="%s.git" --work-tree="%s" pull origin %s', 74 | $git_path, $base_path, $base_path, $k); 75 | $output = shell_exec(escapeshellcmd($shell)); 76 | } 77 | } 78 | } 79 | return true; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Web/config.php: -------------------------------------------------------------------------------- 1 | array( 20 | * 'branch_name' => array('base_path' => 'folder_path') 21 | * ) 22 | * ); 23 | * 24 | * If your github project url path: 25 | * https://github.com/appleboy/CodeIgniter-Git-Deploy 26 | * 27 | * For example: Single project, multi branch 28 | * array( 29 | * 'codeigniter-git-deploy' => array( 30 | * 'master' => array('base_path' => '/path/CodeIgniter-Git-Deploy_1/'), 31 | * 'develop' => array('base_path' => '/path/CodeIgniter-Git-Deploy_2/') 32 | * ) 33 | * ); 34 | * 35 | * For example: Multi project, multi branch 36 | * array( 37 | * 'codeigniter-git-deploy' => array( 38 | * 'master' => array('base_path' => '/path/CodeIgniter-Git-Deploy_1/'), 39 | * 'develop' => array('base_path' => '/path/CodeIgniter-Git-Deploy_2/') 40 | * ), 41 | * 'codeigniter-my-model' => array( 42 | * 'master' => array('base_path' => '/path/CodeIgniter-MY-Model_1/'), 43 | * 'develop' => array('base_path' => '/path/CodeIgniter-MY-Model_2/') 44 | * ) 45 | * ); 46 | * 47 | */ 48 | 49 | $config['git_path'] = '/usr/local/bin/git'; 50 | $config['github'] = array( 51 | 'php-git-deploy' => array( 52 | 'master' => array('base_path' => '/home/git/test/PHP-Git-Deploy/') 53 | ) 54 | ); 55 | -------------------------------------------------------------------------------- /tests/Web/Test/DeployTest.php: -------------------------------------------------------------------------------- 1 | deploy = new Deploy(); 13 | 14 | $payload = array( 15 | 'repository' => array('name' => 'test'), 16 | 'ref' => 'master' 17 | ); 18 | $_POST['payload'] = json_encode($payload); 19 | } 20 | 21 | public function testindex() 22 | { 23 | $this->assertTrue(true, $this->deploy->index()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | add('Web\Test', __DIR__); 14 | --------------------------------------------------------------------------------