服务器之家

服务器之家 > 正文

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

时间:2021-11-15 22:01     来源/作者:DevOps云学堂

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

一. Jira简介

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

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

1.1 Jira可以做什么?

规划

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

跟踪

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

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

二. 安装部署

2.1 数据中心版本

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

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

选择历史版本下载:

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

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

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

选择部署的环境:

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

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

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

2.2 安装部署

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

访问:http://serverip:8801

2.3 初始化配置

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

获取授权码:

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

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

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

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

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

三. Jira使用实践

3.1 创建一个项目

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

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

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

3.2 为项目添加模块

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

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

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

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

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

3.4 需求/任务管理

创建需求

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

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

3.5 发布Release

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

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

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

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

四. Jira自动化实践

工作流

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

工具链集成

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

4.1 需求与代码关联

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

1. 创建Jenkins作业并配置webhook

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

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

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

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

选项解释:

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

3. Jenkins流水线配置

解析Jira传递过来的数据;

  1. webhookData = readJSON text: "${webhookData}"
  2. // Jira事件
  3. jiraEvent = webhookData.webhookEvent
  4. jiraProjectName = webhookData.issue.fields.project.name
  5. // 获取gitlab参数
  6. gitlabProjects = []
  7. gitlabBranchName = webhookData.issue.key
  8. gitlabGroupName = jiraProjectName
  9. for (i in webhookData.issue.fields.components){
  10. gitlabProjects.add(i["name"])
  11. }
  12. 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. // 获取所有项目的id
  12. def GetProjectsId(gitlabGroupName, gitlabProjects){
  13. gitlabProjectIds = []
  14. for (project in gitlabProjects){
  15. id = GetProjectId(gitlabGroupName, project)
  16. println(id)
  17. if (id != 0){
  18. gitlabProjectIds.add(id)
  19. }
  20. }
  21. return gitlabProjectIds
  22. }
  23. // 根据项目名称获取项目id
  24. def GetProjectId(groupName, projectName){
  25. apiUrl = "projects?search=${projectName}"
  26. response = HttpReq('GET', apiUrl, "")
  27. response = readJSON text: response.content - "\n"
  28. if (response.size() > 1){
  29. for (i in response){
  30. println(i["path_with_namespace"])
  31. println(groupName + projectName)
  32. if (i["path_with_namespace"] == "${groupName}/${projectName}"){
  33. println(i["id"])
  34. return i["id"]
  35. }
  36. }
  37. } else {
  38. return response[0]["id"]
  39. }
  40. }
  41. // 封装HTTP
  42. def HttpReq(reqType, reqUrl,reqBody ){
  43. def gitServer = "http://192.168.1.200/api/v4"
  44. withCredentials([string(credentialsId: '058b7907-ebe2-4d14-9b91-1ac72e071c59', variable: 'GITLABTOKEN')]) {
  45. response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',
  46. consoleLogResponseBody: true,
  47. contentType: 'APPLICATION_JSON_UTF8',
  48. customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: "${GITLABTOKEN}"]],
  49. httpMode: "${reqType}",
  50. url: "${gitServer}/${reqUrl}",
  51. wrapAsMultipart: false,
  52. requestBody: "${reqBody}"
  53. }
  54. return response
  55. }

Pipeline主程序

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

4. 效果验证

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

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

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

Jenkins 流水线运行;

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

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

验证Gitlab中多了分支;

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

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

原文链接:https://mp.weixin.qq.com/s/LFR4NB1zLLKeJNutkz800w

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部