techsemicolon / gitdeployer
Git webhook 部署包,用于 Laravel,目前集成 Bitbucket
Requires
- php: >=5.4.0
- nesbot/carbon: ^1.26.3 || ^2.0
- symfony/process: ^3|^4
This package is auto-updated.
Last update: 2024-09-09 20:10:21 UTC
README
Git webhook 部署包,用于 Laravel,目前集成 Bitbucket。
介绍
当你将新版本或热修复推送到生产分支时,每次都需要登录到实时服务器手动执行 git pull,这很麻烦。这就是 webhook 成为关键部分的原因。目前有许多工具可以提供 CI/CD 选项和自动化的 Git 部署。然而,我认为并非所有项目都需要这种程度的紧急性和复杂性。我有一个非常简单的应用程序,只运行在一个服务器上。我正在寻找一种非常即插即用的 webhook 包用于 Laravel,我没有找到符合我预期的包,因此我创建了它。
工作原理
目前它仅与 Bitbucket 集成,而不是 GitHub(很快也会添加)。一旦安装了此包,它就会提供一个 URL www.your-url.com/git/webhook
,您可以将该 URL 添加到 Bitbucket 的 webhook 配置中。然后,每当 webhook 触发时,此 URL 就会通过 POST 请求从您的服务器接收 webhook 负载。
服务器接收到负载后,会进行以下检查
- 验证请求 IP
在执行任何 webhook 操作之前,我们需要确保请求负载确实来自 Bitbucket,而不是由任何其他未知的服务器错误发送。该包虚拟上为 Bitbucket 的 IP 地址添加了白名单,以便仅处理来自 Bitbucket 的有效负载。
- 检查仓库
如果 Bitbucket webhook 负载中的仓库是 Laravel 项目当前活跃的仓库,则只进行下一步操作。否则不进行任何操作。
- 检查当前活跃分支是否已更新
如果您在实时实例上具有活跃的 production
分支,而负载中有一些 hotfix
分支的更改,我们不需要在实时服务器上运行 webhook 脚本。该包仅在负载包含当前活跃分支的新更改时才采取进一步行动。
- 执行 webhook 操作
如果所有上述检查都通过,则以脚本的形式依次执行 webhook 操作。该包允许您在 webhook 触发时完全控制要运行的脚本和命令。
先决条件
-
实时服务器上设置的 Bitbucket 仓库必须使用
ssh
,这样在您执行 git pull 时就不需要输入密码。 -
运行作为 web 服务器用户和组以及 Laravel 文件的文件权限应相同。例如,如果您有
www-data
运行作为 web 服务器,而 Laravel 文件具有权限ubuntu.ubuntu
,则可能会创建权限问题。
安装
使用 composer 安装此包
composer require techsemicolon/gitdeployer
安装后,您可以在 config/app.php
文件中添加服务提供者以用于 Laravel 版本 <= 5.2。对于更高版本,服务提供者将自动包含。
Techsemicolon\Gitdeployer\GitdeployerServiceProvider::class,
现在,您可以发布包的供应商文件
php artisan vendor:publish --provider="Techsemicolon\Gitdeployer\GitdeployerServiceProvider"
这将添加一个配置文件 git.php
和一个名为 webhookscripts
的脚本文件夹到应用根目录。
重要的是,您需要添加指向/git/webhook
的URL,该URL路由可以避免CSRF验证,因此bitbucket的POST请求不会被拒绝。为此,您需要在App\Http\Middleware\VerifyCsrfToken.php
文件中添加以下内容:
/** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = [ '/git/webhook' ];
工作流程
除了在如何工作
部分中提到的4个步骤之外,该包遵循一个简化的工作流程。webhookscripts
文件夹是该包的关键。
当您运行vendor publish时,它会自动为您创建一个名为deploy.sh
的文件。这包含主要的部署bash脚本。每当webhook被触发时,这个bash脚本就会被运行。
另外,可选地,该包还允许您指定两个额外的脚本文件,您可以使用自己喜欢的名称,这些文件将在主deploy.sh
bash脚本之前和之后运行。这是为了让您控制在拉取最新版本之前和之后需要做什么。
您可以将这些文件存储在webhookscripts
文件夹中,使用您想要的名称。您可以在config/git.php
文件中指定这个名称。更多配置选项如下。
配置
config/git.php
文件具有以下配置:
<?php return [ // Specify full absolute path of git dir // This will help if your .git folder is not inside the working directory // The value of this will be passed like `git pull --git-dir=/your/path/repo.git` // This is optional 'dir' => env('GIT_DIR', null), // This the name of script to run before running main deployment script // The script files should be inside webhookscripts folder // This is optional 'before_script' => env('GIT_BEFORE_SCRIPT', null), // This the name of script to run after running main deployment script // The script files should be inside webhookscripts folder // This is optional 'after_script' => env('GIT_AFTER_SCRIPT', null), // These are the official bitbucket IP addresses // which needs to be whitelisted // This is required 'bitbucket_ips' => [ '18.205.93.0/25', '18.234.32.128/25', '13.52.5.0/25' ], ];
获取部署输出的事件
了解脚本运行时发生了什么,服务器控制台输出了什么内容非常重要。该包会发出您可以监听的事件。这些事件包含时间、控制台输出和错误。
成功事件
<?php namespace Techsemicolon\Gitdeployer\Events; class GitWebhookWasDeployed { public $time; public $output; /** * Create a new event instance. * * @param integer $time * @param string $output * * @return void */ public function __construct($time, $output) { $this->time = $time; $this->output = $output; } }
失败事件
<?php namespace Techsemicolon\Gitdeployer\Events; class GitWebhookDeployFailed { public $error; /** * Create a new event instance. * * @param string $error * * @return void */ public function __construct($error) { $this->error = $error; } }
主部署脚本
以下是默认的deploy.sh
脚本。您可以根据需要对其进行修改。
#!/bin/sh gitdir=${1:-''} # activate maintenance mode php artisan down # update source code git pull $gitdir # update PHP dependencies export COMPOSER_HOME='/tmp/composer' composer install --no-interaction --no-dev --prefer-dist # --no-interaction Do not ask any interactive question # --no-dev Disables installation of require-dev packages. # --prefer-dist Forces installation from package dist even for dev versions. # clear cache php artisan cache:clear # clear config cache php artisan config:clear # cache config php artisan config:cache # restart queues php artisan -v queue:restart # update database php artisan migrate --force # --force Required to run when in production. # stop maintenance mode php artisan up
注意:bash变量gitdir
是必需的,以确保如果它在配置文件中指定,它将使用git-dir配置。
注意事项
-
该包使用
symfony/process
运行bash脚本。因此,请确保webhookscripts
目录中的bash脚本具有所需的权限,以便使它们可执行。 -
当您设置
ssh-key
并将其添加到github时,请确保ssh密钥是为运行web服务器的正确用户生成的,并且该用户也有权限访问laravel文件。 -
一旦ssh密钥保存到bitbucket,请运行
ssh -T git@bitbucket.com
。这是为了将bitbucket添加到您的known hosts中。如果不执行此操作,脚本将抛出错误。
许可证
此包是开源软件,采用MIT许可证。