{"id":31123,"date":"2026-06-17T08:00:00","date_gmt":"2026-06-17T00:00:00","guid":{"rendered":"https:\/\/cn.hostease.com\/blog\/?p=31123"},"modified":"2026-06-17T11:53:32","modified_gmt":"2026-06-17T03:53:32","slug":"webhook-github-server-auto-deploy","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/blog\/tutorials\/webhook-github-server-auto-deploy\/","title":{"rendered":"Webhook \u4e8b\u4ef6\u9a71\u52a8\u81ea\u52a8\u5316\u5b9e\u6218\uff1a\u4ece GitHub \u5230\u670d\u52a1\u5668\u7684\u81ea\u52a8\u90e8\u7f72"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u4f60\u9700\u8981 Webhook \u9a71\u52a8\u7684\u81ea\u52a8\u90e8\u7f72<\/h2>\n<p><img decoding=\"async\" alt=\"Webhook \u4e8b\u4ef6\u9a71\u52a8\u81ea\u52a8\u90e8\u7f72\u5c01\u9762\u56fe\" src=\"https:\/\/cn.hostease.com\/blog\/wp-content\/uploads\/2026\/06\/webhook-practical-github-server-cover.webp\" \/><\/p>\n<p>\u6bcf\u6b21\u6539\u5b8c\u4ee3\u7801\uff0c\u624b\u52a8\u767b\u5f55\u670d\u52a1\u5668\u3001\u62c9\u53d6\u6700\u65b0\u7248\u672c\u3001\u91cd\u542f\u670d\u52a1\u2014\u2014\u8fd9\u5957\u6d41\u7a0b\u4f60\u91cd\u590d\u4e86\u591a\u5c11\u6b21\uff1f\u5982\u679c\u56e2\u961f\u6709\u4e09\u4e2a\u4eba\u540c\u65f6\u63d0\u4ea4\u4ee3\u7801\uff0c\u90e8\u7f72\u7a97\u53e3\u5c31\u53d8\u6210\u4e86\u6392\u961f\u7a97\u53e3\uff0c\u7ebf\u4e0a\u73af\u5883\u968f\u65f6\u53ef\u80fd\u56e0\u4e3a\u6f0f\u62c9\u5206\u652f\u800c\u51fa\u9519\u3002<\/p>\n<p>Webhook\uff08\u4e8b\u4ef6\u56de\u8c03\uff09\u89e3\u51b3\u7684\u6838\u5fc3\u95ee\u9898\u5c31\u662f\u300c\u8c01\u6765\u901a\u77e5\u670d\u52a1\u5668\u8be5\u66f4\u65b0\u4e86\u300d\u3002GitHub \u5728\u4f60\u63a8\u9001\u4ee3\u7801\u7684\u77ac\u95f4\uff0c\u5411\u4f60\u6307\u5b9a\u7684\u670d\u52a1\u5668\u5730\u5740\u53d1\u9001\u4e00\u6761 HTTP \u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u6536\u5230\u540e\u81ea\u52a8\u6267\u884c\u62c9\u53d6\u548c\u90e8\u7f72\u811a\u672c\u3002\u6574\u4e2a\u8fc7\u7a0b\u4e0d\u9700\u8981\u4eba\u76ef\u3001\u4e0d\u9700\u8981\u624b\u52a8\u89e6\u53d1\uff0c\u4ee3\u7801\u5408\u5e76\u548c\u4e0a\u7ebf\u4e4b\u95f4\u7684\u5ef6\u8fdf\u53ef\u4ee5\u538b\u7f29\u5230 30 \u79d2\u4ee5\u5185\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u4f1a\u5e26\u4f60\u4ece\u96f6\u642d\u5efa\u4e00\u5957\u57fa\u4e8e GitHub Webhook \u7684\u81ea\u52a8\u90e8\u7f72\u6d41\u7a0b\uff1a\u4ece\u670d\u52a1\u5668\u7aef\u63a5\u6536\u811a\u672c\u7684\u7f16\u5199\uff0c\u5230 GitHub \u4fa7\u7684 Webhook \u914d\u7f6e\uff0c\u518d\u5230\u5b89\u5168\u6821\u9a8c\u3001\u591a\u73af\u5883\u5206\u652f\u7ba1\u7406\u548c\u5e38\u89c1\u6545\u969c\u6392\u67e5\u3002\u5982\u679c\u4f60\u6b63\u5728\u7528 <a href=\"https:\/\/cn.hostease.com\/vps\/\">VPS\uff08\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668\uff09<\/a>\u6216\u4e91\u670d\u52a1\u5668\uff08\u5f39\u6027\u8ba1\u7b97\u5b9e\u4f8b\uff09\u8dd1\u9879\u76ee\uff0c\u8fd9\u5957\u65b9\u6848\u51e0\u4e4e\u96f6\u6210\u672c\u5c31\u80fd\u843d\u5730\u3002<\/p>\n<h2>Webhook \u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u5728\u52a8\u624b\u914d\u7f6e\u4e4b\u524d\uff0c\u5148\u7406\u89e3\u4e00\u4e0b Webhook \u7684\u89e6\u53d1\u94fe\u8def\u3002\u5f53\u4f60\u5728 GitHub \u4ed3\u5e93\u6267\u884c <code>git push<\/code>\uff0cGitHub \u4f1a\u68c0\u67e5\u8be5\u4ed3\u5e93\u662f\u5426\u6ce8\u518c\u4e86 Webhook\u3002\u5982\u679c\u5339\u914d\u5230\u5bf9\u5e94\u4e8b\u4ef6\uff08\u9ed8\u8ba4\u662f <code>push<\/code>\uff09\uff0cGitHub \u5c31\u5411\u4f60\u9884\u8bbe\u7684 URL \u53d1\u9001\u4e00\u4e2a POST \u8bf7\u6c42\uff0c\u8bf7\u6c42\u4f53\u5305\u542b\u8fd9\u6b21\u63a8\u9001\u7684\u5b8c\u6574\u4fe1\u606f\uff1a\u54ea\u4e2a\u5206\u652f\u3001\u8c01\u63d0\u4ea4\u7684\u3001\u6539\u4e86\u54ea\u4e9b\u6587\u4ef6\u3001\u63d0\u4ea4\u4fe1\u606f\u662f\u4ec0\u4e48\u3002<\/p>\n<p>\u670d\u52a1\u5668\u7aef\u53ea\u9700\u8981\u505a\u4e00\u4ef6\u4e8b\uff1a\u76d1\u542c\u8fd9\u4e2a POST \u8bf7\u6c42\uff0c\u9a8c\u8bc1\u6765\u6e90\u5408\u6cd5\u6027\uff0c\u7136\u540e\u6267\u884c\u9884\u5b9a\u4e49\u7684\u90e8\u7f72\u811a\u672c\u3002\u6574\u4e2a\u6d41\u7a0b\u53ef\u4ee5\u7528\u56db\u4e2a\u73af\u8282\u6982\u62ec\uff1a<\/p>\n<ol>\n<li><strong>\u4ee3\u7801\u63a8\u9001<\/strong> \u2014 \u5f00\u53d1\u8005\u6267\u884c <code>git push<\/code> \u5230 GitHub<\/li>\n<li><strong>\u4e8b\u4ef6\u89e6\u53d1<\/strong> \u2014 GitHub \u68c0\u6d4b\u5230 push \u4e8b\u4ef6\uff0c\u7ec4\u88c5 payload<\/li>\n<li><strong>HTTP \u56de\u8c03<\/strong> \u2014 GitHub \u5411\u4f60\u7684\u670d\u52a1\u5668\u53d1\u9001 POST \u8bf7\u6c42<\/li>\n<li><strong>\u6267\u884c\u90e8\u7f72<\/strong> \u2014 \u670d\u52a1\u5668\u9a8c\u8bc1\u7b7e\u540d\u540e\u8fd0\u884c\u90e8\u7f72\u811a\u672c<\/li>\n<\/ol>\n<p>\u8fd9\u5957\u6a21\u5f0f\u7684\u597d\u5904\u5728\u4e8e\u5b83\u662f\u7eaf\u4e8b\u4ef6\u9a71\u52a8\u7684\uff1a\u6ca1\u6709\u5b9a\u65f6\u8f6e\u8be2\uff0c\u6ca1\u6709\u989d\u5916\u7684\u4e2d\u95f4\u4ef6\uff0cGitHub \u672c\u8eab\u5c31\u662f\u4e8b\u4ef6\u6e90\u3002\u5bf9\u4e8e\u4e2d\u5c0f\u578b\u9879\u76ee\u6765\u8bf4\uff0c\u8fd9\u6bd4\u642d\u5efa Jenkins \u6216 GitLab CI \u66f4\u8f7b\u91cf\u3002<\/p>\n<h2>\u642d\u5efa\u670d\u52a1\u5668\u7aef Webhook \u63a5\u6536\u5668<\/h2>\n<h3>\u73af\u5883\u51c6\u5907<\/h3>\n<p>\u5728\u4f60\u7684\u670d\u52a1\u5668\uff08\u5efa\u8bae\u8fd0\u884c Ubuntu 22.04 \u6216 Debian 12\uff09\u4e0a\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5 Git \u548c Node.js\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo apt update &amp;&amp; sudo apt install -y git curl\ncurl -fsSL https:\/\/deb.nodesource.com\/setup_20.x | sudo -E bash -\nsudo apt install -y nodejs\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u662f Python \u540e\u7aef\uff0c\u4e5f\u53ef\u4ee5\u7528 Flask \u6216 FastAPI \u6765\u63a5\u6536 Webhook\uff0c\u539f\u7406\u76f8\u540c\u3002\u8fd9\u91cc\u7528 Node.js \u505a\u793a\u4f8b\uff0c\u56e0\u4e3a\u5b83\u5904\u7406 JSON \u8bf7\u6c42\u4f53\u6700\u65b9\u4fbf\u3002<\/p>\n<h3>\u521b\u5efa\u63a5\u6536\u811a\u672c<\/h3>\n<p>\u5728\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u7528\u4e8e\u5b58\u653e\u90e8\u7f72\u670d\u52a1\uff1a<\/p>\n<pre><code class=\"language-bash\">mkdir -p \/opt\/webhook-receiver &amp;&amp; cd \/opt\/webhook-receiver\nnpm init -y\nnpm install express crypto\n<\/code><\/pre>\n<p>\u7136\u540e\u521b\u5efa <code>server.js<\/code>\uff1a<\/p>\n<pre><code class=\"language-javascript\">const express = require('express');\nconst crypto = require('crypto');\nconst { execSync } = require('child_process');\n\nconst app = express();\nconst PORT = 9000;\nconst SECRET = process.env.WEBHOOK_SECRET || 'your-strong-secret-here';\nconst DEPLOY_DIR = '\/var\/www\/your-project';\n\nfunction verifySignature(payload, signature) {\n  const hmac = crypto.createHmac('sha256', SECRET);\n  hmac.update(payload);\n  const digest = 'sha256=' + hmac.digest('hex');\n  return crypto.timingSafeEqual(\n    Buffer.from(digest),\n    Buffer.from(signature)\n  );\n}\n\napp.post('\/deploy', express.raw({ type: 'application\/json' }), (req, res) =&gt; {\n  const signature = req.headers['x-hub-signature-256'];\n  if (!signature || !verifySignature(req.body, signature)) {\n    console.error('\u7b7e\u540d\u9a8c\u8bc1\u5931\u8d25\uff0c\u62d2\u7edd\u6267\u884c\u90e8\u7f72');\n    return res.status(403).json({ error: 'Invalid signature' });\n  }\n\n  const payload = JSON.parse(req.body.toString());\n  const branch = payload.ref?.replace('refs\/heads\/', '');\n  console.log(`\u6536\u5230 push \u4e8b\u4ef6: \u5206\u652f=${branch}, \u63d0\u4ea4\u8005=${payload.pusher?.name}`);\n\n  try {\n    execSync(`cd ${DEPLOY_DIR} &amp;&amp; git pull origin ${branch}`, { timeout: 60000 });\n    \/\/ \u6839\u636e\u9879\u76ee\u7c7b\u578b\u6267\u884c\u540e\u7eed\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a\n    \/\/ execSync('npm install --production', { cwd: DEPLOY_DIR });\n    \/\/ execSync('pm2 restart your-app');\n    console.log('\u90e8\u7f72\u5b8c\u6210');\n    res.json({ status: 'ok', branch });\n  } catch (err) {\n    console.error('\u90e8\u7f72\u811a\u672c\u6267\u884c\u5931\u8d25:', err.message);\n    res.status(500).json({ error: err.message });\n  }\n});\n\napp.listen(PORT, () =&gt; {\n  console.log(`Webhook \u63a5\u6536\u5668\u5df2\u542f\u52a8\uff0c\u76d1\u542c\u7aef\u53e3 ${PORT}`);\n});\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u505a\u4e86\u4e09\u4ef6\u4e8b\uff1a\u9a8c\u8bc1 GitHub \u53d1\u6765\u7684 HMAC-SHA256 \u7b7e\u540d\u3001\u89e3\u6790\u63a8\u9001\u7684\u5206\u652f\u4fe1\u606f\u3001\u5728\u76ee\u6807\u76ee\u5f55\u6267\u884c <code>git pull<\/code>\u3002\u7b7e\u540d\u9a8c\u8bc1\u662f\u5b89\u5168\u7684\u5173\u952e\u2014\u2014\u6ca1\u6709\u5b83\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u5411\u4f60\u7684\u7aef\u53e3\u53d1 POST \u8bf7\u6c42\u89e6\u53d1\u90e8\u7f72\u3002<\/p>\n<h3>\u7528 systemd \u6258\u7ba1\u670d\u52a1<\/h3>\n<p>\u4e3a\u4e86\u8ba9\u8fd9\u4e2a\u63a5\u6536\u5668\u5728\u670d\u52a1\u5668\u91cd\u542f\u540e\u81ea\u52a8\u8fd0\u884c\uff0c\u521b\u5efa\u4e00\u4e2a systemd \u670d\u52a1\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo tee \/etc\/systemd\/system\/webhook-receiver.service &gt; \/dev\/null &lt;&lt; 'EOF'\n[Unit]\nDescription=GitHub Webhook Receiver\nAfter=network.target\n\n[Service]\nType=simple\nUser=deploy\nEnvironment=WEBHOOK_SECRET=\u66ff\u6362\u4e3a\u4f60\u7684\u5f3a\u5bc6\u94a5\nWorkingDirectory=\/opt\/webhook-receiver\nExecStart=\/usr\/bin\/node server.js\nRestart=on-failure\nRestartSec=5\n\n[Install]\nWantedBy=multi-user.target\nEOF\n\nsudo systemctl daemon-reload\nsudo systemctl enable --now webhook-receiver\nsudo systemctl status webhook-receiver\n<\/code><\/pre>\n<p>\u542f\u52a8\u540e\u53ef\u4ee5\u7528 <code>curl -X POST http:\/\/localhost:9000\/deploy<\/code> \u6d4b\u8bd5\u7aef\u53e3\u662f\u5426\u6b63\u5e38\u76d1\u542c\u3002\u6b63\u5f0f\u73af\u5883\u5efa\u8bae\u5728\u524d\u9762\u52a0\u4e00\u5c42 Nginx \u53cd\u5411\u4ee3\u7406\uff0c\u628a <code>\/deploy<\/code> \u8def\u5f84\u8f6c\u53d1\u5230 9000 \u7aef\u53e3\uff0c\u540c\u65f6\u914d\u7f6e SSL\uff08\u5b89\u5168\u4f20\u8f93\u534f\u8bae\uff09\u8bc1\u4e66\u4fdd\u8bc1\u4f20\u8f93\u52a0\u5bc6\u3002<\/p>\n<h2>\u914d\u7f6e GitHub Webhook<\/h2>\n<p>\u670d\u52a1\u5668\u7aef\u51c6\u5907\u597d\u4e4b\u540e\uff0c\u56de\u5230 GitHub \u4ed3\u5e93\u5b8c\u6210\u6700\u540e\u4e00\u6b65\u914d\u7f6e\uff1a<\/p>\n<ol>\n<li>\u8fdb\u5165\u4ed3\u5e93\u7684 <strong>Settings \u2192 Webhooks \u2192 Add webhook<\/strong><\/li>\n<li><strong>Payload URL<\/strong> \u586b\u5199\u4f60\u7684\u670d\u52a1\u5668\u5730\u5740\uff0c\u4f8b\u5982 <code>https:\/\/your-server.com\/deploy<\/code><\/li>\n<li><strong>Content type<\/strong> \u9009\u62e9 <code>application\/json<\/code><\/li>\n<li><strong>Secret<\/strong> \u586b\u5199\u548c\u670d\u52a1\u5668\u7aef <code>WEBHOOK_SECRET<\/code> \u5b8c\u5168\u4e00\u81f4\u7684\u5bc6\u94a5\u5b57\u7b26\u4e32<\/li>\n<li><strong>SSL verification<\/strong> \u4fdd\u6301 enabled\uff08\u5982\u679c\u4f60\u914d\u4e86 SSL \u8bc1\u4e66\uff09<\/li>\n<li><strong>Which events<\/strong> \u9009\u62e9 <code>Just the push event<\/code>\uff08\u9ed8\u8ba4\u5373\u53ef\uff09<\/li>\n<li>\u70b9\u51fb <strong>Add webhook<\/strong> \u4fdd\u5b58<\/li>\n<\/ol>\n<p>\u4fdd\u5b58\u540e GitHub \u4f1a\u7acb\u5373\u53d1\u9001\u4e00\u4e2a ping \u8bf7\u6c42\u6d4b\u8bd5\u8fde\u901a\u6027\u3002\u5982\u679c\u670d\u52a1\u5668\u8fd4\u56de 200\uff0cWebhook \u5217\u8868\u4e2d\u4f1a\u663e\u793a\u7eff\u8272\u5bf9\u52fe\u3002\u5982\u679c\u663e\u793a\u7ea2\u8272\u611f\u53f9\u53f7\uff0c\u70b9\u8fdb\u53bb\u770b Response Body \u901a\u5e38\u80fd\u5b9a\u4f4d\u95ee\u9898\u2014\u2014\u6700\u5e38\u89c1\u7684\u662f\u670d\u52a1\u5668\u9632\u706b\u5899\u6ca1\u653e\u884c\u7aef\u53e3\uff0c\u6216\u8005 Nginx \u6ca1\u914d\u5bf9\u53cd\u5411\u4ee3\u7406\u3002<\/p>\n<p>\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u5728\u672c\u5730\u4fee\u6539\u4e00\u884c\u4ee3\u7801\u7136\u540e <code>git push<\/code>\uff0c\u89c2\u5bdf\u670d\u52a1\u5668\u65e5\u5fd7\uff08<code>journalctl -u webhook-receiver -f<\/code>\uff09\uff0c\u5982\u679c\u770b\u5230\u300c\u6536\u5230 push \u4e8b\u4ef6\u300d\u548c\u300c\u90e8\u7f72\u5b8c\u6210\u300d\u7684\u8f93\u51fa\uff0c\u6574\u6761\u94fe\u8def\u5c31\u901a\u4e86\u3002<\/p>\n<h2>\u5b89\u5168\u52a0\u56fa\u4e0e\u751f\u4ea7\u7ea7\u5b9e\u8df5<\/h2>\n<p>\u4e0a\u9762\u7684\u6700\u5c0f\u53ef\u7528\u7248\u672c\u80fd\u8dd1\u8d77\u6765\uff0c\u4f46\u8981\u4e0a\u751f\u4ea7\u73af\u5883\u8fd8\u9700\u8981\u51e0\u4e2a\u52a0\u56fa\u63aa\u65bd\u3002<\/p>\n<h3>IP \u767d\u540d\u5355\u9650\u5236<\/h3>\n<p>GitHub \u5b98\u65b9\u516c\u5e03\u4e86 Webhook \u56de\u8c03\u7684 IP \u6bb5\uff0c\u4f60\u53ef\u4ee5\u5728\u9632\u706b\u5899\u5c42\u9762\u53ea\u653e\u884c\u8fd9\u4e9b IP\u3002\u7528 <code>curl https:\/\/api.github.com\/meta<\/code> \u83b7\u53d6 <code>hooks<\/code> \u5b57\u6bb5\u4e0b\u7684 IP \u5217\u8868\uff0c\u7136\u540e\u914d\u5230\u4f60\u7684\u9632\u706b\u5899\u89c4\u5219\u91cc\u3002\u8fd9\u6837\u5373\u4f7f\u6709\u4eba\u77e5\u9053\u4e86\u4f60\u7684\u90e8\u7f72\u7aef\u53e3\uff0c\u4e5f\u65e0\u6cd5\u4ece\u5176\u4ed6 IP \u53d1\u8bf7\u6c42\u3002<\/p>\n<h3>\u90e8\u7f72\u9501\u9632\u6b62\u5e76\u53d1<\/h3>\n<p>\u5982\u679c\u4e24\u4e2a\u4eba\u5728 10 \u79d2\u5185\u5148\u540e push\uff0c\u670d\u52a1\u5668\u4f1a\u6536\u5230\u4e24\u6b21 Webhook\u3002\u5982\u679c\u7b2c\u4e00\u6b21 <code>git pull<\/code> \u8fd8\u6ca1\u6267\u884c\u5b8c\uff0c\u7b2c\u4e8c\u6b21\u53c8\u89e6\u53d1\uff0c\u53ef\u80fd\u5bfc\u81f4\u4ed3\u5e93\u72b6\u6001\u51b2\u7a81\u3002\u89e3\u51b3\u65b9\u6848\u662f\u52a0\u4e00\u628a\u6587\u4ef6\u9501\uff1a<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/bash\nLOCKFILE=&quot;\/tmp\/deploy.lock&quot;\nif [ -f &quot;$LOCKFILE&quot; ]; then\n  echo &quot;\u90e8\u7f72\u6b63\u5728\u8fdb\u884c\u4e2d\uff0c\u8df3\u8fc7\u672c\u6b21\u89e6\u53d1&quot;\n  exit 0\nfi\ntouch &quot;$LOCKFILE&quot;\ntrap &quot;rm -f $LOCKFILE&quot; EXIT\n\ncd \/var\/www\/your-project\ngit pull origin main\nnpm install --production\npm2 restart your-app\n<\/code><\/pre>\n<p>\u628a\u90e8\u7f72\u903b\u8f91\u4ece Node.js \u8fc1\u5230\u8fd9\u4e2a shell \u811a\u672c\u91cc\uff0cNode.js \u53ea\u8d1f\u8d23\u9a8c\u8bc1\u7b7e\u540d\u540e\u8c03\u7528 <code>execSync('bash deploy.sh')<\/code>\u3002\u6587\u4ef6\u9501\u4fdd\u8bc1\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u90e8\u7f72\u8fdb\u7a0b\u5728\u8dd1\u3002<\/p>\n<h3>\u65e5\u5fd7\u4e0e\u544a\u8b66<\/h3>\n<p>\u90e8\u7f72\u6210\u529f\u4e0d\u4ee3\u8868\u4ee3\u7801\u6ca1\u6709 bug\u3002\u5efa\u8bae\u5728\u90e8\u7f72\u811a\u672c\u672b\u5c3e\u52a0\u4e00\u6b65\u5065\u5eb7\u68c0\u67e5\uff0c\u6bd4\u5982 <code>curl -s -o \/dev\/null -w \"%{http_code}\" http:\/\/localhost:3000\/health<\/code>\uff0c\u5982\u679c\u8fd4\u56de\u975e 200 \u5c31\u901a\u8fc7\u4f01\u4e1a\u5fae\u4fe1\u6216\u9489\u9489 Webhook \u53d1\u544a\u8b66\u3002\u540c\u65f6\u628a\u6bcf\u6b21\u90e8\u7f72\u7684 Git commit hash \u548c\u65f6\u95f4\u6233\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\uff0c\u65b9\u4fbf\u56de\u6eda\u65f6\u5b9a\u4f4d\u300c\u4e0a\u6b21\u6b63\u5e38\u8fd0\u884c\u662f\u54ea\u4e2a\u7248\u672c\u300d\u3002\u5173\u4e8e<a href=\"https:\/\/cn.hostease.com\/blog\/server\/\">\u670d\u52a1\u5668\u6027\u80fd<\/a>\u76d1\u63a7\u548c\u65e5\u5fd7\u7ba1\u7406\u7684\u66f4\u591a\u6280\u5de7\uff0c\u53ef\u4ee5\u53c2\u8003<a href=\"https:\/\/cn.hostease.com\/blog\/optimization\/\">\u7f51\u7ad9\u4f18\u5316\u6307\u5357<\/a>\u3002<\/p>\n<h2>\u591a\u73af\u5883\u5206\u652f\u7b56\u7565<\/h2>\n<p>\u5b9e\u9645\u9879\u76ee\u901a\u5e38\u6709\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u751f\u4ea7\u4e09\u5957\u73af\u5883\u3002\u4f60\u53ef\u4ee5\u7528\u5206\u652f\u540d\u6765\u533a\u5206\u90e8\u7f72\u76ee\u6807\uff1a<\/p>\n<ul>\n<li>\u63a8\u9001\u5230 <code>develop<\/code> \u5206\u652f \u2192 \u81ea\u52a8\u90e8\u7f72\u5230\u6d4b\u8bd5\u670d\u52a1\u5668<\/li>\n<li>\u63a8\u9001\u5230 <code>main<\/code> \u5206\u652f \u2192 \u81ea\u52a8\u90e8\u7f72\u5230\u751f\u4ea7\u670d\u52a1\u5668<\/li>\n<li>\u63a8\u9001\u5230 <code>feature\/*<\/code> \u5206\u652f \u2192 \u4e0d\u89e6\u53d1\u90e8\u7f72\uff08\u5728 Webhook \u63a5\u6536\u5668\u91cc\u8fc7\u6ee4\uff09<\/li>\n<\/ul>\n<p>\u5728 <code>server.js<\/code> \u91cc\u52a0\u4e00\u6bb5\u5206\u652f\u5224\u65ad\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-javascript\">const DEPLOY_MAP = {\n  'main': { dir: '\/var\/www\/prod', script: 'deploy-prod.sh' },\n  'develop': { dir: '\/var\/www\/staging', script: 'deploy-staging.sh' },\n};\n\nconst target = DEPLOY_MAP[branch];\nif (!target) {\n  console.log(`\u5206\u652f ${branch} \u4e0d\u5728\u90e8\u7f72\u6620\u5c04\u4e2d\uff0c\u8df3\u8fc7`);\n  return res.json({ status: 'skipped', branch });\n}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7b56\u7565\u7684\u597d\u5904\u662f\u56e2\u961f\u6210\u5458\u4e0d\u9700\u8981\u8bb0\u4efb\u4f55\u90e8\u7f72\u547d\u4ee4\uff0c\u53ea\u8981\u5f80\u6b63\u786e\u7684\u5206\u652f\u63a8\u4ee3\u7801\u5c31\u884c\u3002\u6d4b\u8bd5\u73af\u5883\u53ef\u4ee5\u5bb9\u5fcd\u5feb\u901f\u8fed\u4ee3\uff0c\u751f\u4ea7\u73af\u5883\u5219\u5efa\u8bae\u914d\u5408 Pull Request Review \u6d41\u7a0b\u2014\u2014\u53ea\u6709\u5408\u5e76\u5230 <code>main<\/code> \u7684\u4ee3\u7801\u624d\u4f1a\u89e6\u53d1\u751f\u4ea7\u90e8\u7f72\u3002\u5982\u679c\u4f60\u7684\u751f\u4ea7\u73af\u5883\u8dd1\u5728<a href=\"https:\/\/cn.hostease.com\/dedicated-server\/\">\u72ec\u7acb\u670d\u52a1\u5668\uff08\u7269\u7406\u4e13\u7528\u670d\u52a1\u5668\uff09<\/a>\u4e0a\uff0c\u591a\u73af\u5883\u9694\u79bb\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u7cfb\u7edf\u7528\u6237\u548c\u76ee\u5f55\u6743\u9650\u6765\u8fdb\u4e00\u6b65\u52a0\u56fa\u3002<\/p>\n<h2>\u5e38\u89c1\u6545\u969c\u6392\u67e5<\/h2>\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u4ee5\u4e0b\u95ee\u9898\u51fa\u73b0\u9891\u7387\u6700\u9ad8\uff1a<\/p>\n<p><strong>Webhook \u53d1\u9001\u6210\u529f\u4f46\u670d\u52a1\u5668\u6ca1\u53cd\u5e94<\/strong> \u2014 \u5927\u6982\u7387\u662f\u9632\u706b\u5899\u6216\u5b89\u5168\u7ec4\u6ca1\u653e\u884c\u7aef\u53e3\u3002\u7528 <code>curl<\/code> \u4ece\u5916\u90e8\u670d\u52a1\u5668\u6d4b\u8bd5\u4f60\u7684\u90e8\u7f72\u7aef\u53e3\u662f\u5426\u53ef\u8fbe\u3002\u5982\u679c\u7528\u4e86 Nginx \u53cd\u5411\u4ee3\u7406\uff0c\u68c0\u67e5 <code>proxy_pass<\/code> \u76ee\u6807\u7aef\u53e3\u662f\u5426\u548c Node.js \u76d1\u542c\u7aef\u53e3\u4e00\u81f4\u3002<\/p>\n<p><strong>\u7b7e\u540d\u9a8c\u8bc1\u4e00\u76f4\u5931\u8d25<\/strong> \u2014 \u6700\u5e38\u89c1\u7684\u539f\u56e0\u662f GitHub Secret \u548c\u670d\u52a1\u5668\u7aef\u73af\u5883\u53d8\u91cf\u4e0d\u4e00\u81f4\u3002\u6ce8\u610f Secret \u524d\u540e\u4e0d\u8981\u6709\u7a7a\u683c\uff0c\u73af\u5883\u53d8\u91cf\u6587\u4ef6\u4fee\u6539\u540e\u8981 <code>systemctl restart webhook-receiver<\/code> \u624d\u751f\u6548\u3002<\/p>\n<p><strong>\u90e8\u7f72\u811a\u672c\u6743\u9650\u4e0d\u8db3<\/strong> \u2014 systemd \u670d\u52a1\u7684 <code>User=deploy<\/code> \u9700\u8981\u5bf9\u9879\u76ee\u76ee\u5f55\u6709\u8bfb\u5199\u6743\u9650\u3002\u7528 <code>ls -la \/var\/www\/your-project<\/code> \u786e\u8ba4\u76ee\u5f55\u5f52\u5c5e\uff0c\u5fc5\u8981\u65f6 <code>chown -R deploy:deploy \/var\/www\/your-project<\/code>\u3002<\/p>\n<p><strong>push \u540e\u90e8\u7f72\u5ef6\u8fdf\u5f88\u957f<\/strong> \u2014 GitHub Webhook \u7684\u6295\u9012\u6709\u91cd\u8bd5\u673a\u5236\uff0c\u5982\u679c\u670d\u52a1\u5668\u54cd\u5e94\u6162\uff08\u8d85\u8fc7 10 \u79d2\uff09\uff0cGitHub \u4f1a\u6807\u8bb0\u4e3a\u8d85\u65f6\u5e76\u7a0d\u540e\u91cd\u8bd5\u3002\u68c0\u67e5\u90e8\u7f72\u811a\u672c\u662f\u5426\u6709\u8017\u65f6\u64cd\u4f5c\uff08\u6bd4\u5982\u5168\u91cf <code>npm install<\/code>\uff09\uff0c\u5c3d\u91cf\u4f18\u5316\u5230\u589e\u91cf\u5b89\u88c5\u3002<\/p>\n<p><img decoding=\"async\" alt=\"Webhook \u6545\u969c\u6392\u67e5\u6d41\u7a0b\u793a\u610f\u56fe\" src=\"https:\/\/cn.hostease.com\/blog\/wp-content\/uploads\/2026\/06\/webhook-practical-github-server-troubleshoot.webp\" \/><\/p>\n<h2>\u603b\u7ed3\uff1a\u5efa\u8bae\u4e0e\u4e0b\u4e00\u6b65\u884c\u52a8<\/h2>\n<p>Webhook \u9a71\u52a8\u7684\u81ea\u52a8\u90e8\u7f72\u672c\u8d28\u4e0a\u662f\u628a\u300c\u4eba\u627e\u4ee3\u7801\u300d\u53d8\u6210\u4e86\u300c\u4ee3\u7801\u627e\u4eba\u300d\u2014\u2014\u4ee3\u7801\u63a8\u4e0a\u53bb\uff0c\u670d\u52a1\u5668\u81ea\u52a8\u54cd\u5e94\u3002\u5bf9\u4e8e\u4e2d\u5c0f\u56e2\u961f\u548c\u4e2a\u4eba\u9879\u76ee\u6765\u8bf4\uff0c\u8fd9\u5957\u65b9\u6848\u7684\u8fd0\u7ef4\u6210\u672c\u8fdc\u4f4e\u4e8e\u642d\u5efa\u5b8c\u6574\u7684 CI\/CD \u5e73\u53f0\uff0c\u800c\u4e14\u4ece\u63d0\u4ea4\u5230\u4e0a\u7ebf\u7684\u53cd\u9988\u5468\u671f\u53ef\u4ee5\u538b\u7f29\u5230\u534a\u5206\u949f\u3002<\/p>\n<p>\u5982\u679c\u4f60\u51c6\u5907\u843d\u5730\uff0c\u5efa\u8bae\u6309\u8fd9\u4e2a\u987a\u5e8f\u63a8\u8fdb\uff1a\u5148\u5728\u6d4b\u8bd5\u73af\u5883\u8dd1\u901a\u5355\u5206\u652f\u90e8\u7f72\uff0c\u786e\u8ba4 Webhook \u6536\u53d1\u6b63\u5e38\uff1b\u518d\u52a0\u7b7e\u540d\u9a8c\u8bc1\u548c\u6587\u4ef6\u9501\uff0c\u786e\u4fdd\u5b89\u5168\u548c\u5e76\u53d1\u6ca1\u95ee\u9898\uff1b\u6700\u540e\u6269\u5c55\u5230\u591a\u73af\u5883\u5206\u652f\u6620\u5c04\u3002\u90e8\u7f72\u811a\u672c\u5efa\u8bae\u7528\u72ec\u7acb\u7684 shell \u6587\u4ef6\u7ba1\u7406\uff0c\u4e0d\u8981\u628a\u6240\u6709\u903b\u8f91\u90fd\u585e\u5728 Node.js \u91cc\u2014\u2014shell \u811a\u672c\u66f4\u5bb9\u6613\u8c03\u8bd5\uff0c\u4e5f\u65b9\u4fbf\u590d\u7528\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u5df2\u7ecf\u7528\u4e0a\u4e86 Hostease \u7684 <a href=\"https:\/\/cn.hostease.com\/vps\/\">VPS<\/a>\uff08<a href=\"https:\/\/cn.hostease.com\/vps\/\">\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668<\/a>\uff09\u6216<a href=\"https:\/\/cn.hostease.com\/dedicated-server\/\">\u72ec\u7acb\u670d\u52a1\u5668<\/a>\uff08\u7269\u7406<a href=\"https:\/\/cn.hostease.com\/dedicated-server\/\">\u4e13\u7528\u670d\u52a1\u5668<\/a>\uff09\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u73b0\u6709\u73af\u5883\u4e0a\u90e8\u7f72\u8fd9\u5957 Webhook \u63a5\u6536\u5668\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5f00\u901a\u670d\u52a1\u3002\u9047\u5230\u7aef\u53e3\u6216\u9632\u706b\u5899\u914d\u7f6e\u95ee\u9898\uff0c\u53ef\u4ee5\u53c2\u8003 Hostease \u7684<a href=\"https:\/\/cn.hostease.com\/blog\/server\/\">\u670d\u52a1\u5668\u8fd0\u7ef4\u6307\u5357<\/a>\u83b7\u53d6\u8be6\u7ec6\u64cd\u4f5c\u6b65\u9aa4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6559\u4f60\u7528 GitHub Webhook \u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u81ea\u52a8\u90e8\u7f72\uff0c\u4ee3\u7801\u63a8\u9001\u5230\u670d\u52a1\u5668\u96f6\u4eba\u5de5\u5e72\u9884\uff0c\u6db5\u76d6\u73af\u5883\u642d\u5efa\u3001\u5b89\u5168\u6821\u9a8c\u4e0e\u591a\u73af\u5883\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":2,"featured_media":31121,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4099],"tags":[4360,4393,4392,3976],"class_list":["post-31123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-cicd","tag-github","tag-webhook","tag-category"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/cn.hostease.com\/blog\/wp-content\/uploads\/2026\/06\/webhook-practical-github-server-cover.webp","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/posts\/31123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/comments?post=31123"}],"version-history":[{"count":1,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/posts\/31123\/revisions"}],"predecessor-version":[{"id":31124,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/posts\/31123\/revisions\/31124"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/media\/31121"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/media?parent=31123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/categories?post=31123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/blog\/wp-json\/wp\/v2\/tags?post=31123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}