目录

一个使用Go编写的Github webhook接收处理服务

目录

如题,今天突然遇到一个场景,此博客的hexo架子是放在群辉docker里的,一般写完文章会push到VPS的git仓库,再用nginx展示静态页。 今天心血来潮,push到github的仓库里,利用github pages来展示页面,也蛮简单。但vps 就接收不到了呀。

于是乎,在vps用go写了一段服务。利用github仓库的webhook,也就是 push到github后,他可以请求下你指定的url,发送一个push状态。 利用这个特性,vps上起了一个web服务,用于接收github的 webhook请求,并自动pull 静态页到www目录,实现同步更新。

最近在看golang,就用go写了一个服务。参考了https://github.com/moonagic/GoWebhook

废话不多说,直接看代码。

 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
53
54
55
56
57
58
59
60
/**
 * @author seven
 * @version 1.0
*/
package main
import (
	"fmt"
    "net/http"
    "log"
	"crypto/hmac"
	"crypto/sha1"
	"encoding/hex"
	"io/ioutil"
	"os/exec"
)
//处理请求
func wsHandler(w http.ResponseWriter, r *http.Request) {
	var Secret string ="xxxx"
	if (r.Header.Get("x-github-event") == "push" || r.Header.Get("x-github-event") == "ping") {
		bodyContent, _ := ioutil.ReadAll(r.Body)
		r.Body.Close()
		signature := r.Header.Get("X-Hub-Signature")
		if VerifySignature(signature, string(bodyContent), Secret) { //检验 github 发过来的 签名
			//("验证通过,启动部署任务")
			
			//校验通过,执行shell命令,pull最新代码
			cmd := exec.Command("/bin/sh","-c","cd /www && git reset --hard master && git pull") 
			_, err := cmd.Output()
			if err == nil {
				fmt.Fprintln(w, "{\"code\":200, \"description\":\"false\"}")
			} else {
				fmt.Fprintln(w, "{\"code\":200, \"description\":\"OK\"}")
			}
		} else {
			fmt.Fprintln(w, "{\"code\":200, \"error\":\"Signature error\"}")
		}
	} else {
		fmt.Fprintln(w, "{\"code\":200, \"error\":\"Unmatch x-github-event\"}")
	}
}
//main入口函数
func main()  {
    // 当有请求访问ws时,执行此回调方法
    http.HandleFunc("/",wsHandler)
    // 监听127.0.0.1:7777
    err := http.ListenAndServe("127.0.0.1:7777", nil)
    if err != nil {
        log.Fatal("ListenAndServe", err.Error())
    }
}
//根据 密钥和 body 生成签名
func generateHashSignature(message string, secret string) string {
	h := hmac.New(sha1.New, []byte(secret))
	h.Write([]byte(message))
	return "sha1=" + hex.EncodeToString(h.Sum(nil))
}
// 比较签名结果
func VerifySignature(signature string, data string, secret string) bool {
	return signature == generateHashSignature(string(data), secret)
}

/www目录是 nginx 的root目录,静态页是直接pull到 www的。所以屏蔽了对.git的访问。

github page的地址: https://82kg.github.io/

/images/webhook.jpg