搭建 webhooks 自动部署 Github 代码至自建 Linux 服务器
webhook
博客默认节点部署在github上,服务器部署的从节点,以及又拍云多节点部署,但是每次提交之后都要手动同步有点浪费时间, 查找文档发现webhook可以实现github有代码变更可以通知其他节点实现所有节点同步的功能, webhook有python,php,nodejs等版本 作为屌丝运维当然首选go了
下载
wget https://github.com/adnanh/webhook/releases/download/2.7.0/webhook-linux-amd64.tar.gz
tar -zxvf webhook-linux-amd64.tar.gz
mv webhook /usr/local/bin/
mkdir -p /etc/hooks
示例
vi /etc/hooks/hooks.json
[
{
"id": "deploy-repo",
"execute-command": "/etc/hooks/deploy.sh",
"response-message": "Executing deploy script",
"trigger-rule": {
"and": [{
"match": {
"type": "payload-hash-sha1",
"secret": "iloveyou",
"parameter": {
"source": "header",
"name": "X-Hub-Signature"
}
}
}, {
"match": {
"type": "value",
"value": "refs/heads/master",
"parameter": {
"source": "payload",
"name": "ref"
}
}
}]
}
}
]
示例
vi /etc/hooks/deploy.sh
#!/bin/bash
cd /path/to/repository
#git fetch -all
#git reset --hard origin/master
git pull
systemd
#添加服务 vi /usr/lib/systemd/system/webhook.service
[Unit]
Description=Webhook
[Service]
ExecStart=/usr/local/bin/webhook -hooks /etc/hooks/hooks.json -hotreload
[Install]
WantedBy=multi-user.target
启动服务
systemctl enable webhook.service --now
添加反向代理
nginx配置
server{
listen 80;
server_name webhooks.demo.com;
location / {
index index.html;
proxy_pass http://192.168.1.41:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
}
github设置webhook如图
文章作者 🐳Myki
上次更新 2020-10-09