gitlab webhook介绍及与openshift项目关联

原文出处:https://docs.gitlab.com/ee/user/project/integrations/webhooks.html

当一个项目的代码有更新或者有新的问题被创建,项目的webhook允许你触发一个url。

你可以配置webhook去监听特定事件例如提交,问题或者合并。gitlab会像webhook url发送一个post请求。

多数情况下,你需要配置webhook receiver去处理从gitlab发来的信息,然后根据你的需要,将其转发给另一个应用。

Webhooks 是‘用户定义的 http回调’。他们通常被一些事务触发,例如推送补丁或者追加评论等。当这个事件发生的时候,gitlab就会webhook处配置的url发送http post请求。url接受到请求后,可以做任何需要的处理,例如触发持续集成系统并发报告给问题跟踪系统。

社区版的gitlab每个project都可以配置webhook,配置的入口在每个项目的Settings -> Integrations.

如果你自己写接受webhook的endpoint,请注意:

1. 这endpoint需要尽可能快的返回response,否则gitlab长时间等太久,会认为这个hook失败了而会重新尝试。

2.这endpoint必须返回有意义的http请求,否则gitlab会认为hook失败而重新尝试。

3.gitlab不关心endpoint返回的response中的返回码

如果你配置一个secret token, 他将会出现在hook request的http header的 X-Gitlab-Token。endpoint会校验这个请求是合法的。

默认的,如果gitlab中‘enable ssl certificate’使能,自签证书无法验证通过,可以根据情况关闭该选项。

其他一些push events的配置比较简单,这里不再翻译。

你可以手动触发这个webhook已进行测试。还在webhook这个配置界面,for examole -> for triggering 等

 

openshift中关于webhook的解释

https://docs.openshift.com/container-platform/3.9/dev_guide/builds/triggering_builds.html#webhook-triggers

webhook触发器允许你通过给openshift container platform api endpoint发一个请求而触发一个新的build。你可以定义在这个webhook为github, gitlab,bitbucket或者generic webhooks。

openshift container platform webhooks当前允许支持scms的push event,其他的触发事件会被忽略。

当push events被触发,在相应bc中会对事件进行确认。如果事件匹配,则这个满足条件的特定的commit就会触发build。

oc new-app和oc new-build会自动创建github和generic的webhook triggers。但是其他需要的一些webhook必须手动添加(setting triggers)。

对于所有的webhooks, 你可以定义一个含有webhooksecretkey的secret,这个值的value就是配置webhook url的secret值。这个secret值对于webhook url来说是必须的,因为它保证了这个url的唯一性,已防止其他buid会被触发。

我们这里主要做gitlab的webhooks的配置,所以将gitlab相关的步骤介绍如下:

# 1. create one secret
kind: Secret
apiVersion: v1
metadata:
  name: gitlab-wlin-errata-upshift
data:
  WebHookSecretKey: d2xpbi1lcnJhdGEtdXBzaGlmdA==
type: Opaque
# 2. add the trigger to the bc
    - gitlab:
        secretReference:
          name: gitlab-errata-rails-errata-upshift
      type: GitLab
# 3. run 'oc describe bc' to get the Webhooks url, you will check one simple url like: https://paas_host/oapi/v1/namespaces/namespace/buildconfigs/bc/webhooks/errata-rails-errata-upshift/gitlab
# the 'errata-rails-errata-upshift' is my secret
# 4. copy it and add to the Webhooks the gitlab target repo
# 5. test to make sure it works well.