目录
[2.GitLab 查看项目](#2.GitLab 查看项目)
[3.Jira 远程触发 Jenkins 实现合并 GitLab 分支](#3.Jira 远程触发 Jenkins 实现合并 GitLab 分支)
[4.K8S master节点操作](#4.K8S master节点操作)
[5.Jira 发布流水线](#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集群状态
bash
# 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)拿到关键数据
bash
1)用于创建gitlab 项目名称
issue.fields.components
2) 用于gitlab 分支名称
issue.key
3)用于gitlab 项目组名称
issue.fields.project.name
4)用于gitlab 特性分支
issue.fields.fixVersions
(8)Jira官网查询issues API接口 (一个Jira项目中的issues对应一个GitLab的特性分支)
Jira REST API examples (atlassian.com)
bash
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)
bash
http://192.168.204.180:8801/rest/api/2/search?jql=project= DEVOPS3 AND fixVersion = 1.1.6

(10)GitLab官网查询合并请求 API
bash
POST /projects/:id/merge_requests

(11) Postman (POST 方式)测试创建合并请求
bash
http://192.168.204.8:82/api/v4/projects/20/merge_requests

(12)GitLab查看新增的合并请求


(13)合并请求详细信息

(14)这里先关闭合并请求
(15)Jenkins修改Jira流水线代码
bash
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}"
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
case "jira:issue_updated":
if (webhookData.issue.fields.fixVersions.size() >= 1){
jiraFixVersion = webhookData.issue.fields.fixVersions[0]["name"]
//获取fixversion关联的所有issues
issues = 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)
}
}
// 查询JiraReleaseissue
def 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
}
// 根据项目名称获取项目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){
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)外部测试访问
bash
# curl http://devops03-devops-ui.devops.com:31291

(3)删除命名空间devops03
bash
# 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查看镜像
bash
192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.11

(18)K8S node节点连接Harbor拉取镜像
bash
# 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,目前无资源
bash
# 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)
bash
# curl http://devops03-devops-ui.devops.com:31291

(24)最后版本分支 RELEASE-1.1.11 合并到master分支