接上篇webhook部署
官網(wǎng)參考地址
碼云地址
github地址
參考配置
實(shí)現(xiàn)webhook以post方式傳送數(shù)據(jù)
1.大部分情況是傳遞json格式的數(shù)據(jù)
參考curl命令的詳細(xì)使用curl命令使用
curl -H "Content-Type: application/json" -X POST -d '{"info":{"name":"wade.qu", "passwd":"123456","data":"this is test content"}}' http://10.41.1.20:9001/hooks/test-webhook
- 配置hooks.json文件接受相關(guān)參數(shù)配置
hook.json
[
{
"id": "test-webhook",
"execute-command": "/tmp/redeploy.sh",
"command-working-directory": "/tmp",
"pass-arguments-to-command":
[
{
"source": "payload",
"name": "info"
}
]
}
]
2.啟動(dòng)項(xiàng)目 webhook -hooks hook.json -verbose --port 9001
3.配置腳本接受參數(shù)或者打印參數(shù)
redeploy.sh腳本內(nèi)容
#!/bin/bash
filename=`date +%Y%m%H%M%S`
touch $filename
echo "this is test file ,please test"
echo $1 > $filename
4.發(fā)送請(qǐng)求鏈接進(jìn)行測(cè)試
curl -H "Content-Type: application/json" -X POST -d '{"info":{"name":"wade.qu", "passwd":"123456","data":"this is test content"}}' http://10.41.1.20:9001/hooks/test-webhook
結(jié)果內(nèi)容如下
root@devops-test:/tmp# cat 202210141848
map[passwd:123456 data:this is test content name:wade.qu]
5.定義匹配的規(guī)則信息以及相關(guān)參數(shù)
通過(guò)token匹配請(qǐng)求是否準(zhǔn)入
curl -H "Content-Type: application/jsons" -X POST -d '{"info":{"name":"wade.qu", "passwd":"123456","data":"this is test content"}}' 'http://10.41.1.20:9001/hooks/test-webhook?token=5656'
匹配的hook.json文件內(nèi)容如下
[
{
"id": "test-webhook",
"execute-command": "/tmp/redeploy.sh",
"response-message": "ok",
"command-working-directory": "/tmp",
"response-headers":
[
{
"name": "Access-Control-Allow-Origin",
"value": "application/json"
}
],
"pass-arguments-to-command":
[
{
"source": "header",
"name": "Content-Type"
},
{
"source": "payload",
"name": "info.name"
},
{
"source": "payload",
"name": "info.passwd"
}
],
"trigger-rule":
{
"and":
[
{
"match":
{
"type": "value",
"value": "5656",
"parameter":
{
"source": "url",
"name": "token"
}
}
}
]
}
}
]
此時(shí)如果不帶對(duì)應(yīng)的token 將會(huì)出現(xiàn)不滿足請(qǐng)求的條件的提示
其中 "response-message": "ok" 這個(gè)參數(shù)是指定返回值的內(nèi)容(如果請(qǐng)求成功的話)