新版集成jenkins的打包方案完成

This commit is contained in:
aixianling
2024-10-17 18:12:12 +08:00
parent 91ca77ffa8
commit 6532b66240
3 changed files with 141 additions and 50 deletions

View File

@@ -4,7 +4,7 @@
"description": "node服务端", "description": "node服务端",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"dev": "pm2 restart pm2.config.js --only dev --attach", "dev": "nodemon index.js",
"pro": "pm2 restart pm2.config.js --only oms-node", "pro": "pm2 restart pm2.config.js --only oms-node",
"deploy": "pm2 deploy pm2.config.js production", "deploy": "pm2 deploy pm2.config.js production",
"stop": "pm2 delete all" "stop": "pm2 delete all"

View File

@@ -2,23 +2,50 @@ const db = require("../../utils/dbUitls");
const execute = require("../../tools/exec") const execute = require("../../tools/exec")
const dayjs = require("dayjs") const dayjs = require("dayjs")
const fse = require("fs-extra"); const fse = require("fs-extra");
const axios = require('axios')
module.exports = { module.exports = {
action: "/node/custom/getZip", action: "/node/custom/getZip",
method: "post", method: "post",
execute: (request, response) => { execute: (request, response) => {
let {id} = request.query, uniCon = `id='${id}'` let {id} = request.query, uniCon = `id='${id}'`
db.query(`select * from node_custom_config where id='${id}'`).then(res => { db.query(`select *
let info = res?.[0], sql from node_custom_config
if (info?.id) { where id = '${id}'`).then(res => {
sql = `update node_custom_config set download=null,error=null,zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}' where ${uniCon}` let info = res?.[0] || {}, sql
if (info.id) {
sql = `update node_custom_config
set download=null,
error=null,
zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}'
where ${uniCon}`
if (info.type == 'web') {
const {name, version, dist = `${name}_v${version}`} = info
const buildConfig = {
web: {task: "devops-web", token: 'fLeOGSVIRs405Me'},
}[info.type]
db.query(sql).then(() => axios.get(`https://jenkins.sinoecare.com/view/devops/job/${buildConfig.task}/buildWithParameters`,null,{
params:{
token: buildConfig.token,
pid: id, dist
}
}))
.then(() => db.query(`update node_custom_config
set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}',
error=''
where ${uniCon}`))
.then(() => response.send({code: 0, msg: "打包任务已发送"}))
.catch(err => {
console.log(err)
return fse.outputFile(`./logs/errors/${dayjs().format("YYYY-MM-DD")}.log`, err)
})
} else {
const buildPath = { const buildPath = {
web: 'base-web', web: 'base-web',
wxwork: 'base-wxcp', wxwork: 'base-wxcp',
mp: 'dvcp_v2_wxmp' mp: 'dvcp_v2_wxmp'
}[info.type] || {} }[info.type] || {}
let path = `../${buildPath}`, let path = `../${buildPath}`,
{dist} = info {dist = `../zips/${info.name}v${info.version || "1.0.0"}`} = info
dist = dist || `../zips/${info.name}v${info.version || "1.0.0"}`
Promise.all([ Promise.all([
execute(`./shell/update.sh ${info.name} ${path}`), execute(`./shell/update.sh ${info.name} ${path}`),
db.query(sql) db.query(sql)
@@ -29,11 +56,15 @@ module.exports = {
.then(() => fse.emptyDir(dist)) .then(() => fse.emptyDir(dist))
.then(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`)) .then(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`))
.then(() => { .then(() => {
return db.query(`update node_custom_config set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}',error='' where ${uniCon}`) return db.query(`update node_custom_config
set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}',
error=''
where ${uniCon}`)
}).catch(err => { }).catch(err => {
console.log(err) console.log(err)
return fse.outputFile(`./logs/errors/${dayjs().format("YYYY-MM-DD")}.log`, err) return fse.outputFile(`./logs/errors/${dayjs().format("YYYY-MM-DD")}.log`, err)
}) })
}
} else return response.send({code: 1, err: "无法找到定制项目信息"}) } else return response.send({code: 1, err: "无法找到定制项目信息"})
}).catch(err => { }).catch(err => {
console.log(err) console.log(err)

View File

@@ -2,7 +2,13 @@ const db = require("../../utils/dbUitls");
const execute = require("../../tools/exec") const execute = require("../../tools/exec")
const dayjs = require("dayjs") const dayjs = require("dayjs")
const fse = require("fs-extra"); const fse = require("fs-extra");
const axios = require('axios')
const http = axios.create({
baseURL: 'https://jenkins:114295d0b955e67a18b11917bd281ea9dc@jenkins.sinoecare.com',
})
const jobs = {}
let controller = new AbortController() let controller = new AbortController()
const handleZip = (id, ws) => { const handleZip = (id, ws) => {
const uniCon = `id='${id}'` const uniCon = `id='${id}'`
@@ -37,34 +43,83 @@ const handleZip = (id, ws) => {
} }
} }
db.detail({table: "node_custom_config", id}).then(info => { const startUpdateSql = () => db.query(`update node_custom_config
if (info?.id) { set download=null,
error=null,
zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}'
where ${uniCon}`)
const endUpdateSql = () => db.query(`update node_custom_config
set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}',
error=''
where ${uniCon}`)
const errorUpdateSql = msg => db.query(`update node_custom_config
set error='${msg}',
zipTime=null
where ${uniCon}`)
db.detail({table: "node_custom_config", id}).then((info = {}) => {
if (info.id) {
const {signal} = controller; const {signal} = controller;
const progress = new counter(`正在处理 ${info.name} 的打包工作...`) const progress = new counter(`正在处理 ${info.name} 的打包工作...`)
if (info.type == 'web') {
const {name, version, dist = `${name}_v${version}`} = info
const buildConfig = {
web: {task: "devops-web", token: 'fLeOGSVIRs405Me'},
}[info.type]
startUpdateSql()
.then(() => progress.set(30, id))
.then(() => http.get(`/view/devops/job/${buildConfig.task}/buildWithParameters`,
{params: {token: buildConfig.token, pid: id, dist}}))
.then(() => http.get(`/view/devops/job/${buildConfig.task}/api/json`).then(res => res.data.lastBuild.number))
.then(currentJob => new Promise((resolve, reject) => {
jobs[id] = {task: buildConfig.task, build: currentJob}
const timer = setInterval(() => {
http.get(`/view/devops/job/${buildConfig.task}/${currentJob}/api/json`).then(res => {
if (res.data.result == 'SUCCESS') {
clearInterval(timer)
resolve(endUpdateSql())
} else if (res.data.res == 'ABORTED') {
clearInterval(timer)
reject("构建取消")
} else if (res.data.res == 'FAILURE') {
clearInterval(timer)
reject("构建失败")
}
})
}, 1000)
})).catch(err => {
console.log(err)
const msg = `执行失败:${err.cmd || err}`
return errorUpdateSql(msg)
.then(() => db.detail({table: "node_custom_config", id}))
.then(row => progress.stop(row))
.catch(() => 0)
})
} else {
const buildPath = { const buildPath = {
web: 'base-web', wxwork: 'base-wxcp', mp: 'dvcp_v2_wxmp' web: 'base-web', wxwork: 'base-wxcp', mp: 'dvcp_v2_wxmp'
}[info.type] || {} }[info.type] || {}
let path = `../${buildPath}`, {dist} = info let path = `../${buildPath}`, {dist} = info
dist = dist || `../zips/${info.name}v${info.version || "1.0.0"}` dist = dist || `../zips/${info.name}v${info.version || "1.0.0"}`
Promise.all([db.query(`update node_custom_config set download=null,error=null,zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}' where ${uniCon}`), execute(`./shell/update.sh ${info.name} ${path}`, signal)]) Promise.all([startUpdateSql(), execute(`./shell/update.sh ${info.name} ${path}`, signal)])
.then(cmd => progress.set(30, info?.id)) .then(cmd => progress.set(30, info?.id))
.then(() => execute(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`, signal)) .then(() => execute(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`, signal))
.then(cmd => progress.set(70, info?.id)) .then(cmd => progress.set(70, info?.id))
.then(() => fse.emptyDir(dist)) .then(() => fse.emptyDir(dist))
.then(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`, signal)) .then(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`, signal))
.then(cmd => progress.set(90, info?.id)) .then(cmd => progress.set(90, info?.id))
.then(() => db.query(`update node_custom_config set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}',error='' where ${uniCon}`)) .then(() => endUpdateSql())
.then(() => db.detail({table: "node_custom_config", id})) .then(() => db.detail({table: "node_custom_config", id}))
.then(row => progress.finish(row)) .then(row => progress.finish(row))
.catch(err => { .catch(err => {
console.log(err) console.log(err)
const msg = `执行失败:${err.cmd}` const msg = `执行失败:${err.cmd}`
return db.query(`update node_custom_config set error='${msg}',zipTime=null where ${uniCon}`) return errorUpdateSql(msg)
.then(() => db.detail({table: "node_custom_config", id})) .then(() => db.detail({table: "node_custom_config", id}))
.then(row => progress.stop(row)) .then(row => progress.stop(row))
.catch(() => 0) .catch(() => 0)
}) })
} }
}
}).catch(err => { }).catch(err => {
console.log(err) console.log(err)
}) })
@@ -74,8 +129,13 @@ module.exports = {
action: "/custom/getZip", execute: (ws, request) => { action: "/custom/getZip", execute: (ws, request) => {
const {id, cid} = request const {id, cid} = request
if (cid) { if (cid) {
if (jobs[cid]) {
const {task, build} = jobs[cid]
http.post(`/job/${task}/${build}/stop`)
} else {
controller.abort() controller.abort()
controller = new AbortController() controller = new AbortController()
}
} else if (id) { } else if (id) {
handleZip(id, ws) handleZip(id, ws)
} }