Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qa-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
qa-api
Commits
220b9128
Commit
220b9128
authored
Dec 18, 2019
by
智勇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化保存订阅触发jenkins逻辑
parent
c1c203c9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
22 deletions
+58
-22
pipeline.js
app/controller/pipeline.js
+58
-22
No files found.
app/controller/pipeline.js
View file @
220b9128
...
@@ -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=
${
n
ame
}
&private_token=
${
token
}
`
,
url
:
`http://
${
proConfig
.
git_lab
}
/api/
${
version
}
/projects?per_page=200&search=
${
svcN
ame
}
&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
===
n
ame
)
{
if
(
item
.
name
===
svcName
||
item
.
path
===
svcN
ame
)
{
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
'
,
get
Master
)
.
post
(
'
/master
'
,
get
GitBranch
)
.
post
(
'
/find
'
,
findPipes
)
.
post
(
'
/find
'
,
findPipes
)
.
post
(
'
/webhooks
'
,
webhooks
)
.
post
(
'
/webhooks
'
,
webhooks
)
.
post
(
'
/delete
'
,
deletePipes
)
.
post
(
'
/delete
'
,
deletePipes
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment