当前位置: 首页 > 后端技术 > Node.js

使用Github的WebHooks进行网站自动部署

时间:2023-04-03 11:44:12 Node.js

原理在使用Github操作仓库时,可以配置webhooks向服务器发送请求。服务器收到请求后,使用脚本自动执行gitpull操作。图片来源:Github的webhook触发vps上的脚本构建Webhook服务。通过执行npmi-ggithub-webhook-handler安装github-webhook-handler中间件。新建文件webhook.jsvarhttp=require('http')varcreateHandler=require('github-webhook-handler')varhandler=createHandler({path:'/',secret:'root'})//以上secret与github后台设置一致functionrun_cmd(cmd,args,callback){varspawn=require('child_process').spawn;varchild=spawn(cmd,args);varresp="";child.stdout.on('data',function(buffer){resp+=buffer.toString();});child.stdout.on('end',function(){callback(resp)});}http.createServer(function(req,res){handler(req,res,function(err){res.statusCode=404res.end('没有这样的位置')})}).listen(7777)handler.on('error',function(err){console.error('Error:',err.message)})handler.on('push',function(event){console.log('收到%s到%s的推送事件',event.payload.repository.name,event.payload.ref);run_cmd('sh',['./deploy.sh',event.payload.repository.name],function(text){console.log(text)});})wherevarhandler=createHandler({path:'/',secret:'root'})秘密字段在Github设置的话,需要注意一下。如果运行时提示github-webhook-handlerisnotdefined,可以在该目录下执行npmlinkgithub-webhook-handler。参考地址:静态网站使用Github的Webhook同一个服务自动部署多个webhook当你有多个仓库需要自动部署时,可以在一个服务上启用多个webhookvarhttp=require('http')varcreateHandler=require('node-github-webhook')varhandler=createHandler([//多个库{path:'/app1',secret:'CUSTOM'},{path:'/app2',secret:'CUSTOM'}])//varhandler=createHandler({path:'/webhook1',secret:'secret1'})//单个库http.createServer(function(req,res){handler(req,res,function(err){res.statusCode=404res.end('nosuchlocation')})}).listen(7777)handler.on('error',function(err){console.error('Error:',err.message)})handler.on('push',function(event){console.log('收到%s到%s的推送事件',event.payload.repository.name,event.payload.ref)switch(event.path){case'/app1':runCmd('sh',['./app1_deploy.sh',event.payload.repository.name],function(text){console.log(text)})breakcase'/app2':runCmd('sh',['./app2_deploy.sh',event.payload.repository.name],function(text){控制台.log(text)})休息默认值://处理其他中断}})functionrunCmd(cmd,args,callback){varspawn=require('child_process').spawnvarchild=spawn(cmd,args)varresp=''child.stdout.on('data',function(buffer){resp+=buffer.toString()})child.stdout.on('end',function(){callback(resp)})}当多个webhooks用于同一服务时,你最终会得到PayloadURLis:http:/yourdomain:7777/app1orhttp:/yourdomain:7777/app2,注意我在实践中发现无法使用/目录,无法监听webhook参考地址:https://github.com/rvagg/github...完成shell脚本使用脚本前需要对网站根目录做一些处理#打开网站根目录cd/home/wwwroot/domain.com#使用Git文件控制gitinit#添加远程Git仓库地址gitremoteaddoriginhttps://xx.git参考地址:如何强制gitpull覆盖本地文件?然后创建deploy.sh,和webhook.js在同一目录下#!/bin/bash#网站根目录WEB_PATH='/home/wwwroot/domain.com'echo"开始部署"cd$WEB_PATHecho"fetchingfromremote..."#为了避免冲突,强制更新本地文件gitfetch--allgitreset--hardorigin/masterecho"done"由于Linux文件权限问题可能无法执行。建议执行chmod777./deploy.sh使用pm2安装processguardpm2:npmipm2-grunwebhook.jspm2startwebhook.js进入Gtihub后台进行设置输入需要的项目的github地址要自动部署添加webhook,进入Settings设置页面,点击左侧Webhooks原地址:UseGithub's-WebHooks-forautomaticwebsitedeployment