sevaske/git-webhook-handler

使用 webhook 自动更新您的应用程序的简单方法。

v0.2.1 2022-05-16 23:03 UTC

This package is auto-updated.

Last update: 2024-09-27 15:57:26 UTC


README

使用 webhook 自动更新您的应用程序的简单方法。

此脚本监听 bitbucket webhook 并更新您项目的当前分支。

它做什么?
git fetch
git pull origin {current_branch_name}

安装

composer require sevaske/git-webhook-handler

示例

您可以在文件中创建:{your_project_path}/webhook/index.php

仅更新项目
<?php
require_once __DIR__ . '/../vendor/autoload.php';

$projectRootPath = dirname(__DIR__) . DIRECTORY_SEPARATOR;
$requestContent = file_get_contents("php://input");
$gitAlias = 'git';

$webhook = new \GitWebhookHandler\Webhook\Bitbucket(
    $projectRootPath,
    $requestContent,
    $gitAlias
);

$result = $webhook->handlePull(); // boolean
更新并处理结果
<?php
require_once __DIR__ . '/../vendor/autoload.php';

$projectRootPath = dirname(__DIR__) . DIRECTORY_SEPARATOR;
$requestContent = file_get_contents("php://input");
$gitAlias = 'git';

$webhook = new \GitWebhookHandler\Webhook\Bitbucket(
    $projectRootPath,
    $requestContent,
    $gitAlias
);

// update and get results of executions
$fetchResult = $webhook->git->fetch();
$pullResult = $webhook->git->pull();

// Execution details
$fetchResult->output; // The result of executing the command "git fetch"
$pullResult->output; // The result of executing the command "git pull origin {your_current_branch}"
$noChanges = \GitWebhookHandler\Terminal\Git::catchNoChanges($pullResult); // True if no changes
$pullErrors = \GitWebhookHandler\Terminal\Git::catchErrors($pullResult); // Array of errors

// Errors & the repo details
$webhook->errors; // Array of errors
$webhook->git->branchName; // Current branch of your project
$webhook->requestHandler->authors; // Array of changes authors (full name, email and nickname)
$webhook->requestHandler->request->repository->full_name; // Repository name

// maybe you want to do something else?
$composerUpdateCommand = "export COMPOSER_HOME=/home/sevaske/.composer && cd {$projectRootPath} && composer update";
$composerUpdateResult = \GitWebhookHandler\Terminal\Command::exec($composerUpdateCommand);
$composerUpdateResult->output; // The result of executing the command "composer update"

错误?

如果您有任何与访问仓库相关的错误,您可以运行以下命令

git config --global --add safe.directory {project_path}
sudo git remote set-url origin https://{bitbucket_user}:{auth_pass}@bitbucket.org/{project_name}/{repo_name}.git

此外,脚本必须能够访问您项目的所有文件,否则您可能会遇到错误。一个快速解决方案(但不够安全,请谨慎操作)

sudo chown -R www-data:www-data {project_path}

安全性如何?

webhook 检查分支名称并检查该分支是否存在于仓库中。执行命令是固定的,不包含任何动态内容。