Jenkins Pipeline 共享库
使用共享库可以共享一段Pipeline代码,避免一次多处修改。
# 创建共享库
共享库是一个Git仓库
我们的共享库目录如下:
└─vars
└─BuildJava.groovy
BuildJava.groovy:
#!groovy
def send() {
//return
def body = """
{
"buildStatus": "${env.BUILD_STATUS}",
"jobName": "${env.appName}",
"buildUser": "${env.BUILD_USER}",
"gitBranch": "${env.GIT_BRANCH}",
"gitCommit": "${env.GIT_COMMIT}",
"gitCommitUser": "${env.GIT_COMMITER}",
"gitCommitMessage": "${env.GIT_MESSAGE}",
"jobUrl": "${env.BUILD_URL}"
}
"""
httpRequest(customHeaders: [[name: 'Content-Type', value: 'application/json;charset=UTF-8']],
httpMode: 'POST',
requestBody: body,
url: "http://feishu-adapter:8080/jenkins/push")
}
def call() {
env.label = "jenkins-agent-${env.appName}-${env.BUILD_TAG}"
env.GIT_COMMIT = "暂未获取"
env.GIT_COMMITER = "暂未获取"
env.GIT_MESSAGE = "暂未获取"
env.BUILD_STATUS = "1"
try {
//从pod启动agent容器
print(env.label)
podTemplate(label: env.label, cloud: 'kubernetes', containers: [containerTemplate(name: 'jnlp',
// 包含 docker kubectl
image: 'dragonmo/jenkins-agent-docker:1.2.6',
alwaysPullImage: false,
privileged: true,
workingDir: "/root",
args: '${computer.jnlpmac} ${computer.name}'),],
volumes: [hostPathVolume(mountPath: '/root/workspace/', hostPath: '/jenkins-agent/workspace'),
hostPathVolume(mountPath: '/var/lib/docker/image/', hostPath: '/jenkins-agent/dockerimage')
]) {
node(env.label) {
stage('init') {
wrap([$class: 'BuildUser']) {
env.BUILD_USER = env.BUILD_USER
}
println("开始构建,构建人${env.BUILD_USER},项目名:${env.appName},标签名:${env.label},构建容器:${env.label}")
//复制kube集群配置文件 使kubectl能够连接集群
configFileProvider([configFile(fileId: 'kubeconfig', variable: 'KUBE_CONFIG')]) {
sh 'mkdir ~/.kube'
sh 'cp $KUBE_CONFIG ~/.kube/config'
sh 'kubectl get nodes'
}
}
stage('pull git') {
//拉取代码
git(url: env.GIT_URL, branch: env.GIT_BRANCH, credentialsId: 'da2cde30-a763-419e-9857-ec128fd8a6d7')
env.GIT_COMMIT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
env.GIT_COMMITER = sh(script: 'git --no-pager show -s --format=\'%an\'', returnStdout: true).trim()
env.GIT_MESSAGE = sh(script: 'git log -n 1 --pretty=format:\'%s\'', returnStdout: true).trim()
println("获取到git信息 commit:${env.GIT_COMMIT},commiter:${env.GIT_COMMITER},message:${env.GIT_MESSAGE}")
send()
}
stage('maven package') {
dir(serviceDir) {
//安装maven
withMaven(maven: 'maven') {
env.PATH = "/root/tools/hudson.tasks.Maven_MavenInstallation/maven/bin:${env.PATH}"
//maven打包
sh "mvn package -Dmaven.test.skip=true"
//删除源码包
sh "rm -f ./target/*-sources.jar"
}
}
}
stage("docker build and push") {
sh "mkdir /etc/docker/"
//配置私服http
def daemon = '{\"registry-mirrors\":[\"https://docker.mirrors.ustc.edu.cn\"],\"insecure-registries\":[\"nexus3:8082\",\"nexus3:8083\"]}'
dir(env.serviceDir) {
sh "echo '" + daemon + "' > /etc/docker/daemon.json"
sh "service docker stop"
sh "service docker start"
//生成dockerfile 打包
sh "echo FROM openjdk:8u312-oracle >Dockerfile"
sh "echo COPY ./target/*.jar /app.jar >>Dockerfile"
//程序入口
sh 'echo ENTRYPOINT ' + env.entrypoint + ' >>Dockerfile'
//打包
image = docker.build "${env.appName}:${env.BUILD_TAG}"
int i = 0
while (true) {
try {
//登陆私服
docker.withRegistry("http://nexus3:8082/repository/docker-dram/", "f65db64b-5f08-4c24-b39a-bf9227e37f74") {
//推送镜像
image.push(env.BUILD_TAG)
}
break
} catch (Throwable e) {
//重试3次登陆
i++
println(e.getMessage())
if (i == 3) {
throw e
} else {
sh 'sleep 3'
}
}
}
}
}
stage("k8s deploy") {
//引入部署模板
configFileProvider([configFile(fileId: env.deploymentTemplateId, variable: 'DEPLOYMENT')]) {
//替换模板字段 生成部署文件
sh "sed 's/#{appname}/${env.appName}/;s/#{apptag}/${env.BUILD_TAG}/' $DEPLOYMENT > deployment.yaml"
//部署
sh "kubectl apply -f deployment.yaml"
}
env.BUILD_STATUS = "2"
send()
}
}
}
} catch (Throwable e) {
env.BUILD_STATUS = "3"
send()
throw e
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 配置共享库
系统管理->全局设置->Global Pipeline Libraries
# 使用共享库
#!groovy
@Library('iot-back-dev') _
//项目名
env.appName = "iot-cloud-alarm"
//git地址
env.GIT_URL = "ssh://[email protected]:30386/IoTFrame/cloud/iot-cloud-alarm.git"
//git分支
env.GIT_BRANCH = "dev"
//多maven 要执行的目录
env.serviceDir = 'iot-cloud-alarm-service'
//程序入口
env.entrypoint = 'java -jar -Dactive=dev -DnacosAddress=nacos:8848 /app.jar --server.port=8080'
env.deploymentTemplateId = "deployment-iot-back-persist"
BuildJava()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
上次更新: 10/23/2024