2017 © Pedro Peláez
 

library auto-git-pull

Automatically pulls a git repository when commits are pushed.

image

tmd/auto-git-pull

Automatically pulls a git repository when commits are pushed.

  • Saturday, December 12, 2015
  • by antriver
  • Repository
  • 7 Watchers
  • 56 Stars
  • 1,168 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 14 Forks
  • 4 Open issues
  • 14 Versions
  • 4 % Grown

The README.md

Auto git pull

Automatically pull when changes are pushed to a Git repository. (Actually it does a git fetch followed by git reset but that wasn't as catchy.), (*1)

About

There are two important parts: * A PHP script which Bitbucket or GitHub will automatically send a request to when you push. (http://mysite/deploy.php in the examples below) * A shell script which does the actual pulling. (scripts/git-pull.sh), (*2)

The reason for the separation is so you don't need to grant the web user write permission to your files. You just need to allow it to run the one script as a user that does have write permission., (*3)

Setup

  • Install the latest version with composer require tmd/auto-git-pull, (*4)

  • Make the pull script executable (you need to do this if you update the package as well):, (*5)

chmod +x vendor/tmd/auto-git-pull/scripts/git-pull.sh

You can have this automatically happen by adding this to your composer.json:, (*6)

"scripts": {
    "post-install-cmd": [
        "chmod +x vendor/tmd/auto-git-pull/scripts/git-pull.sh"
    ]
}
  • Create a publicly accessible URL on your site which will be called by GitHub/Bitbucket and run the deployment (e.g. http://mysite.com/deploy.php) and set the parameters as appropriate.

Example showing all the options that can be given and their default values: The only required option is directory, (*7)

use Tmd\AutoGitPull\Deployer;

require 'vendor/autoload.php';

$deployer = new Deployer([
    // IP addresses that are allowed to trigger the pull
    // (CLI is always allowed)
    'allowedIpRanges' => [
        '131.103.20.160/27', // Bitbucket
        '165.254.145.0/26', // Bitbucket
        '104.192.143.0/24', // Bitbucket
        '104.192.143.192/28', // Bitbucket (Dec 2015)
        '104.192.143.208/28', // Bitbucket (Dec 2015)
        '192.30.252.0/22', // GitHub
    ],

    // These are added to the allowedIpRanges array
    // to avoid having to define the Bitbucket/GitHub IPs in your own code
    'additionalAllowedIpRanges' => [
        '192.168.0.2/24'
    ],

    // Git branch to reset to
    'branch' => 'master',

    // User to run the script as
    'deployUser' => 'anthony',

    // Directory of the repository
    'directory' => '/var/www/mysite/',

    // Path to the pull script
    // (You can provide your own script instead)
    'pullScriptPath' => __DIR__ . '/scripts/git-pull.sh',

    // Git remote to fetch from
    'remote' => 'origin'
]);

$deployer->postDeployCallback = function () {
    echo 'Yay!';
};

$deployer->deploy();

Example in Laravel showing minimal options:, (*8)

Route::post('deploy', function()
{
    $deployer = new \Tmd\AutoGitPull\Deployer(array(
        'directory' => '/var/www/mysite/'
    ));
    $deployer->deploy();
});

Example with logging:, (*9)

use Monolog\Logger;
use Monolog\Handler\FingersCrossedHandler;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\StreamHandler;
use Tmd\AutoGitPull\Deployer;

require 'vendor/autoload.php';

$deployer = new Deployer([
    'directory' => '/var/www/mysite/'
]);

$logger = new Logger('deployment');

// Output log messages to screen
$logger->pushHandler(
    new StreamHandler("php://output")
);

// Write all log messages to a log file
$logger->pushHandler(
    new RotatingFileHandler('/var/log/mysite-deploy.log')
);

// Send an email if there's an error
$logger->pushHandler(
    new FingersCrossedHandler(
        new NativeMailerHandler('anthony@example.com', 'Deployment Failed', 'anthony@localhost', Logger::DEBUG),
        new ErrorLevelActivationStrategy(Logger::ERROR)
    )
);

$deployer->setLogger($logger);

$deployer->deploy();
  • Add the hook on Bitbucket/GitHub to run the script:

Add bitbucket deploy hook, (*10)

If the web server user does not have write permissions on the directory

If your webserver runs as a different user than the owner of the files (as is best practise) you need to allow the webserver to do the pull., (*11)

  • Allow the web server user to run the pull script as a user with write permissions:
sudo visudo

# Add the line:
# (Edit users and path as appropriate)
# www-data = User the PHP script runs as
# anthony = User the shell script needs to run as to write to the directory
# /var/www/mysite/vendor/tmd/auto-git-pull/scripts/git-pull.sh = Path to shell script

www-data ALL=(anthony) NOPASSWD: /var/www/mysite/vendor/tmd/auto-git-pull/scripts/git-pull.sh
  • Set the user to run the pull as in the parameters:
$deployer = new \Tmd\AutoGitPull\Deployer(array(
    'deployUser' => 'anthony',
    // ...
));

If your repository is private

You need to setup a deployment key so the pull can happen without a password being entered., (*12)

  • Generate an sshkey using ssh-keygen *for the user that will run the pull script (e.g. www-data)., (*13)

  • Follow these instructions to add a deployment key to the git repository:, (*14)

Bitbucket: https://confluence.atlassian.com/bitbucket/use-deployment-keys-294486051.html GitHub: https://developer.github.com/guides/managing-deploy-keys/#deploy-keys, (*15)

  • Change your git remote url from HTTPS to SSH if necessary:
cd /var/www/mysite
git remote -v

If your output looks like this, you're using HTTPS:, (*16)

origin  https://bitbucket.org/me/mysite.git (fetch)
origin  https://bitbucket.org/me/mysite.git (push)

Change it to use ssh, like this:, (*17)

git remote set-url origin git@bitbucket.org:me/mysite.git

The Versions

12/12 2015

dev-master

9999999-dev

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

The Requires

 

12/12 2015

2.1.2

2.1.2.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

The Requires

 

16/11 2015

2.1.1

2.1.1.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

The Requires

 

27/10 2015

2.0.0.x-dev

2.0.0.9999999-dev

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

The Requires

 

27/10 2015

2.0.0

2.0.0.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

The Requires

 

10/09 2015

1.1.4.x-dev

1.1.4.9999999-dev

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

10/09 2015

1.1.4

1.1.4.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

14/03 2015

1.1.3

1.1.3.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

14/03 2015

1.1.2

1.1.2.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

14/03 2015

1.1.1

1.1.1.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

13/03 2015

1.1.0

1.1.0.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

29/11 2014

1.0.1

1.0.1.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

29/11 2014

1.0.0

1.0.0.0

Automatically pulls a git repository when commits are pushed.

  Sources   Download

MIT

13/03 2014

dev-GitDeployer

dev-GitDeployer

Hook to automatically pull git commits into production.

  Sources   Download

MIT