tmd/auto-git-pull

在提交被推送时自动拉取git仓库。

2.1.2 2015-12-11 23:26 UTC

This package is auto-updated.

Last update: 2024-09-28 10:00:26 UTC


README

当Git仓库被推送更改时自动拉取。(实际上它执行了一个git fetch,然后是git reset,但这不够吸引人。)

关于

有两个重要部分

  • 一个PHP脚本,当你推送时,Bitbucket或GitHub会自动向其发送请求。(例如下面的http://mysite/deploy.php
  • 一个执行实际拉取的shell脚本。(scripts/git-pull.sh

分离的原因是为了你不需要授予网络用户对文件的写权限。你只需要允许它以具有写权限的用户身份运行一个脚本。

设置

  • 使用composer require tmd/auto-git-pull安装最新版本

  • 使拉取脚本可执行(如果你更新了包,也需要这样做)

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

你可以通过将以下内容添加到你的composer.json来实现自动执行:

"scripts": {
    "post-install-cmd": [
        "chmod +x vendor/tmd/auto-git-pull/scripts/git-pull.sh"
    ]
}
  • 在你的网站上创建一个公开可访问的URL,该URL将被GitHub/Bitbucket调用以执行部署(例如http://mysite.com/deploy.php),并设置适当的参数。

示例显示了所有可能的选项及其默认值:唯一必需的选项是directory

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();

Laravel中的示例,展示了最小选项

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

带日志的示例

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();
  • 在Bitbucket/GitHub上添加钩子以运行脚本

Add bitbucket deploy hook

如果网络服务器用户没有对目录的写权限

如果你的网络服务器以不同于文件所有者的用户身份运行(这是最佳实践),则需要允许网络服务器执行拉取。

  • 允许网络服务器用户以具有写权限的用户身份运行拉取脚本
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
  • 在参数中设置运行拉取的用户
$deployer = new \Tmd\AutoGitPull\Deployer(array(
    'deployUser' => 'anthony',
    // ...
));

如果你的仓库是私有的

你需要设置一个部署密钥,以便在不输入密码的情况下执行拉取。

  • 使用ssh-keygen为将运行拉取脚本的用户(例如www-data)生成ssh密钥。

  • 按照以下说明将部署密钥添加到git仓库

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

  • 如果需要,将你的git远程URL从HTTPS更改为SSH
cd /var/www/mysite
git remote -v

如果你的输出看起来像这样,你正在使用HTTPS

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

将其更改为使用ssh,如下所示

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