Golang 部署webhook,远程触发执行指定脚本。

下载

GITHUB地址:https://github.com/adnanh/webhook

https://github.com/adnanh/webhook/releases 直接下载最新的对应版本即可。

webhook配置

vim 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
[
  {
    "id": "lswzw-test", // 这里填写 http://server/hooks/{ID} 对应的是{ID} 值。也是唯一值。
    "execute-command": "./test.sh", // 须要执行的shell文件。
    "command-working-directory": "./", // 执行的工作目录。
    "pass-arguments-to-command": // 指定获取 post请求参数
    [
      {
        "source": "payload", // 这里是固定,获取请求信息
        "name": "text.content" // 这里text.content 是获取 json里面 text:content 里面对应的值。 在测试里面能更直观查看。
      },// 官方实例里面 这里是可以获取多个值。 我这只须要1个。所以只留了1个。
    ],
    "trigger-rule": // 指定数据格式才执行。
    {
      "and":
      [
        {
          "match":
          {
            "type": "value",
            "value": "lswzw", // 这里做的是匹配固定值。
            "parameter":
            {
              "source": "payload", // 获取请求信息
              "name": "msgtype"   // 获取请求里 json   msgtype:对应的参数
            }
          }
        }
      ]
    }
  }
]

要执行的脚本配置

vim test.sh
这里就是要执行的shell 可以自己写任何shell内容。 $1 即传入的第1个参数。

1
2
#!/bin/bash
echo $1 >> test

启动

1
nohup ./webhook -port 9000 -hotreload -hooks webhook.json -verbose > webhook.log 2>&1 &

注: -hotreload 直接修改 json文件保存后 会自动重新加载配置文件。不须要重启服务。

调用测试

1
2
3
4
5
6
curl -H "Content-Type:application/json" -X POST --data '{
  "msgtype": "lswzw",
  "text":{
  "content":"this is lswzw test!"
  }
}' http://127.0.0.1:9000/hooks/lswzw-test

执行后。 查看生成的test文件 里面就会带上 content 传过来的值。