开源端到端流水线实践-需求与代码管理

业务的简称为demo,微服务架构。N多个微服务。服务命名:业务简称-应用名称-类型(demo-hello-service)。特性分支开发,版本分支发布。每个需求(任务/故事)对应一个特性分支。每个发布(release)对应一个版本分支。

1.需求与代码管理
Jira作为需求和缺陷管理,采用Scrum开发方法,jira中的项目名称与业务简称一致(demo)。Gitlab作为版本控制系统,每个Group对应一个业务,每个微服务对应一个代码库。

需求与代码关联:在jira中创建一个任务/故事,关联模块后自动在该模块创建一个以ISSUE(任务/故事)ID的特性分支。此时的模块等同于每个微服务的项目(代码库)名称。以下面图中为例:我们在demo项目中创建了一个模块demo-hello-service,其实对应的就是Gitlab代码库中demo组的demo-hello-service服务。

特性分支:创建好每个模块后,就可以实现需求与代码关联。例如:我们在Jira项目demo中创建一个问题,类型为故事(不受限制可为其他),重点是需要将改故事关联到模块(只有关联到模块,我们才能通过接口得知哪个问题关联的哪个代码库)。

版本分支:当特性分支开发完成以及测试验证完成后,基于主干分支创建一个版本分支,然后将所有的特性分支合并到版本分支。此时可以通过Jira中创建一个发布版本,然后问题关联发布版本(此动作表示该特性分支已经通过验证,可以合并)。自动完成版本分支的创建和特性分支到版本分支的合并请求。

2. 配置过程
需求与代码库关联,主要用到的工具链为: Jira + GitLab + Jenkins。Jira负责创建需求,配置webhook。Jenkins负责接收Jira webhook请求,然后通过接口实现GitLab项目分支创建。

特性分支自动化:当我们在jira上面创建了问题,此时会通过Jira的webhook触发对应的Jenkins作业,该Jenkins作业通过解析Jira webhook传递的数据,找到问题名称和模块名称。调用GitlabAPI 项目查询接口,根据模块名称找到代码库。调用GitLabAPI 分支创建接口,根据问题名称基于主干分支创建一个特性分支。任务结束。

版本分支自动化:Jira创建发布版本,Issue关联版本。自动在gitlab代码库基于master创建版本分支,并开启特性分支到版本分支的合并请求。

2.1 准备工作
在Jenkins, 创建一个Pipeline 作业并配置GenericTrigger 触发器,接收JiraWebhook数据。projectKey 参数表示Jira项目名称,webHookData 参数为Jira webhook的所有数据。token 是触发器的触发token,这里默认采用的作业名称(作业名称要唯一)。

 
 
 
 
  1. triggers {
  2.         GenericTrigger( causeString: 'Trigger By Jira Server -->>>>> Generic Cause', 
  3.                         genericRequestVariables: [[key: 'projectKey', regexpFilter: '']], 
  4.                         genericVariables: [[defaultValue: '', key: 'webHookData', regexpFilter: '', value: '$']], 
  5.                         printContributedVariables: true, 
  6.                         printPostContent: true, 
  7.                         regexpFilterExpression: '', 
  8.                         regexpFilterText: '', 
  9.                         silentResponse: true, 
  10.                         token: "${JOB_NAME}"
  11.         )

在Jira项目中配置Webhook,勾选触发事件填写触发URL。http://jenkins.idevops.site/generic-webhook-trigger/invoke?token=demo-jira-service&projectKey=${project.key} (这个地址是jenkins Generictrigger生成的,这里不做过多的介绍)

Jira webhook数据参考, 这些参数可以在Jenkinsfile中通过readJSON格式化,然后获取值。

 
 
 
 
  1. response = readJSON text: """${webHookData}"""
  2. println(response)
  3. //获取webhook的事件类型
  4. env.eventType = response["webhookEvent"]
 
 
 
 
  1. {
  2.     "timestamp":1603087582648,
  3.     "webhookEvent":"jira:issue_created",
  4.     "issue_event_type_name":"issue_created",
  5.     "user":Object{...},
  6.     "issue":{
  7.         "id":"10500",
  8.         "self":"http://192.168.1.200:8050/rest/api/2/issue/10500",
  9.         "key":"DEMO-2",
  10.         "fields":{
  11.             "issuetype":{
  12.                 "self":"http://192.168.1.200:8050/rest/api/2/issuetype/10001",
  13.                 "id":"10001",
  14.                 "description":"",
  15.                 "iconUrl":"http://192.168.1.200:8050/images/icons/issuetypes/story.svg",
  16.                 "name":"故事",
  17.                 "subtask":false
  18.             },
  19.             "components":[
  20.                 {
  21.                     "self":"http://192.168.1.200:8050/rest/api/2/component/10200",
  22.                     "id":"10200",
  23.                     "name":"demo-hello-service",
  24.                     "description":"demo-hello-service应用"
  25.                 }
  26.             ],
  27.             "timespent":null,
  28.             "timeoriginalestimate":null,
  29.             "description":null,
  30.             ...
  31.             ...
  32.             ...

2.2 封装GitLab接口
Gitlab接口文档

共享库:src/org/devops/gitlab.groovy

 
 
 
 
  1. package org.devops
  2. //封装HTTP请求
  3. def HttpReq(reqType,reqUrl,reqBody){
  4.     def gitServer = "http://gitlab.idevops.site/api/v4"
  5.     withCredentials([string(credentialsId: 'gitlab-token', variable: 'gitlabToken')]) {
  6.       result = httpRequest customHeaders: [[maskValue: true, name: 'PRIVATE-TOKEN', value: "${gitlabToken}"]], 
  7.                 httpMode: reqType, 
  8.                 contentType: "APPLICATION_JSON",
  9.                 consoleLogResponseBody: true,
  10.                 ignoreSslErrors: true, 
  11.                 requestBody: reqBody,
  12.                 url: "${gitServer}/${reqUrl}"
  13.                 //quiet: true
  14.     }
  15.     return result
  16. }
  17. //更新文件内容
  18. def UpdateRepoFile(projectId,filePath,fileContent){
  19.     apiUrl = "projects/${projectId}/repository/files/${filePath}"
  20.     reqBody = """{"branch": "master","encoding":"base64", "content": "${fileContent}", "commit_message": "update a new file"}"""
  21.     response = HttpReq('PUT',apiUrl,reqBody)
  22.     println(response)
  23. }
  24. //获取文件内容
  25. def GetRepoFile(projectId,filePath){
  26.     apiUrl = "projects/${projectId}/repository/files/${filePath}/raw?ref=master"
  27.     response = HttpReq('GET',apiUrl,'')
  28.     return response.content
  29. }
  30. //创建仓库文件
  31. def CreateRepoFile(projectId,filePath,fileContent){
  32.     apiUrl = "projects/${projectId}/repository/files/${filePath}"
  33.     reqBody = """{"branch": "master","encoding":"base64", "content": "${fileContent}", "commit_message": "create a new file"}"""
  34.     response = HttpReq('POST',apiUrl,reqBody)
  35.     println(response)
  36. }
  37. //更改提交状态
  38. def ChangeCommitStatus(projectId,commitSha,status){
  39.     commitApi = "projects/${projectId}/statuses/${commitSha}?state=${status}"
  40.     response = HttpReq('POST',commitApi,'')
  41.     println(response)
  42.     return response
  43. }
  44. //获取项目ID
  45. def GetProjectID(repoName='',projectName){
  46.     projectApi = "projects?search=${projectName}"
  47.     response = HttpReq('GET',projectApi,'')
  48.     def result = readJSON text: """${response.content}"""
  49.     
  50.     for (repo in result){
  51.        // println(repo['path_with_namespace'])
  52.         if (repo['path'] == "${projectName}"){
  53.             
  54.             repoId = repo['id']
  55.             println(repoId)
  56.         }
  57.     }
  58.     return repoId
  59. }
  60. //删除分支
  61. def DeleteBranch(projectId,branchName){
  62.     apiUrl = "/projects/${projectId}/repository/branches/${branchName}"
  63.     response = HttpReq("DELETE",apiUrl,'').content
  64.     println(response)
  65. }
  66. //创建分支
  67. def CreateBranch(projectId,refBranch,newBranch){
  68.     try {
  69.         branchApi = "projects/${projectId}/repository/branches?branch=${newBranch}&ref=${refBranch}"
  70.         response = HttpReq("POST",branchApi,'').content
  71.         branchInfo = readJSON text: """${response}"""
  72.     } catch(e){
  73.         println(e)
  74.     }  //println(branchInfo)
  75. }
  76. //创建合并请求
  77. def CreateMr(projectId,sourceBranch,targetBranch,title,assigneeUser=""){
  78.     try {
  79.         def mrUrl = "projects/${projectId}/merge_requests"
  80.         def reqBody = """{"source_branch":"${sourceBranch}", "target_branch": "${targetBranch}","title":"${title}","assignee_id":"${assigneeUser}"}"""
  81.         response = HttpReq("POST",mrUrl,reqBody).content
  82.         return response
  83.     } catch(e){
  84.         println(e)
  85.     }
  86. }
  87. //搜索分支
  88. def SearchProjectBranches(projectId,searchKey){
  89.     def branchUrl =  "projects/${projectId}/repository/branches?search=${searchKey}"
  90.     response = HttpReq("GET",branchUrl,'').content
  91.     def branchInfo = readJSON text: """${response}"""
  92.     
  93.     def branches = [:]
  94.     branches[projectId] = []
  95.     if(branchInfo.size() ==0){
  96.         return branches
  97.     } else {
  98.         for (branch in branchInfo){
  99.             //println(branch)
  100.             branches[projectId] += ["branchName":branch["name"],
  101.                                     "commitMes":branch["commit"]["message"],
  102.                                     "commitId":branch["commit"]["id"],
  103.                                     "merged": branch["merged"],
  104.                                     "createTime": branch["commit"]["created_at"]]
  105.         }
  106.         return branches
  107.     }
  108. }
  109. //允许合并
  110. def AcceptMr(projectId,mergeId){
  111.     def apiUrl = "projects/${projectId}/merge_requests/${mergeId}/merge"
  112.     HttpReq('PUT',apiUrl,'')
  113. }

2.3 共享库配置

演示效果:上传了两个小视频,可以扫描进入视频号查看。

网站栏目:开源端到端流水线实践-需求与代码管理
URL标题:http://www.shufengxianlan.com/qtweb/news16/470666.html

成都网站建设公司_创新互联,为您提供面包屑导航标签优化Google品牌网站设计网站收录域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联