Commit 220b9128 authored by 智勇's avatar 智勇

优化保存订阅触发jenkins逻辑

parent c1c203c9
...@@ -143,30 +143,40 @@ const getBuildInfoByHooks = async (ctx) => { ...@@ -143,30 +143,40 @@ const getBuildInfoByHooks = async (ctx) => {
ctx.body = ctx.ok(pipePush) ctx.body = ctx.ok(pipePush)
} }
const getMaster = async function (ctx) { const gitBranch = async (svcName) => {
const { name, gitlabAddress } = ctx.request.body const proConfig = await ProConfig.getOneProject({ project_name: svcName }, { git_lab: 1 })
let projectId const version = proConfig.git_lab === 'gitabc.xyqb.com' ? 'v3' : 'v4'
const version = gitlabAddress === 'gitabc.xyqb.com' ? 'v3' : 'v4'
const token = config.gitlab.token const token = config.gitlab.token
const data = await awaitRequest({ const data = await awaitRequest({
url: `http://${gitlabAddress}/api/${version}/projects?per_page=200&search=${name}&private_token=${token}`, url: `http://${proConfig.git_lab}/api/${version}/projects?per_page=200&search=${svcName}&private_token=${token}`,
method: 'GET', method: 'GET',
}) })
if (data.length === 0) { if (data.length === 0) {
ctx.body = ctx.fail('无拉去此项目的权限') return data
return
} }
let projectId
data.forEach((item) => { data.forEach((item) => {
// 再次筛选下数据 因为gitlab返回的数据是模糊查询 // 再次筛选下数据 因为gitlab返回的数据是模糊查询
if (item.name === name || item.path === name) { if (item.name === svcName || item.path === svcName) {
projectId = item.id projectId = item.id
} }
}) })
const dataMaster = await awaitRequest({ const branchInfo = await awaitRequest({
url: `http://${gitlabAddress}/api/${version}/projects/${projectId}/repository/branches?private_token=${token}&per_page=100`, url: `http://${proConfig.git_lab}/api/${version}/projects/${projectId}/repository/branches?private_token=${token}&per_page=100`,
method: 'GET', method: 'GET',
}) })
ctx.body = ctx.ok(dataMaster) return branchInfo
}
const getGitBranch = async function (ctx) {
const { name } = ctx.request.body
const branchInfo = await gitBranch(name)
if (branchInfo.length === 0) {
ctx.body = ctx.fail('无拉去此项目的权限')
} else {
ctx.body = ctx.ok(branchInfo)
}
} }
const findPipes = async function (ctx) { const findPipes = async function (ctx) {
...@@ -174,21 +184,47 @@ const findPipes = async function (ctx) { ...@@ -174,21 +184,47 @@ const findPipes = async function (ctx) {
ctx.body = ctx.ok(data) ctx.body = ctx.ok(data)
} }
const getImageData = async (svcName, cluster) => {
const proConfig = await ProConfig.getOneProject({ project_name: svcName }, { type: 1 })
const params = { reponame: `qa-${proConfig.type}/${svcName}` }
const imageData = await awaitRequest({
url: `${config.api.tke_api}/tag`,
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8',
cluster: cluster || 'qa',
},
body: JSON.stringify(params),
})
return imageData.data
}
const save = async function (ctx) { const save = async function (ctx) {
const data = ctx.request.body const data = ctx.request.body
await PipeLine.savePipes(data) await PipeLine.savePipes(data)
logger.info('save pipeline application', data) logger.info('保存订阅 数据', data)
for (const item of data.repos) { for (const item of data.repos) {
const buildData = { let branchInfo = await gitBranch(item.repository)
projectName: item.repository, branchInfo = branchInfo.filter(i => i.name === item.ref.name)
branchName: item.ref.name, if (branchInfo.length > 0) {
type: item.type, let imageData = await getImageData(item.repository, data.cluster)
gitUser: data.update_user || data.new_user, imageData = imageData.tagInfo.filter(i => i.tagName.indexOf(item.ref.name.split('-')[0]) !== -1)
commitMes: 'save pipeline application', // 如果还没有当前分支的镜像,或者提交时间比镜像制作时间晚时,触发jenkins
if (imageData.length === 0 || branchInfo[0].commit.committed_date.replace('T', ' ') > imageData[0].updateTime) {
const buildData = {
projectName: item.repository,
branchName: item.ref.name,
type: item.type,
gitUser: data.update_user || data.new_user,
commitMes: 'save pipeline application',
}
pipelineJenkinsBuild(buildData)
logger.info('保存订阅 触发 pipeline jenkins build', data.application_name, buildData)
await sleep(1 * 1000)
} else {
logger.info('保存订阅时 已经有更新的镜像 不触发 pipeline jenkins build', data.application_name, item.repository, item.ref.name)
}
} }
pipelineJenkinsBuild(buildData)
logger.info('保存订阅 触发 pipeline jenkins build', data.application_name, buildData)
await sleep(2 * 1000)
} }
ctx.body = ctx.ok('ok') ctx.body = ctx.ok('ok')
} }
...@@ -298,7 +334,7 @@ const router = new Router() ...@@ -298,7 +334,7 @@ const router = new Router()
router router
.use(bodyParser()) .use(bodyParser())
.post('/save', save) .post('/save', save)
.post('/master', getMaster) .post('/master', getGitBranch)
.post('/find', findPipes) .post('/find', findPipes)
.post('/webhooks', webhooks) .post('/webhooks', webhooks)
.post('/delete', deletePipes) .post('/delete', deletePipes)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment