Jira 自動化實踐:基於 Jenkins 實現需求與代碼基線關聯

  1. Jira 簡介

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

1.1 Jira 可以做什麼?

規劃
創建項目,用戶需求和事務、規劃 Sprint 並跨團隊分配開發任務。

跟蹤
全面瞭解項目進度情況,安排整個團隊工作的優先級排序並進行討論。


  1. 安裝部署

2.1 數據中心版本

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


選擇歷史版本下載:


下載最新的 LTS 長期支持版本

選擇部署的環境:

開始下載 Jira Software Server 安裝程序:


2.2 安裝部署

scp atlassian-jira-software-8.20.1-x64.bin  root@192.168.1.200:/opt/

## 添加權限
[root@zeyang-nuc-service opt]# chmod +x atlassian-jira-software-8.20.1-x64.bin


## 運行安裝程序
[root@zeyang-nuc-service opt]# ./atlassian-jira-software-8.20.1-x64.bin
Unpacking JRE ...
Starting Installer ...

This will install Jira Software 8.20.1 on your computer.
OK [o, Enter], Cancel [c]      ## 回車

Click Next to continue, or Cancel to exit Setup.

Choose the appropriate installation or upgrade option.
Please choose one of the following:
Express Install (use default settings) [1], Custom Install (recommended for advanced users) [2, Enter], Upgrade an existing Jira installation [3]

## 此處選擇自定義安裝,  輸入回車

Select the folder where you would like Jira Software to be installed.
Where should Jira Software be installed?
[/opt/atlassian/jira]

## 選擇安裝目錄


Default location for Jira Software data
[/var/atlassian/application-data/jira]

## 選擇數據目錄

Configure which ports Jira Software will use.
Jira requires two TCP ports that are not being used by any other
applications on this machine. The HTTP port is where you will access Jira
through your browser. The Control port is used to startup and shutdown Jira.
Use default ports (HTTP: 8080, Control: 8005) - Recommended [1, Enter], Set custom value for HTTP and Control ports [2]
2   ## 輸入2  自定義服務端口
HTTP Port Number
[8071]
8801
Control Port Number
[8077]
8802

## 安裝服務
Jira can be run in the background.
You may choose to run Jira as a service, which means it will start
automatically whenever the computer restarts.
Install Jira as Service?
Yes [y, Enter], No [n]
y


Details on where Jira Software will be installed and the settings that will be used.
Installation Directory: /opt/atlassian/jira
Home Directory: /var/atlassian/application-data/jira
HTTP Port: 8801
RMI Port: 8802
Install as service: Yes
Install [i, Enter], Exit [e]


Extracting files ...


## 啓動服務
Please wait a few moments while Jira Software is configured.

Installation of Jira Software 8.20.1 is complete
Start Jira Software 8.20.1 now?
Yes [y, Enter], No [n]
y

Please wait a few moments while Jira Software starts up.
Launching Jira Software ...



Installation of Jira Software 8.20.1 is complete
Your installation of Jira Software 8.20.1 is now ready and can be accessed
via your browser.
Jira Software 8.20.1 can be accessed at http://localhost:8801
Finishing installation ...

訪問:http://serverip:8801


2.3 初始化配置


獲取授權碼:

註冊賬號登錄:生成授權碼,激活實例;

  1. 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 特性分支合併到版本分支;

  1. Jira 自動化實踐

工作流

工具鏈集成

4.1 需求與代碼關聯

1. 創建 Jenkins 作業並配置 webhook

webhookData:這個變量存放的是 Jira 傳遞的數據。

2. 爲 Jira 配置一個系統級別的 webhook

選項解釋:

  1. 指定 Jenkins 的 webhook 觸發器地址;

  2. 通過 JQL 指定,允許 devops03 這個項目進行觸發;

  3. 勾選動作,觸發事件;(我在 jira 做了什麼操作之後能夠觸發)

3. Jenkins 流水線配置

解析 Jira 傳遞過來的數據;

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}"

封裝 GitLab API 接口
https://docs.gitlab.com/ee/api/branches.html#create-repository-branch

// 創建分支
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)
        println(id)
        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){
            println(i["path_with_namespace"])
            println(groupName + projectName)
            if (i["path_with_namespace"] == "${groupName}/${projectName}"){
                println(i["id"])
                return i["id"]
            }
        }
    } else {
        return response[0]["id"]
    }
}


// 封裝HTTP
def HttpReq(reqType, reqUrl,reqBody ){
    def gitServer = "http://192.168.1.200/api/v4"
    withCredentials([string(credentialsId: '058b7907-ebe2-4d14-9b91-1ac72e071c59', 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
}

Pipeline 主程序

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
                    }
                }
            }
        }
    }
}
  1. 效果驗證

  2. 在 Jira 裏面創建一個模塊和 issue, 關聯 項目;


  1. Jenkins 流水線運行;

  1. 驗證 Gitlab 中多了分支;

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/y8gVTMbsK5icvt9DFU6Hrw