Golang 部署webhook,远程触发执行指定脚本。
下载
GITHUB地址:https://github.com/adnanh/webhook
https://github.com/adnanh/webhook/releases 直接下载最新的对应版本即可。
webhook配置
vim webhook.json
使用时把备注删掉
[
{
"id": "4444", // 这里填写 http://server/hooks/{ID} 对应的是{ID} 值。也是唯一值。
"execute-command": "./test4.sh", // 须要执行的shell文件。
"command-working-directory": "./", // 执行的工作目录。
"pass-arguments-to-command": // 指定获取 post请求参数
[
{
"source": "payload", // 这里是固定,获取请求信息
"name": "text.content" // 这里text.content 是获取 json里面 text:content 里面对应的值。 在测试里面能更直观查看。
}
],
"trigger-rule": // 指定数据格式才执行。
{
"match":
{
"type": "value",
"value": "lswzw", // 这里做的是匹配固定值。
"parameter":
{
"source": "payload", // 获取请求信息
"name": "msgtype" // 获取请求里 json msgtype:对应的参数
}
}
}
},
// 下面是创建一个2222 这个接口, 请求后执行不同的指令。
{
"id": "2222",
"execute-command": "./test2.sh",
"command-working-directory": "./",
"pass-arguments-to-command":
[
{
"source": "payload",
"name": "text.content"
}
],
"trigger-rule":
{
"match":
{
"type": "value",
"value": "lswzw",
"parameter":
{
"source": "payload",
"name": "msgtype"
}
}
}
}
]
要执行的脚本配置
这里就是要执行的shell 可以自己写任何shell内容。 $1 即传入的第1个参数。
test2.sh
#!/bin/bash
echo $1 >> test
test4.sh
#!/bin/bash
echo $1 >> test
启动
nohup ./webhook -port 9000 -hotreload -hooks webhook.json -verbose > webhook.log 2>&1 &
注: -hotreload 直接修改 json文件保存后 会自动重新加载配置文件。不须要重启服务。
调用测试
curl -H "Content-Type:application/json" -X POST --data '{
"msgtype": "lswzw",
"text":{
"content":"this is lswzw 2222!"
}
}' http://127.0.0.1:9000/hooks/2222
curl -H "Content-Type:application/json" -X POST --data '{
"msgtype": "lswzw",
"text":{
"content":"this is lswzw 4444!"
}
}' http://127.0.0.1:9000/hooks/4444
执行后。 查看生成的test文件 里面就会带上 content 传过来的值。