目錄
一、實驗
1.環境
2.GitLab 查看項目
3.Jira 遠程觸發 Jenkins 實現合并 GitLab 分支
4.K8S master節點操作
5.Jira 發布流水線
一、實驗
1.環境
(1)主機
表1 主機
主機 | 架構 | 版本 | IP | 備注 |
master1 | K8S master節點 | 1.20.6 | 192.168.204.180 | jenkins slave (從節點) |
jira | 9.12.1 | 192.168.204.180:8801 | ||
node1 | K8S node節點 | 1.20.6 | 192.168.204.181 | |
node2 | K8S node節點 | 1.20.6 | 192.168.204.182 | |
jenkins | ?jenkins主節點? ? ?? | 2.414.2 | 192.168.204.15:8080 | ?gitlab?runner (從節點) |
harbor私有倉庫 | 1.2.2 | 192.168.204.15 | ||
gitlab | gitlab 主節點????? | 12.10.14 | 192.168.204.8:82 | jenkins slave (從節點) |
sonarqube | 9.6 | 192.168.204.8:9000 |
(2)查看K8S集群狀態
# kubectl get node
2.GitLab 查看項目
(1)GitLab查看前端項目(項目編號為20)
3.Jira 遠程觸發 Jenkins 實現合并 GitLab 分支
(1)點擊系統
(2)管理員入口 (需要輸入密碼)
(3)修改網絡鉤子(選擇Issue操作事件、Release操作事件來觸發)
問題里添加“已更新”
(4)完成修改
(5)修復項目版本
指定1.1.6版本
(6)Jenkins觸發流水線,JSON格式轉換
(7)拿到關鍵數據
1)用于創建gitlab 項目名稱
issue.fields.components 2) 用于gitlab 分支名稱
issue.key3)用于gitlab 項目組名稱
issue.fields.project.name4)用于gitlab 特性分支
issue.fields.fixVersions
(8)Jira官網查詢issues API接口? (一個Jira項目中的issues對應一個GitLab的特性分支)
Jira REST API examples (atlassian.com)
curl \-D- \-u charlie:charlie \-X GET \-H "Content-Type: application/json" \http://localhost:8080/rest/api/2/search?jql=assignee=charlie
(9)Postman( GET?方式)測試取到Jira 的修復版本信息(fixVersion)
http://192.168.204.180:8801/rest/api/2/search?jql=project= DEVOPS3 AND fixVersion = 1.1.6
(10)GitLab官網查詢合并請求 API
Merge requests API | GitLab
POST /projects/:id/merge_requests
(11) Postman (POST 方式)測試創建合并請求
http://192.168.204.8:82/api/v4/projects/20/merge_requests
(12)GitLab查看新增的合并請求
(13)合并請求詳細信息
(14)這里先關閉合并請求
(15)Jenkins修改Jira流水線代碼
webhookData = readJSON text: "${webhookData}"//jira 事件
jiraEvent = webhookData.webhookEvent
jiraProjectName = webhookData.issue.fields.project.name// 獲取gitlab參數
gitlabProjects = []
gitlabBranchName = webhookData.issue.key
gitlabGroupName = jiraProjectNamefor (i in webhookData.issue.fields.components){gitlabProjects.add(i["name"])
}//描述信息
currentBuild.description = "Trigger by ${jiraEvent} \n project: ${gitlabProjects} \n branch: ${gitlabBranchName}"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")}breakcase "jira:issue_updated":if (webhookData.issue.fields.fixVersions.size() >= 1){jiraFixVersion = webhookData.issue.fields.fixVersions[0]["name"]//獲取fixversion關聯的所有issuesissues = GetIssuesByFixVersion(jiraProjectName, jiraFixVersion)// 在issue關聯的所有項目創建版本分支for (id in projectIds){CreateBranch(id, "RELEASE-${jiraFixVersion}", "master") //RELEASE-1.1.6// 創建合并請求 特性分支 > 版本分支for(issue in issues) {CreateMergeRequest(id, issue, "RELEASE-${jiraFixVersion}" )}}break}default:println(error)break}}}}}
}// 創建合并請求def CreateMergeRequest(projectId,sourceBranch,targetBranch) {try {apiUrl = "projects/${projectId}/merge_requests"reqBody = """{"source_branch": "${sourceBranch}","target_branch":"${targetBranch}","title": "${sourceBranch}>>>${targetBranch}byJenkins"}"""response = HttpReq('POST', apiUrl, reqBody)}catch (Exception e) {println(e)}
}// 查詢JiraReleaseissuedef GetIssuesByFixVersion(projectName, fixVersion){jql = "project%20=%20${projectName}%20AND%20fixVersion%20=%20${fixVersion}"response = sh returnStdout: true,script: """curl \-u admin:admin \-H "Content-Type: application/json" \--request GET "http://192.168.204.180:8801/rest/api/2/search?jql=${jql}" -s"""response = readJSON text: """ ${response - "\n"} """issues = []for (i in response["issues"]){issues.add(i["key"])}return issues
}// 創建分支
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)if (id != 0){gitlabProjectIds.add(id)}}return gitlabProjectIds
}// 根據項目名稱獲取項目iddef 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){if (i["path_with_namespace"] == "${groupName}/${projectName}"){return i["id"]}}}else {return response[0]["id"]}}// 封裝HTTP
def HttpReq(reqType, reqUrl,reqBody ){def gitServer = "http://192.168.204.8:82/api/v4"withCredentials([string(credentialsId: '02dce3ff-4e46-4de2-b079-5dd6093d4f64', 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
}
保存
4.K8S master節點操作
(1)K8S master節點另開一個終端用watch命令觀察pod變化
# watch -n 1 "kubectl get pod -n devops03"
(2)外部測試訪問
# curl http://devops03-devops-ui.devops.com:31291
(3)刪除命名空間devops03
# kubectl delete ns devops03
(4)觀察pod變化
5.Jira 發布流水線
(1)Jira新建問題
(2)Jira創建事件觸發Jenkins流水線
(3) GitLab更新特性分支DEVOPS-11
(4)修改GitLab修改特性分支Dockerfile,并提交特性分支DEVOPS3-11
(5)Jira 新建版本
(6)查看版本
(7)修改版本為1.1.11
(8)Jenkins更新事件觸發Jenkins流水線
(9)GitLab提示特性分支合并到版本分支的請求
(10)合并請求
(11)完成合并
(12)查看合并
(13)Jenkins構建基于HELM的K8S CI流水線
(14)成功
(15)查看Blue Ocean
(16) 查看階段視圖
(17)Harbor查看鏡像
192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.11
(18)K8S node節點連接Harbor拉取鏡像
# docker login -u admin -p Harbor12345 192.168.204.15# docker pull 192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.11# docker logout 192.168.204.15
node1節點
node2節點
(19)K8S master節點創建命名空間devops03,目前無資源
# kubectl create ns devops03# kubectl get all -n devops03
(20)GitLab 查看HELM模板文件values.yaml 已更新鏡像標簽為RELEASE-1.1.11
(21)Jenkins構建基于HELM的K8S CD流水線
(22)觀察pod變化
(23)外部測試訪問(當前版本為1.1.11)
# curl http://devops03-devops-ui.devops.com:31291
(24)最后版本分支 RELEASE-1.1.11 合并到master分支