目录
[2.GitLab 查看项目](#2.GitLab 查看项目)
[4. Jira 通过Webhook 触发Jenkins流水线](#4. Jira 通过Webhook 触发Jenkins流水线)
[3.Jira 远程触发 Jenkins 实现更新 GitLab 分支](#3.Jira 远程触发 Jenkins 实现更新 GitLab 分支)
[1.Jira 配置网络钩子失败](#1.Jira 配置网络钩子失败)
[2. Jira 远程触发Jenkins 报错](#2. Jira 远程触发Jenkins 报错)
一、实验
1.环境
(1)主机
表1 主机
|---------|--------------|----------|----------------------|------------------------------------------------------------------------------------------------|
| 主机 | 架构 | 版本 | IP | 备注 |
| master1 | K8S master节点 | 1.20.6 | 192.168.204.180 | jenkins slave (从节点) |
| | jira | 9.12.1 | 192.168.204.180:8801 | |
| node1 | K8S node节点 | 1.20.6 | 192.168.204.181 | |
| node2 | K8S node节点 | 1.20.6 | 192.168.204.182 | |
| jenkins | jenkins主节点 | 2.414.2 | 192.168.204.15:8080 | gitlab runner (从节点) |
| gitlab | gitlab 主节点 | 12.10.14 | 192.168.204.8:82 | jenkins slave (从节点) |
| | sonarqube | 9.6 | 192.168.204.8:9000 | |
(2)查看K8集群状态
bash
# kubectl get node
![](https://file.jishuzhan.net/article/1738549656528359426/2f35129ad4616419249723193c456fae.webp)
2.GitLab 查看项目
(1) GitLab查看后端项目(项目编号为19)
![](https://file.jishuzhan.net/article/1738549656528359426/bfad221cf0859afdc4d8195413c4449a.webp)
(2)GitLab查看前端项目(项目编号为20)
![](https://file.jishuzhan.net/article/1738549656528359426/77dd8e9444ef16ff5d3b9f236af11590.webp)
3.Jira新建模块
(1)查询已有模块(前端项目)
![](https://file.jishuzhan.net/article/1738549656528359426/93b1e3b82e21b1af87c1ca5e767f0dd1.webp)
(2)新建后端项目模块
![](https://file.jishuzhan.net/article/1738549656528359426/43f7a31ae54f9f7c98e951f86235f2b6.webp)
(3)再次查看模块
![](https://file.jishuzhan.net/article/1738549656528359426/475a0cc8567b5636c240266d016fe3cc.webp)
4. Jira 通过Webhook 触发Jenkins流水线
(1)Jenkins新建流水线
![](https://file.jishuzhan.net/article/1738549656528359426/292adf79a4bd4411b9e059e790b99665.webp)
(2)配置触发器
![](https://file.jishuzhan.net/article/1738549656528359426/b8f34a9c77e27544dd654b2196c9f9f6.webp)
![](https://file.jishuzhan.net/article/1738549656528359426/5ddd846f214e1d7fb7732e60e83af131.webp)
(3)拿到Webhook 触发地址
bash
http://JENKINS_URL/generic-webhook-trigger/invoke
![](https://file.jishuzhan.net/article/1738549656528359426/ebbd98e0f5f7586b42a43d420d3090a4.webp)
(4)拿到携带TOKEN的请求参数
bash
/invoke?token=TOKEN_HERE
![](https://file.jishuzhan.net/article/1738549656528359426/be672ed1853c05a61f5d3e740b992ab3.webp)
(5)Jira配置网络钩子
![](https://file.jishuzhan.net/article/1738549656528359426/83587c3f1e546c750c5cc57aec172aec.webp)
(6)完成创建
![](https://file.jishuzhan.net/article/1738549656528359426/949d1cb262dd6eb3f4127631808d2bc9.webp)
(7)新建问题
![](https://file.jishuzhan.net/article/1738549656528359426/61bd4d99ffad1843c75c89dabe6b3f89.webp)
(8)Jenkins流水线成功运行
![](https://file.jishuzhan.net/article/1738549656528359426/bd0b92d83a393d08a56decb80a353f9a.webp)
![](https://file.jishuzhan.net/article/1738549656528359426/c1dcc46b711e256375a179df529a1758.webp)
(9)再次新建问题
![](https://file.jishuzhan.net/article/1738549656528359426/5cbe26e81fb2c7cd75933f5e79d20f8f.webp)
(10)Jenkins流水线成功运行
(11)修改Jenkins流水线配置
bash
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo "${webhookData}"
}
}
}
}
(12)拿到webhook数据
![](https://file.jishuzhan.net/article/1738549656528359426/a5b1afa7c44a749c9530b6333cc7a118.webp)
(13)JSON转码
![](https://file.jishuzhan.net/article/1738549656528359426/b9fede9485a9da0cf6b2610e9d820147.webp)
(14)拿到关键数据
bash
1)用于创建gitlab 项目名称
issue.fields.components
2) 用于gitlab 分支名称
issue.key
3)用于gitlab 项目组名称
issue.fields.project.name
![](https://file.jishuzhan.net/article/1738549656528359426/e1940ccfac5d5415446d347636aff266.webp)
(15)Postman测试,获取项目id
bash
http://192.168.204.8:82/api/v4/projects?search=devops03-devops-service
![](https://file.jishuzhan.net/article/1738549656528359426/38b2e5985f70fd1ce84a27dd758da018.webp)
(16)Postman测试创建分支
bash
http://192.168.204.8:82/api/v4/projects/20/repository/branches?branch=newbranch&ref=master
![](https://file.jishuzhan.net/article/1738549656528359426/2a908bab6009673744e79b26cae48792.webp)
(17)GitLab前端项目成功创建分支
![](https://file.jishuzhan.net/article/1738549656528359426/02ee16ce14ac5f7978d255f7e9d31570.webp)
3.Jira 远程触发 Jenkins 实现更新 GitLab 分支
(1)Jenkins修改流水线代码
bash
webhookData = readJSON text: "${webhookData}"
//jira 事件
jiraEvent = webhookData.webhookEvent
jiraProjectName = webhookData.issue.fields.project.name
// 获取gitlab参数
gitlabProjects = []
gitlabBranchName = webhookData.issue.key
gitlabGroupName = jiraProjectName
for (i in webhookData.issue.fields.components){
gitlabProjects.add(i["name"])
}
//描述信息
currentBuild.description = "Trigger by ${jiraEvent} \n project: ${gitlabProjects} \n branch: ${gitlabBranchName}"
pipeline {
agent { label "build" }
stages {
stage("Process") {
steps {
script {
println(gitlabProjects)
println(gitlabBranchName)
projectIds = GetProjectsId(gitlabGroupName,gitlabProjects)
switch(jiraEvent) {
case "jira:issue_created":
println(projectIds)
for (id in projectIds){
CreateBranch(id,gitlabBranchName,"master")
}
break
default:
println(error)
break
}
}
}
}
}
}
// 创建分支
def CreateBranch(projectId,newBranchName,sourceBranchName){
apiUrl = "projects/${projectId}/repository/branches?branch=${newBranchName}&ref=${sourceBranchName}"
response = HttpReq('POST', apiUrl, "")
}
// 获取所有项目id
def GetProjectsId(gitlabGroupName,gitlabProjects){
gitlabProjectIds = []
for (project in gitlabProjects){
id = GetProjectId(gitlabGroupName,project)
if (id != 0){
gitlabProjectIds.add(id)
}
}
return gitlabProjectIds
}
// 根据项目名称获取项目id
def GetProjectId(groupName,projectName){
apiUrl = "projects?search=${projectName}"
response = HttpReq('GET', apiUrl, "")
response = readJSON text: response.content - "\n"
for (i in response){
if (i["path_with_namespace"] == "${groupName}/${projectName}"){
return i["id"]
}
}
}
// 封装HTTP
def HttpReq(reqType, reqUrl,reqBody ){
def gitServer = "http://192.168.204.8:82/api/v4"
withCredentials([string(credentialsId: '02dce3ff-4e46-4de2-b079-5dd6093d4f64', variable: 'GITLABTOKEN')]) {
response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',
consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON_UTF8',
customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: "${GITLABTOKEN}"]],
httpMode: "${reqType}",
url: "${gitServer}/${reqUrl}",
wrapAsMultipart: false,
requestBody: "${reqBody}"
}
return response
}
![](https://file.jishuzhan.net/article/1738549656528359426/943a614c8574592f4fa490a44a637ca1.webp)
(2) Jira 新建问题更新前端项目分支
![](https://file.jishuzhan.net/article/1738549656528359426/336cbfd7fe3567d5db1177ade95cf05d.webp)
(3)Jenkins运行成功,出现相关描述信息
![](https://file.jishuzhan.net/article/1738549656528359426/f88c7c065daeed0d8388f5fb4cee49cc.webp)
![](https://file.jishuzhan.net/article/1738549656528359426/fab192dd969f53e2d6fa311cbe47adc9.webp)
(4)GitLab查看前端项目新增分支
![](https://file.jishuzhan.net/article/1738549656528359426/19728c88b3eb39d00cc544f30844aeef.webp)
(5)Jira 新建问题,实现同时更新前后端项目分支
(6)成功触发Jenkins流水线
![](https://file.jishuzhan.net/article/1738549656528359426/16737d74e8adea9442ebdfdf0633147d.webp)
(7)GitLab查看前端项目新增分支
![](https://file.jishuzhan.net/article/1738549656528359426/9b8b0b853ed3c2673e71b2d98a71fa1a.webp)
(8)GitLab查看后端项目新增分支
![](https://file.jishuzhan.net/article/1738549656528359426/777bc82b9acfa7cf8279eea7179cdf22.webp)
(9)优化Jenkins流水线代码,防止GitLab 同一项目fork问题
bash
webhookData = readJSON text: "${webhookData}"
//jira 事件
jiraEvent = webhookData.webhookEvent
jiraProjectName = webhookData.issue.fields.project.name
// 获取gitlab参数
gitlabProjects = []
gitlabBranchName = webhookData.issue.key
gitlabGroupName = jiraProjectName
for (i in webhookData.issue.fields.components){
gitlabProjects.add(i["name"])
}
//描述信息
currentBuild.description = "Trigger by ${jiraEvent} \n project: ${gitlabProjects} \n branch: ${gitlabBranchName}"
pipeline {
agent { label "build" }
stages {
stage("Process") {
steps {
script {
println(gitlabProjects)
println(gitlabBranchName)
projectIds = GetProjectsId(gitlabGroupName,gitlabProjects)
switch(jiraEvent) {
case "jira:issue_created":
println(projectIds)
for (id in projectIds){
CreateBranch(id,gitlabBranchName,"master")
}
break
default:
println(error)
break
}
}
}
}
}
}
// 创建分支
def CreateBranch(projectId,newBranchName,sourceBranchName){
try {
apiUrl = "projects/${projectId}/repository/branches?branch=${newBranchName}&ref=${sourceBranchName}"
response = HttpReq('POST', apiUrl, "")
}
catch(Exception e){
println(e)
}
}
// 获取所有项目id
def GetProjectsId(gitlabGroupName,gitlabProjects){
gitlabProjectIds = []
for (project in gitlabProjects){
id = GetProjectId(gitlabGroupName,project)
if (id != 0){
gitlabProjectIds.add(id)
}
}
return gitlabProjectIds
}
// 根据项目名称获取项目id
def GetProjectId(groupName,projectName){
apiUrl = "projects?search=${projectName}"
response = HttpReq('GET', apiUrl, "")
response = readJSON text: response.content - "\n"
if (response.size() > 1){
for (i in response){
if (i["path_with_namespace"] == "${groupName}/${projectName}"){
return i["id"]
}
}
}else {
return response[0]["id"]
}
}
// 封装HTTP
def HttpReq(reqType, reqUrl,reqBody ){
def gitServer = "http://192.168.204.8:82/api/v4"
withCredentials([string(credentialsId: '02dce3ff-4e46-4de2-b079-5dd6093d4f64', variable: 'GITLABTOKEN')]) {
response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',
consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON_UTF8',
customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: "${GITLABTOKEN}"]],
httpMode: "${reqType}",
url: "${gitServer}/${reqUrl}",
wrapAsMultipart: false,
requestBody: "${reqBody}"
}
return response
}
![](https://file.jishuzhan.net/article/1738549656528359426/358c2b1e409f7ce352f937f269a753b0.webp)
(10)Jira 再次新建问题,实现同时更新前后端项目分支
(11)成功
![](https://file.jishuzhan.net/article/1738549656528359426/ea75155346dd60a920fb42b862de7b95.webp)
(12)GitLab查看前端项目新增分支
![](https://file.jishuzhan.net/article/1738549656528359426/12ac773bae850994ed692e7d92334566.webp)
(13)GitLab查看后端项目新增分支
![](https://file.jishuzhan.net/article/1738549656528359426/c66f0a0e49d3d9c9fdeedf0012ab49aa.webp)
二、问题
1.Jira 配置网络钩子失败
(1)报错
Jira新建问题,Jenkins未自动运行流水线。
![](https://file.jishuzhan.net/article/1738549656528359426/b68e6e88e68823fe3ae0cadad70ba69f.webp)
(2)原因分析
选项错误。
(3)解决方法
修改Jira 网络钩子选项。
修改前:
![](https://file.jishuzhan.net/article/1738549656528359426/1c6b38771e3661a27c2a8d5adac4e46b.webp)
![](https://file.jishuzhan.net/article/1738549656528359426/e0c2818429111b2a8ede18e13020c8ef.webp)
![](https://file.jishuzhan.net/article/1738549656528359426/83eae73d53112f6ce092eaad07be570f.webp)
修改后:
![](https://file.jishuzhan.net/article/1738549656528359426/252718fafcec24c4ec5b32248784a376.webp)
![](https://file.jishuzhan.net/article/1738549656528359426/f96a6145239cec43f146841dd031162a.webp)
![](https://file.jishuzhan.net/article/1738549656528359426/949d1cb262dd6eb3f4127631808d2bc9.webp)
2. Jira 远程触发Jenkins 报错
(1)报错
![](https://file.jishuzhan.net/article/1738549656528359426/10f61abc6b0a26ddfb4557723a3d4df4.webp)
![](https://file.jishuzhan.net/article/1738549656528359426/223ae610aaec0232f8330e2d8060e8c5.webp)
(2)原因分析
代码错误。
(3)解决方法
修改前:
![](https://file.jishuzhan.net/article/1738549656528359426/881504678562385c670d2f7209252b46.webp)
修改后:
![](https://file.jishuzhan.net/article/1738549656528359426/b896ba5c9fcba8585bb7651197793fe6.webp)