DevOps WebHook汇总 (Gitlab,Jenkins,K8S Event)
# jenkins pipeline
这里我写了一个服务做中转,只记录一下jenkins pipeline如何发起HTTP请求,具体格式需自定义。
发起HTTP请求:
def send() {
//return
def body = """
{
"buildStatus": "${env.BUILD_STATUS}",
"jobName": "${env.appName}",
"buildUser": "${env.BUILD_USER}",
"gitBranch": "${env.GIT_BRANCH}",
"gitCommit": "${env.GIT_COMMIT}",
"gitCommitUser": "${env.GIT_COMMITER}",
"gitCommitMessage": "${env.GIT_MESSAGE}",
"jobUrl": "${env.BUILD_URL}"
}
"""
httpRequest(customHeaders: [[name: 'Content-Type', value: 'application/json;charset=UTF-8']],
httpMode: 'POST',
requestBody: body,
url: "http://feishu-adapter:8080/jenkins/push")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# gitlab push
系统设置,系统钩子设置 admin/hooks
Push Events Gitlab的请求如下:
{
"object_kind": "push",
"event_name": "push",
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"ref": "refs/heads/master",
"checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "Hello World",
"user_id": 4,
"user_name": "John Smith",
"user_email": "[email protected]",
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
"project_id": 15,
"project": {
"id": 15,
"name": "gitlab",
"description": "",
"web_url": "http://test.example.com/gitlab/gitlab",
"avatar_url": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
"git_ssh_url": "[email protected]:gitlab/gitlab.git",
"git_http_url": "http://test.example.com/gitlab/gitlab.git",
"namespace": "gitlab",
"visibility_level": 0,
"path_with_namespace": "gitlab/gitlab",
"default_branch": "master"
},
"commits": [
{
"id": "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
"message": "Add simple search to projects in public area\n\ncommit message body",
"title": "Add simple search to projects in public area",
"timestamp": "2013-05-13T18:18:08+00:00",
"url": "https://test.example.com/gitlab/gitlab/-/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428",
"author": {
"name": "Test User",
"email": "[email protected]"
}
}
],
"total_commits_count": 1,
"push_options": {
"ci": {
"skip": true
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
发到中转适配程序上转格,在适配过程中我关注到的是:
- 项目名:.project.name
- 分支:.ref
- 用户名:.user_name
- 提交数量:.commits.size
- 最后提交hash:.commits[commits.size-1].id
- 提交信息:.commits[n].message
# k8s evnet
https://github.com/AliyunContainerService/kube-eventer
我过滤了pod的killing和started事件,下面是推到飞书的配置:
# 配置config map
apiVersion: v1
data:
content: '{ "msg_type": "interactive", "card": { "config": { "wide_screen_mode":
true, "enable_forward": true }, "header": { "title": { "tag": "plain_text", "content":
"{{ .Message }}" }, "template": "Blue" }, "elements": [ { "tag": "div", "text":
{ "tag": "lark_md", "content": "**EventType:** {{ .Type }}\n**EventKind:** {{
.InvolvedObject.Kind }}\n**EventReason:** {{ .Reason }}\n**EventTime:** {{ .LastTimestamp
}}\n**EventMessage:** {{ .Message }}" } } ] } }'
kind: ConfigMap
metadata:
name: custom-webhook
namespace: kube-system
resourceVersion: "13695490"
selfLink: /api/v1/namespaces/kube-system/configmaps/custom-webhook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 部署kube-eventer
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s.kuboard.cn/name: kube-eventer
name: kube-eventer
name: kube-eventer
namespace: kube-system
spec:
selector:
matchLabels:
app: kube-eventer
template:
labels:
app: kube-eventer
spec:
containers:
- command:
- /kube-eventer
- '--source=kubernetes:https://kubernetes.default'
- >-
--sink=webhook:https://open.feishu.cn/open-apis/bot/v2/hook/******************?level=Normal&kinds=Pod&reason=Killing,Started&method=POST&header=Content-Type=application/json&custom_body_configmap=custom-webhook&custom_body_configmap_namespace=kube-system
env:
- name: TZ
value: Asia/Shanghai
image: 'registry.aliyuncs.com/acs/kube-eventer-amd64:v1.2.0-484d9cd-aliyun'
imagePullPolicy: IfNotPresent
name: kube-eventer
resources:
limits:
cpu: 500m
memory: 250Mi
requests:
cpu: 100m
memory: 100Mi
volumeMounts:
- mountPath: /etc/localtime
name: localtime
readOnly: true
- mountPath: /usr/share/zoneinfo
name: zoneinfo
readOnly: true
volumes:
- hostPath:
path: /etc/localtime
type: ''
name: localtime
- hostPath:
path: /usr/share/zoneinfo
type: ''
name: zoneinfo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
上次更新: 10/23/2024