dtforce/slim-hook

此包已弃用且不再维护。未建议替代包。

基于 Slim 框架的 Gitlab webhook

v1.0.0 2016-08-24 16:35 UTC

This package is not auto-updated.

Last update: 2022-03-30 16:06:37 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

PHP 中的 Gitlab webhook

这是一个非常简单的 Gitlab webhook,允许在 BUILD、PUSH 和 TAG 事件发生时启动 bash 脚本。

安装

使用 composer 安装

composer create-project dtforce/slim-hook

或者通过克隆此仓库。

配置

config 文件夹中创建一个名为 local.yaml 的文件,包含如下内容

settings:
  secret: 3219874514564 - this should match your webhook secret token
scripts:
  gitlab-org/gitlab-test: - name of the project
    deploy: - what event do we react to
      staging: bash /path/to/app/test.bash deploy - on deploy, what enviroment do we consider
    push:
      refs/heads/master: - on push, what branch do we consider
        cwd: /path/to/app - optional you can set working directory
        - bash /path/to/app/test.bash push - this is going to be executed throug shell_exec
    tag:
        - bash /path/to/app - on tag no subcategories
        - bash do-smothin-else - you can execute multiple commands with one hook
  gitlab-org/gitlab-something-else: - more projects

变量

当您的脚本正在执行时,环境中有一些变量被提供。

以下是一些示例,应该相当清楚

部署

[
    'HOOK_PROJECT_PATH' => 'gitlab-org/gitlab-test',
    'HOOK_BUILD_ID' => 379,
    'HOOK_BUILD_REF' => 'bcbb5ec396a2c0f828686f14fac9b80b780504f2',
    'HOOK_ENV_NAME' => 'staging'
]

PUSH

[
    'HOOK_PROJECT_PATH' => 'gitlab-org/gitlab-test',
    'HOOK_REF' => 'refs/heads/master',
    'HOOK_BRANCH' => 'master',
    'HOOK_BUILD_REF' => 'da1560886d4f094c3e6c9ef40349f7d38b5d27d7'
]

Tag

[
    'HOOK_PROJECT_PATH' => 'jsmith/example',
    'HOOK_REF' => 'refs/tags/v1.0.0',
    'HOOK_TAG' => 'v1.0.0',
    'HOOK_BUILD_REF' => '82b3d5ae55f7080f1e6022629cdb57bfae7cccc7'
]

启动

您可以通过常规方式配置 Apache,或者可以使用 PHP 内置服务器启动,如下所示

/usr/bin/php7.0 -S localhost:8080 -t /path/to/app/slim-hook/public

BashREST

为了方便,此应用还可以处理简单的 REST 请求。当您在 Gitlab CI 中部署时,这可能很有用,因为您需要获取目标平台上执行脚本的输出。通过以下形式的请求发送到该服务器

POST 到 /groupName/projectName/action

您将启动配置中描述的脚本

bashREST:
  groupName/projectName:
    action: launch some bash command here
    action2:
      cwd: dir
      0: test1
      1: test2

如果您在 POST 主体中发送了一些数据(以 JSON 格式),则脚本将以扁平化的形式在其环境变量中接收它们。

示例

{
    "test" : "asd",
    "nested" : {
        "a" : "b",
        "asd" : "c"
    },
    "array" : ["asd", "zxc", "xcvxcv"]
}

通过 POST 发送到 /bash-rest/test-app/my-action

将设置以下环境变量

HOOK_PROJECT_PATH=bash-rest/test-app
HOOK_ACTION=my-action
HOOK_test=asd
HOOK_nested_a=b
HOOK_nested_asc=c
HOOK_array_0=asd
HOOK_array_1=zxc
HOOK_array_2=xcvxcv

如果您在配置脚本中设置了密钥,则请求 BashREST 服务器需要使用此密钥进行授权。密钥存储在头部字段 X-Secret 中。注意它与 Gitlab Webhooks 中使用的密钥不同。

BashREST 调用示例

假设配置

bashREST:
  groupName/projectName:
    deploy: echo Application $HOOK_PROJECT_PATH action $HOOK_ACTION called with ENV set to $HOOK_ENV

假设 shell_exec 启动 shbash,则调用动作如下

curl -X POST https://:4000/groupName/projectName/deploy -H \
    "X-Secret: $BASH_REST_SECRET" -H 'Content-Type: application/json' -d '{"ENV":"production"}'

响应将以 application/text 返回,包含执行命令的输出结果(写入 stdout

Application groupName/projectName action deploy called with ENV set to production

目的是在 HTTP 协议上实现类似 RPC 的 bash

这就是全部内容

希望您喜欢!