V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
YepW
V2EX  ›  问与答

问大伙个 cicd 的问题, 如何用 node 写了一个类似 github 的 webhook 的回调用来触发构建部署?我用 gpt 生成的代码一直不生效

  •  
  •   YepW · 237 天前 · 458 次点击
    这是一个创建于 237 天前的主题,其中的信息可能已经有所发展或是发生改变。

    git 仓库平台用的是阿里云效

    我把脚本在服务器上执行后,测试 webhook 一直失败,网上也没查到有用的解决办法

    云效的 webhook 配置: url: http://服务器 ip:2333/build secret: xxxx

    下面是 gpt 生成的脚本

    const http = require('http');
    const { exec } = require('child_process');
    
    const PORT = 2333;
    const WEBHOOK_SECRET = 'xxxx';
    
    const server = http.createServer((req, res) => {
      if (req.method === 'POST' && req.url === '/build') {
        let body = '';
    
        req.on('data', (chunk) => {
          body += chunk;
        });
    
        req.on('end', () => {
          const codeupEventHeader = req.headers['codeup-event'];
          const xCodeupToken = req.headers['x-codeup-token'];
    
          if (codeupEventHeader === 'Push Hook' && xCodeupToken === WEBHOOK_SECRET) {
            let repoName = '';
            try {
              const data = JSON.parse(body);
              repoName = data.repository.name;
            } catch (error) {
              console.error('无法解析 JSON 数据:', error);
              res.statusCode = 400;
              res.end('无效的 JSON 数据');
              return;
            }
    
            switch (repoName) {
              case 'fuex-merchant':
                exec('sudo chmod -R 777 /opt/fuex-frontend/fuex-merchant/ && sudo chmod -R 777 /var/www/fuex-frontend/merchant && sudo /opt/fuex-frontend/fuex-merchant/build-prod.sh', (error, stdout, stderr) => {
                  if (error) {
                    console.error('构建失败:', error);
                    res.statusCode = 500;
                    res.end('构建失败');
                  } else {
                    console.log('构建成功');
                    res.statusCode = 200;
                    res.end('构建成功');
                  }
                });
                break;
              default:
                console.error('未知的仓库名称:', repoName);
                res.statusCode = 400;
                res.end('未知的仓库名称');
                break;
            }
          } else {
            console.error('无效的请求');
            res.statusCode = 400;
            res.end('无效的请求');
          }
        });
      } else {
        res.statusCode = 404;
        res.end('Not Found');
      }
    })
    server.listen(PORT, '0.0.0.0', () => {
      console.log(`Webhook 服务器运行在端口 ${PORT}`);
    })
    
    
    
    2 条回复    2023-09-23 18:48:25 +08:00
    musi
        1
    musi  
       236 天前
    建议你在问 gpt 的时候加个 express
    julyclyde
        2
    julyclyde  
       233 天前
    1 是否产生了调用
    2 调用的参数是否正确

    这俩问题是 github 这边的
    下面问题是你这边的

    3 对于正确的参数,你的回调服务能否正常工作

    按边界断开,分别调试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5912 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:46 · PVG 14:46 · LAX 23:46 · JFK 02:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.