Jira自动化实践:基于Jenkins实现需求与代码基线关联,


一. Jira简介

中文官网:https://www.atlassian.com/zh/software/jira

1.1 Jira可以做什么?

规划

创建项目,用户需求和事务、规划 Sprint 并跨团队分配开发任务。

跟踪

全面了解项目进度情况,安排整个团队工作的优先级排序并进行讨论。

二. 安装部署

2.1 数据中心版本

https://www.atlassian.com/zh/software/jira/pricing?tab=data-center 试用

选择历史版本下载:

下载最新的LTS 长期支持版本:

选择部署的环境:

开始下载Jira Software Server安装程序:

2.2 安装部署

  1. scp atlassian-jira-software-8.20.1-x64.bin  root@192.168.1.200:/opt/ 
  2.  
  3. ## 添加权限 
  4. [root@zeyang-nuc-service opt]# chmod +x atlassian-jira-software-8.20.1-x64.bin 
  5.  
  6.  
  7. ## 运行安装程序 
  8. [root@zeyang-nuc-service opt]# ./atlassian-jira-software-8.20.1-x64.bin 
  9. Unpacking JRE ... 
  10. Starting Installer ... 
  11.  
  12. This will install Jira Software 8.20.1 on your computer. 
  13. OK [o, Enter], Cancel [c]      ## 回车 
  14.  
  15. Click Next to continue, or Cancel to exit Setup. 
  16.  
  17. Choose the appropriate installation or upgrade option. 
  18. Please choose one of the following: 
  19. Express Install (use default settings) [1], Custom Install (recommended for advanced users) [2, Enter], Upgrade an existing Jira installation [3] 
  20.  
  21. ## 此处选择自定义安装,  输入回车 
  22.  
  23. Select the folder where you would like Jira Software to be installed. 
  24. Where should Jira Software be installed? 
  25. [/opt/atlassian/jira] 
  26.  
  27. ## 选择安装目录 
  28.  
  29.  
  30. Default location for Jira Software data 
  31. [/var/atlassian/application-data/jira] 
  32.  
  33. ## 选择数据目录 
  34.  
  35. Configure which ports Jira Software will use. 
  36. Jira requires two TCP ports that are not being used by any other 
  37. applications on this machine. The HTTP port is where you will access Jira 
  38. through your browser. The Control port is used to startup and shutdown Jira. 
  39. Use default ports (HTTP: 8080, Control: 8005) - Recommended [1, Enter], Set custom value for HTTP and Control ports [2] 
  40. 2   ## 输入2  自定义服务端口 
  41. HTTP Port Number 
  42. [8071] 
  43. 8801 
  44. Control Port Number 
  45. [8077] 
  46. 8802 
  47.  
  48. ## 安装服务 
  49. Jira can be run in the background. 
  50. You may choose to run Jira as a service, which means it will start 
  51. automatically whenever the computer restarts. 
  52. Install Jira as Service? 
  53. Yes [y, Enter], No [n] 
  54.  
  55.  
  56. Details on where Jira Software will be installed and the settings that will be used. 
  57. Installation Directory: /opt/atlassian/jira 
  58. Home Directory: /var/atlassian/application-data/jira 
  59. HTTP Port: 8801 
  60. RMI Port: 8802 
  61. Install as service: Yes 
  62. Install [i, Enter], Exit [e] 
  63.  
  64.  
  65. Extracting files ... 
  66.  
  67.  
  68. ## 启动服务 
  69. Please wait a few moments while Jira Software is configured. 
  70.  
  71. Installation of Jira Software 8.20.1 is complete 
  72. Start Jira Software 8.20.1 now? 
  73. Yes [y, Enter], No [n] 
  74.  
  75. Please wait a few moments while Jira Software starts up. 
  76. Launching Jira Software ... 
  77.  
  78.  
  79.  
  80. Installation of Jira Software 8.20.1 is complete 
  81. Your installation of Jira Software 8.20.1 is now ready and can be accessed 
  82. via your browser. 
  83. Jira Software 8.20.1 can be accessed at http://localhost:8801 
  84. Finishing installation ... 

访问:http://serverip:8801

2.3 初始化配置

获取授权码:

注册账号登录:生成授权码,激活实例;

三. Jira使用实践

3.1 创建一个项目

注意:一个Jira 项目对应一个GitLab项目组;

3.2 为项目添加模块

注意:一个Jira模块对应一个GitLab项目;

3.3 配置WebHook系统, 网络钩子(webhook)

3.4 需求/任务管理

创建需求

这里在Jira上面把这个需求logging-error, 类型为故事 关联到devops03-devops-service模块;

3.5 发布Release

课程中把 发布对应为GitLab项目代码库中的版本分支;

issue关联发布:可以想象成GitLab特性分支合并到版本分支;

四. Jira自动化实践

工作流

工具链集成

4.1 需求与代码关联

1. 创建Jenkins作业并配置webhook

webhookData:这个变量存放的是Jira 传递的数据。

2. 为Jira配置一个系统级别的webhook

选项解释:

  • 指定Jenkins的webhook触发器地址;
  • 通过JQL指定,允许devops03这个项目进行触发;
  • 勾选动作,触发事件;(我在jira做了什么操作之后能够触发)

3. Jenkins流水线配置

解析Jira传递过来的数据;

  1. webhookData = readJSON text: "${webhookData}" 
  2.  
  3.  
  4. // Jira事件 
  5. jiraEvent = webhookData.webhookEvent 
  6. jiraProjectName = webhookData.issue.fields.project.name 
  7.  
  8. // 获取gitlab参数 
  9. gitlabProjects = [] 
  10. gitlabBranchName = webhookData.issue.key 
  11. gitlabGroupName = jiraProjectName 
  12.  
  13. for (i in webhookData.issue.fields.components){ 
  14.     gitlabProjects.add(i["name"]) 
  15.  
  16. currentBuild.description = "Trigger by ${jiraEvent} \n project: ${gitlabProjects} \n branch: ${gitlabBranchName}" 

封装GitLab API接口

https://docs.gitlab.com/ee/api/branches.html#create-repository-branch

  1. // 创建分支 
  2. def CreateBranch(projectId, newBranchName, sourceBranchName){ 
  3.     try { 
  4.         apiUrl = "projects/${projectId}/repository/branches?branch=${newBranchName}&ref=${sourceBranchName}" 
  5.         response = HttpReq('POST', apiUrl, "") 
  6.     } 
  7.     catch(Exception e) { 
  8.         println(e) 
  9.          
  10.     } 
  11.  
  12. // 获取所有项目的id 
  13. def GetProjectsId(gitlabGroupName, gitlabProjects){ 
  14.     gitlabProjectIds = [] 
  15.     for (project in gitlabProjects){ 
  16.         id = GetProjectId(gitlabGroupName, project) 
  17.         println(id) 
  18.         if (id != 0){ 
  19.             gitlabProjectIds.add(id) 
  20.         } 
  21.     } 
  22.     return gitlabProjectIds 
  23.  
  24. // 根据项目名称获取项目id 
  25. def GetProjectId(groupName, projectName){ 
  26.     apiUrl = "projects?search=${projectName}" 
  27.     response = HttpReq('GET', apiUrl, "") 
  28.     response = readJSON text: response.content - "\n" 
  29.  
  30.     if (response.size() > 1){ 
  31.         for (i in response){ 
  32.             println(i["path_with_namespace"]) 
  33.             println(groupName + projectName) 
  34.             if (i["path_with_namespace"] == "${groupName}/${projectName}"){ 
  35.                 println(i["id"]) 
  36.                 return i["id"] 
  37.             } 
  38.         } 
  39.     } else { 
  40.         return response[0]["id"] 
  41.     } 
  42.  
  43.  
  44. // 封装HTTP 
  45. def HttpReq(reqType, reqUrl,reqBody ){ 
  46.     def gitServer = "http://192.168.1.200/api/v4" 
  47.     withCredentials([string(credentialsId: '058b7907-ebe2-4d14-9b91-1ac72e071c59', variable: 'GITLABTOKEN')]) { 
  48.         response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',  
  49.                           consoleLogResponseBody: true,  
  50.                           contentType: 'APPLICATION_JSON_UTF8',  
  51.                           customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: "${GITLABTOKEN}"]],  
  52.                           httpMode: "${reqType}",  
  53.                           url: "${gitServer}/${reqUrl}",  
  54.                           wrapAsMultipart: false, 
  55.                           requestBody: "${reqBody}" 
  56.  
  57.     } 
  58.     return response 

Pipeline主程序

  1. pipeline { 
  2.     agent { label "build" } 
  3.  
  4.     stages{ 
  5.         stage("Process"){ 
  6.             steps{ 
  7.                 script{ 
  8.                     println(gitlabProjects) 
  9.                     println(gitlabBranchName) 
  10.                     projectIds = GetProjectsId(gitlabGroupName, gitlabProjects) 
  11.                     switch(jiraEvent) { 
  12.                         case "jira:issue_created": 
  13.                             println(projectIds) 
  14.                             for (id in projectIds){ 
  15.                                 CreateBranch(id, gitlabBranchName, "master") 
  16.                             } 
  17.                             break 
  18.                         default: 
  19.                             println("error...") 
  20.                             break 
  21.                     } 
  22.                 } 
  23.             } 
  24.         } 
  25.     } 

4. 效果验证

在Jira里面创建一个模块和issue, 关联项目;

Jenkins 流水线运行;

验证Gitlab中多了分支;

相关内容