本博客的模板是 Volantis,开发者们非常勤劳,经常更新些新玩意。每次收到更新通知时杜老师都会第一时间同步代码,不过还要登录服务器部署才可以生效。本文分享如何配置接收 GitHub Webhook 后自动执行脚本。
解压配置
下载后可先本地解压再传到服务器上的/opt
目录,或在服务器运行 unzip linux-webhook.zip
。如提示 command not found
,请执行下面的命令:
yum -y install unzip # CentOS
apt -y install unzip # Ubuntu
然后编写服务配置文件,将下面代码写入/lib/systemd/system/webhook.service
,其中的 SECRET
可自定义:
[Unit]
Description=linux-webhook
After=network.target
[Service]
Type=simple
ExecStart=/opt/linux-webhook --bash /opt/github.sh --secret SECRET
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
编写脚本
将需执行的脚本写入/opt/github.sh
,例如:
#!/bin/bash
echo "Hello Webhook"
exit 0
脚本末尾一定要加入 exit 0
。最后赋予文件执行权限:
chmod +x /opt/linux-webhook
chmod +x /opt/github.sh
服务相关命令:
systemctl daemon-reload # 重载配置
systemctl start webhook # 启动服务
systemctl status webhook # 状态查询
systemctl enable webhook # 开机启动
服务默认端口为 2020
,更换请将 ExecStart
项改为:
ExecStart=/opt/linux-webhook --port PORT --bash /opt/github.sh --secret SECRET
条评论