设置webhook_webhook工具实现

# webhook工具

## webhook工作流程

![](https://img.kancloud.cn/84/b5/84b589fc655203fdda63b21bf3ae66e5_384x308.png)

## 安装

由于`webhook`是golang开发的, 所以要先安装`golang`

```

$ yum install -y golang

```

然后安装webhook

```

$ go get github.com/adnanh/webhook

```

查看`GOPATH`找到安装目录

```

$ go env

>>>

GOPATH=/root/go

>>>

```

所以安装目录就是`/root/go/bin/webhook`

## 生成ssh key

要获得`pull`权限, 需要把服务器的`ssh key`添加到(github, gitee, gitlab, gitea)上

```

$ ssh-keygen

一路回车

```

然后查看公钥

```

$ cat ~/.ssh/id_rsa.pub

ssh-rsa JHGGGKGYUUGH..........GYUFGUYFUGKJIFGUTFUU root@xxx

```

把她添加到github或者gitee等网站的公钥中, 就可以获得pull权限

## Shell脚本编写

```bash

#!/bin/bash

cd /www/wwwroot

if [ ! -d "webapp" ]; then

git clone [仓库地址]

fi

cd webapp

git pull origin master

```

这里我们拟保存在`/home/webhook/webapp.sh`

**记得添加执行权限, 和测试**

```

$ chmod +x webapp.sh

```

## hook配置和启动

**hooks.json**

```

[

{

"id": "woc",

"execute-command": "webapp.sh",

"command-working-directory": "/home/webhook"

}

]

```

- id 顾名思义, 可以自定义

- execute-command 执行脚本名称

- command-working-directory 脚本所在目录

支持多个

**启动**

```

$ /root/go/bin/webhook -hooks hooks.json -verbose

后台运行, 关闭终端也会运行

$ nohup /root/go/bin/webhook -hooks hooks.json -verbose > /dev/null 2>&1 &

```

然后你可以访问

```

http://yourdomain:9000/hooks/{id}

````

并添加在`github webhook中`

## 参考

- [https://zhuanlan.zhihu.com/p/136364880](https://zhuanlan.zhihu.com/p/136364880)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值