jira集成gitlab
1.jira集成gitlab原理解析
实现原理:jira上配置webhook,自动触发jenkins将webhookData(类似于gitlab钩子触发的runOpts)传递到jenkins流水线,根据参数“webhookEvent”判断触发类型(创建/更新/删除)和项目projectId,根据“webhookEvent”和“projectId”作为入参,封装httprequest请求去CreateBranch或CreateMr,所谓jira集成gitlab是通过“jenkins”实现的
1)jira创建项目
2)创建模块
3)配置jira钩子自动触发流水线
4)jenkins上创建jira-devops-service流水线
定义变量
指定jenkinsfile
6)gitlab.groovy共享库
主要jira传递过来的模块==gitlab项目名--->获取projectId,最终实现创建分支,合并分支
package org.devops //封装HTTP请求 def HttpReq(reqType,reqUrl,reqBody){ def gitServer = "http://192.168.164.158/api/v4" withCredentials([string(credentialsId: 'gitlab-token', variable: 'gitlabToken')]) { result = httpRequest customHeaders: [[maskValue: true, name: 'PRIVATE-TOKEN', value: "${gitlabToken}"]], httpMode: reqType, contentType: "APPLICATION_JSON", consoleLogResponseBody: true, ignoreSslErrors: true, requestBody: reqBody, url: "${gitServer}/${reqUrl}" //quiet: true } return result } //获取项目ID def GetProjectID(repoName='',projectName){ projectApi = "projects?search=${projectName}" response = HttpReq('GET',projectApi,'') def result = readJSON text: """${response.content}""" for (repo in result){ // println(repo['path_with_namespace']) if (repo['path'] == "${projectName}"){ repoId = repo['id'] println(repoId) } } return repoId } //创建分支 def CreateBranch(projectId,refBranch,newBranch){ try { branchApi = "projects/${projectId}/repository/branches?branch=${newBranch}&ref=${refBranch}" response = HttpReq("POST",branchApi,'').content branchInfo = readJSON text: """${response}""" } catch(e){ println(e) } //println(branchInfo) } //创建合并请求 def CreateMr(projectId,sourceBranch,targetBranch,title,assigneeUser=""){ try { def mrUrl = "projects/${projectId}/merge_requests" def reqBody = """{"source_branch":"${sourceBranch}", "target_branch": "${targetBranch}","title":"${title}","assignee_id":"${assigneeUser}"}""" response = HttpReq("POST",mrUrl,reqBody).content return response } catch(e){ println(e) } }
7)jira.jenkinsfile调用
#!groovy @Library('jenkinslib')_ def tools = new org.devops.tools() def gitlab = new org.devops.gitlab() def jira = new org.devops.jira() //def k8s = new org.devops.kubernetes() pipeline { agent any triggers { GenericTrigger( causeString: 'Trigger By Jira Server -->>>>> Generic Cause', genericRequestVariables: [[key: 'projectKey', regexpFilter: '']], genericVariables: [[defaultValue: '', key: 'webHookData', regexpFilter: '', value: '$']], printContributedVariables: true, printPostContent: true, regexpFilterExpression: '', regexpFilterText: '', silentResponse: true, token: "${JOB_NAME}" ) } stages{ stage("FileterData"){ steps{ script{ response = readJSON text: """${webHookData}""" ///数据解析,jenkins收到jira webHook数据进行解析 tools.PrintMes("jira数据解析","green") println(response) env.eventType = response["webhookEvent"] //根据eventType类型来获取不同参数 switch(eventType) { case "jira:version_created": env.versionName = response["version"]["name"] currentBuild.description = " Trigger by ${eventType} ${versionName} " break case ["jira:issue_created" , "jira:issue_updated" ]: env.issueName = response['issue']['key'] env.userName = response['user']['name'] env.moduleNames = response['issue']['fields']['components'] env.fixVersion = response['issue']['fields']['fixVersions'] currentBuild.description = " Trigger by ${userName} ${eventType} ${issueName} " break case "jira:version_released": env.versionName = response["version"]["name"] currentBuild.description = " Trigger by ${eventType} ${versionName} " break default: println("hello") } } } } stage("CreateBranchOrMR"){ when { anyOf { environment name: 'eventType', value: 'jira:issue_created' //issue 创建 /更新 environment name: 'eventType', value: 'jira:issue_updated' } } steps{ script{ tools.PrintMes("CreateBranchOrMR","green") def projectIds = [] println(issueName) fixVersion = readJSON text: """${fixVersion}""" println(fixVersion.size()) //获取项目Id def projects = readJSON text: """${moduleNames}""" for ( project in projects){ println(project["name"]) projectName = project["name"] currentBuild.description += "\n project: ${projectName}" groupName = projectName.split("-")[0] try { projectId = gitlab.GetProjectID(groupName, projectName) println(projectId) projectIds.add(projectId) } catch(e){ println(e) println("未获取到项目ID,请检查模块名称!") } } println(projectIds) if (fixVersion.size() == 0) { for (id in projectIds){ println("新建特性分支--> ${id} --> ${issueName}") currentBuild.description += "\n 新建特性分支--> ${id} --> ${issueName}" gitlab.CreateBranch(id,"master","${issueName}") } } else { fixVersion = fixVersion[0]['name'] println("Issue关联release操作,Jenkins创建合并请求") currentBuild.description += "\n Issue关联release操作,Jenkins创建合并请求 \n ${issueName} --> RELEASE-${fixVersion}" for (id in projectIds){ println("创建RELEASE-->${id} -->${fixVersion}分支") gitlab.CreateBranch(id,"master","RELEASE-${fixVersion}") println("创建合并请求 ${issueName} ---> RELEASE-${fixVersion}") gitlab.CreateMr(id,"${issueName}","RELEASE-${fixVersion}","${issueName}--->RELEASE-${fixVersion}") } } } } } } }
2.自动创建分支
1)jira创建任务
2)自动触发jenkins流水线
3)自动创建分支
3.自动合并分支
实现原理:待特性分支验证没问题后,jira上选定修复版本,流水线实现如下:
gitlab上触发创建release分支;
特性分支合并到release分支;
1)创建release发布版本
2)特性分支DEV4关联发布版本
3)自动触发流水线构建
4)gitlab分支检查
至此:实现了jira上创建问题 --->gitlab自动创建特性分支
jira问题关联发布版本--->gitlab自动创建版本分支(有就不创建幂等性),且特性分支自动合并到版本分支
作者:杰宏唯一
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.