😊 这次自己写了一个基于 nodejs 的自动化部署的工具,因为之前在用 jenkins 由于 jenkins 比较强大,而且比较笨重,对于我来说只是用来部署一个前端 application 而已,所以没必要用这样比较完善的工具,毕竟服务器资源有限。。
😋 附上一个之前写的文章 gitee + webhooks + jenkins 实现自动化部署
原理与 jenkins 类似,也是受益于 jenkins 的启发,自己写了一个 nodejs 部署工具
TIP
🙃 由于本人比较懒,用了 koa
// koa
const Koa = require('koa');
// koa-router
const Router = require('koa-router');
// 实例化 koa
const app = new Koa();
// 实例化 koa-router
const router = new Router();
// 是 child process 的封装
const shell = require('shelljs');
// 基于 nodejs 实现的自动发邮件
const nodemailer = require('nodemailer');
// get response body lib
const bodyParser = require('koa-bodyparser');
// 邮件配置
let transporter = nodemailer.createTransport({
host: 'smtp.exmail.qq.com',
port: 465, // SMTP 端口
secureConnection: true, // 使用了 SSL
auth: {
user: '你要发送 email 的地址',
// smtp授权码
pass: '授权码,不是密码',
}
});
然后开始写自动部署的接口
router.post('/deploy-application', (ctx, next) => {
// 设置跨域
ctx.set('Access-Control-Allow-Origin', '*')
ctx.body = {
message: 'ok',
status: 200,
data: {
test: 'success'
}
}
})
const requestBranch = ctx.request.body.ref
const requestBranchLength = ctx.request.body.ref.lastIndexOf('/') + 1
const branch = requestBranch.slice(requestBranchLength)
if (branch === 'develop') {
// 开发环境部署
ctx.body = 'ok'
shell.exec('yarn run deploy')
}
.sh
文件# 确保抛错
set -e
REF="你的 git 仓库地址"
FOLDER="clone 远程仓库下来的文件夹位置"
DEPLOY_FOLDER="你要部署到服务器的文件夹位置"
rm -rf ${FOLDER}
git clone -b develop ${REF} ${FOLDER}
cd ${FOLDER}
yarn install
yarn run build:develop
rm -rf ${DEPLOY_FOLDER}
cp -R ${FOLDER}dist/${DEPLOY_FOLDER}
let mailOptions = {
from: '"你的邮件" <你的邮件>', // sender address
to: '要接受人的邮件', // list of receivers
subject: '【项目】部署邮件', // Subject line
html: '<b>项目已经部署至<a href="http://xxxx">http://xxx</a></b>' // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
});
app
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods())
app.listen(3000);
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有