You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pterodactyl VPS实现Git Push后自动拉取并重启Discord Bot

在Pterodactyl上实现Node.js Discord Bot代码推送后自动重启的方案

方案一:优化启动命令+Git自动拉取

这是最直接的无额外服务方案,通过调整面板启动命令实现代码拉取+启动联动:

  • 配置容器内Git权限:
    1. 进入Pterodactyl服务器的「Console」,执行ssh-keygen -t ed25519生成SSH密钥(一路回车用默认设置即可)。
    2. 执行cat ~/.ssh/id_ed25519.pub复制公钥,添加到GitHub仓库的「Settings」->「Deploy keys」中,仅拉取的话无需勾选「Allow write access」。
  • 修改启动命令:将原命令node index.js替换为git pull origin <你的分支名> && node index.js,例如git pull origin main && node index.js。
  • 验证:每次启动服务时,面板会先拉取最新代码再启动Bot,手动触发一次即可测试效果。

方案二:Webhook + Pterodactyl API 触发自动更新重启

如果需要代码推送后立即触发更新而非等待启动,可通过Webhook结合面板API实现:

  1. 生成Pterodactyl API密钥:
    • 登录面板,进入「Account Settings」->「API Credentials」,点击「Create New」,权限勾选「Servers -> Control -> Start/Stop/Restart」和「Servers -> Files -> Pull from Repository」。
    • 保存生成的密钥及面板API地址(格式如https://你的面板域名/api)。
  2. 部署Webhook服务:
    写一个轻量Node.js服务(和Bot同服务器部署即可),示例代码:
    const express = require('express');
    const crypto = require('crypto');
    const axios = require('axios');
    
    const app = express();
    const PORT = 3000;
    const WEBHOOK_SECRET = '你自定义的GitHub Webhook密钥';
    const PTERO_API_KEY = '你的Pterodactyl API密钥';
    const SERVER_ID = '你的服务器ID';
    const PTERO_BASE_URL = 'https://你的面板域名/api/client/servers';
    
    // 解析GitHub Webhook的JSON数据并保留原始body用于签名验证
    app.use(express.json({ verify: (req, res, buf) => { req.rawBody = buf; } }));
    
    app.post('/webhook', (req, res) => {
      // 验证GitHub请求合法性,防止恶意调用
      const signature = req.headers['x-hub-signature-256'];
      const hmac = crypto.createHmac('sha256', WEBHOOK_SECRET);
      const digest = `sha256=${hmac.update(req.rawBody).digest('hex')}`;
      if (!crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(digest))) {
        return res.status(403).send('Invalid signature');
      }
    
      // 触发Pterodactyl拉取代码+重启流程
      const pullAndRestart = async () => {
        try {
          // 拉取最新代码
          await axios.post(`${PTERO_BASE_URL}/${SERVER_ID}/pull`, {}, {
            headers: { 'Authorization': `Bearer ${PTERO_API_KEY}`, 'Content-Type': 'application/json' }
          });
          // 重启服务器
          await axios.post(`${PTERO_BASE_URL}/${SERVER_ID}/power`, { signal: 'restart' }, {
            headers: { 'Authorization': `Bearer ${PTERO_API_KEY}`, 'Content-Type': 'application/json' }
          });
          res.status(200).send('Update triggered');
        } catch (err) {
          console.error(err);
          res.status(500).send('Failed to trigger update');
        }
      };
    
      pullAndRestart();
    });
    
    app.listen(PORT, () => console.log(`Webhook server running on port ${PORT}`));
    
    • 安装依赖:npm install express axios crypto,用pm2托管服务避免中断:pm2 start webhook.js --name discord-bot-webhook。
  3. 配置GitHub Webhook:
    • 进入仓库「Settings」->「Webhooks」->「Add webhook」,Payload URL填http://你的VPS IP:3000/webhook(有域名可用域名),Content type选application/json,Secret填自定义密钥,触发事件选「Just the push event」,点击「Add webhook」完成配置。

方案三:利用Pterodactyl Egg自定义脚本

如果面板使用自定义Egg,可修改Egg配置实现自动更新:

  • 进入面板「Nests」->「你的Egg」->「Edit Egg」,找到「Startup Command」或「Script」区域,添加Git拉取逻辑,示例:
    #!/bin/bash
    git pull origin main
    node index.js
    
  • 确保容器内已提前配置好Git SSH密钥(同方案一步骤)。

注意事项

  • 确认容器内Git版本足够新,避免拉取失败。
  • 服务器ID可在Pterodactyl服务器的「Settings」->「General」中找到。
  • 若使用防火墙,需开放Webhook服务的端口(如3000),确保GitHub能正常访问。

内容的提问来源于stack exchange,提问作者Kama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 04:40:37