0%

github webhook

webhook

使用 github webhook 实现网站自动部署,服务器端部署 webhook 客户端后,git push 操作触发服务器自动执行 git pull 操作,实现自动部署。

客户端

github 上有一个 go 语言开发的客户端,部署非常方便:
https://github.com/adnanh/webhook

webhook.json

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
[
{
"id": "webhook",
"execute-command": "/PATH-TO-BINARY/webhook.sh",
"command-working-directory": "/GIT-REPOSITORY-DIR",
"pass-arguments-to-command":
[
{
"source": "payload",
"name": "head_commit.id"
},
{
"source": "payload",
"name": "pusher.name"
},
{
"source": "payload",
"name": "pusher.email"
}
],
"trigger-rule":
{
"and":
[
{
"match":
{
"type": "payload-hash-sha1",
"secret": "YOUR SECRET",
"parameter":
{
"source": "header",
"name": "X-Hub-Signature"
}
}
},
{
"match":
{
"type": "value",
"value": "refs/heads/master",
"parameter":
{
"source": "payload",
"name": "ref"
}
}
}
]
}
}
]

webhook.sh

1
2
3
4
5
#!/bin/bash
WEB_PATH='/GIT-REPOSITORY-DIR'
cd $WEB_PATH
git fetch --all
git reset --hard origin/master

systemd service

1
2
3
4
5
6
7
8
[Unit]
Description=Webhooks

[Service]
ExecStart=/PATH-TO-BINARY/webhook -hooks /PATH-TO-BINARY/webhook.json -hotreload -port PORT

[Install]
WantedBy=multi-user.target

Payload URL

github 后台配置 Payload URL
http://SERVER-IP:PORT/hooks/WEBHOOK-ID