定制 amaxlab/git-web-hook 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amaxlab/git-web-hook

Composer 安装命令:

composer require amaxlab/git-web-hook

包简介

Library for git web hook handle (supported and tested gitlab.com and github.com)

README 文档

README

Build Status Scrutinizer Code Quality

Library for handle git web hooks from gitlab.com or github.com and run commands

Features

  • Run commands by global get request
  • Run commands by git repository push
  • Run commands by some branch push
  • Security check commit author (global, repository, branch)
  • Security check param from $_GET request
  • Send email author and mail recipients with the results of the execute command

Require

  • php >= 5.3
  • symfony/options-resolver ^2.3
  • symfony/yaml ^2.3
  • psr/log >= ~1.0

Install

$ php composer require amaxlab/git-web-hook "~1.0"

or project

$ php composer create-project amaxlab/git-web-hook-composer-install ./git-web-hook --prefer-dist

Usage

Old way:

Specify config in php file directly:

<?php

include __DIR__.'/../vendor/autoload.php';

use AmaxLab\GitWebHook\Hook;

$options = array(
    'sendEmails'          => true,
    'sendEmailAuthor'     => true,
    'mailRecipients'      => array(),
    'allowedAuthors'      => '*',
    'allowedHosts'        => '*',
);

$hook = new Hook(__DIR__, $options);
$hook
	->addRepository('git@github.com:amaxlab/git-web-hook.git', '/var/www/my_project_folder/web', array(/*command executed on each push to repository*/))
		->addBranch('master', array('git status', 'git reset --hard HEAD', 'git pull origin master'), '/var/www/my_project_folder/demo_subdomain',  array(/* array of redefined options*/)) // commands executed on push to specified branch in /var/www/html/my_site/ folder
 		->addBranch('production', 'git pull origin production');

$hook->execute();

You can also specify some commands to execute them on hook call:

$hook->addCommand($someCommand);

Preferred way:

Load config from yaml file

<?php

include __DIR__.'/../vendor/autoload.php';

use AmaxLab\GitWebHook\Hook;

$hook = new Hook(__DIR__);
$hook->loadConfig('/var/www/ghw/config.yml');
$hook->execute();

Configuration

Configuration can be unique for each branch, it is enough to pass the variable options of type array. You can pass them directly while creating hook or load through main config.yml file. You should use yaml files, due to configuration through php will be removed in future releases. See examples below.

$options = array(
    'sendEmails'            => false,                          // Enable or disable sending emails
    'sendEmailAuthor'       => false,                          // Enable or disable sending email commit author
    'sendEmailFrom'         => 'git-web-hook@'.gethostname(),  // Email address from which messages are sent
    'mailRecipients'        => array(),                        // Array of subscribers
    'allowedAuthors'        => array(),                        // Array of commit authors allowed to execute commands
    'allowedHosts'          => array(),                        // Array of hook hosts allowed to execute commands
    'securityCode'          => '',                             // Security code on check $_GET request
    'securityCodeFieldName' => 'code',                         // $_GET field name of security code
    'repositoryFieldName'   => 'url',                          // Repository filed name on the JSON query
);
    #/var/www/ghw/config.yml
    options:
        sendEmails: false,                       # Enable or disable sending emails
        sendEmailAuthor: false,                  # Enable or disable sending email commit author
        sendEmailFrom: 'git-web-hook@youdomain', # Email address from which messages are sent
        mailRecipients: [],                      # Array of subscribers
        allowedAuthors: [],                      # Array of commit authors allowed to execute commands
        allowedHosts: [],                        # Array of hook hosts allowed to execute commands
        securityCode: '',                        # Security code on check $_GET request
        securityCodeFieldName: 'code',           # $_GET field name of security code
        repositoryFieldName: 'url',              # Repository filed name on the JSON query
    commands: [] #commands to execute on each hook call
    path: '/var/www/projects' #main path where commands will be executed, can be overwrite in repository or branch
    #trustedProxies: [192.168.0.100] # if your projects lives behind proxy you should specify it ip, to correctly determine real ip address
    repositoriesDir: /var/www/ghw/repos.d/ #directory to load additional yaml files with repository configuraton
    #repositories: # you can specify some repository directly in main config file
    #    git@github.com:amaxlab/git-web-hook-test.git:
    #        path: null
    #        options: {}
    #        commands: 
    #          - git status
    #        branch:
    #            master:
    #                path: null
    #                options: {}
    #                commands: 
    #                  - git reset --hard HEAD
    #                  - git pull origin master
    #            production:
    #                commands: 
    #                  - git reset --hard HEAD
    #                  - git pull origin production

Logging

Use loggers PSR-3 standard (Monolog)

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

...

$logger = new Logger('git-web-hook');
$logger->pushHandler(new StreamHandler(__DIR__ . '/hook.log', Logger::WARNING));

...

$hook = new Hook(__DIR__, $options, $logger);

Load repository configuration

If you have a lot of repositories you can place them in separate *.yml files and load all configuration from a directory:

<?php

$hook = new Hook(__DIR__, $options);
$hook->loadRepos('/path/to/derectory/'); // or $hook->loadConfig('/path/to/file); if you specify `repositoriesDir` in main config.yml
$hook->execute();

Example of partial configuration file:

repositories:
    #one or several repositories can be described in a file
    git@github.com:amaxlab/git-web-hook-test.git:
        path: null
        options: {}
        commands: 
          - git status
        branch:
            master:
                path: null
                options: {}
                commands: 
                  - git reset --hard HEAD
                  - git pull origin master
            production:
                commands: 
                  - git reset --hard HEAD
                  - git pull origin production

Security code checking configuration

Security code can be cofigured only on the root options.

Setup config:

$options = array(
    ...
    'securityCode'          => 'GjnfkrjdsqKfvgjcjc',
    'securityCodeFieldName' => 'mySecurityCode',
    ...
);
options:
    securityCode: 'GjnfkrjdsqKfvgjcjc',
    securityCodeFieldName: 'mySecurityCode',
);

and setup web hook on gitlab.com or github.com on

http://yourhost/hook.php?mySecurityCode=GjnfkrjdsqKfvgjcjc

if security code not pass check the you see

Jan 01 00:00:00 WARN Security code not match

in the log file

TODO

  • Remove old way of configuration (through php file)
  • Resolve passing config while loading config and remove unnessesary dependencies between classes hook/repository/branch/command
  • Add more tests
  • Refactor logging

License

This library is under the MIT license. See the complete license in here

amaxlab/git-web-hook 适用场景与选型建议

amaxlab/git-web-hook 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「git」 「web」 「HOOK」 「web-hook」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 amaxlab/git-web-hook 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 amaxlab/git-web-hook 我们能提供哪些服务?
定制开发 / 二次开发

基于 amaxlab/git-web-hook 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 58
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 25
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-18