赞
踩
本次以egg后端项目关联gitee自动化部署为例子,涉及PM2进程管理工具、WebHooks、自动化脚本sh、nodejs监听端口服务等知识,此外服务器需要node环境。
autoBulid.sh (路径:/var/www/)
cd /var/www/node-egg
npm run stop
git reset --hard origin/master
git clean -f
git pull
npm run start
webhook-server.js (路径:/var/www/)
const http = require('http'); const { exec } = require('child_process'); // 端口号为3000 const PORT = 3000; const server = http.createServer((req, res) => { if (req.method === 'POST' && req.url === '/webhook') { let body = ''; req.on('data', chunk => { body += chunk.toString(); }); req.on('end', () => { const payload = JSON.parse(body); const branch = payload.ref.split('/').pop(); // 获取提交的分支名称 if (branch === 'master') { exec('/path/to/deploy.sh', (error, stdout, stderr) => { if (error) { console.error('Deployment failed:', error); res.writeHead(500); res.end('Deployment failed'); } else { console.log('Deployment successful'); res.writeHead(200); res.end('Deployment successful'); } }); } else { console.log(`Received push event for branch ${branch}. No deployment needed.`); } }); } else { res.writeHead(404); res.end('Not found'); } }); server.listen(PORT, () => { console.log(`Webhook server listening on port ${PORT}`); });
npm install -g pm2
pm2 start webhook-server.js
gitee为例
此时测试结果为 Received push event for branch ${branch}. No deployment needed.
因为测试分支不是master
所以可以直接本地电脑修改master代码,然后更新推送上面,pm2 logs -f
实时查看node监听服务的日志,确认是否执行了autoBulid.sh自动重新部署脚本。Deployment successful
就说明执行了。
思考:
总结:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。