java
# Jenkinsfile
pipeline {
agent {
kubernetes {
label 'birenchong-java-gateway'
yaml '''apiVersion: v1
kind: Pod
spec:
containers:
- name: maven
image: maven:3.6.3-openjdk-17
command: [\'cat\']
tty: true
volumeMounts:
- mountPath: "/usr/share/maven/conf/settings.xml"
name: "config-volume"
subPath: "settings.xml"
- mountPath: "/root/.sonar/cache"
name: "data-volume"
subPath: "java/.sonar/cache"
readOnly: false
- mountPath: "/root/.m2"
name: "data-volume"
subPath: "java/.m2/caches"
readOnly: false
- mountPath: "/var/run/docker.sock"
name: "volume-0"
readOnly: false
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
- name: "base"
image: "kubesphere/builder-base:v3.2.0"
command: [\'cat\']
tty: true
volumeMounts:
- mountPath: "/home/jenkins/aws"
name: "aws-data-volume"
readOnly: false
- mountPath: "/home/jenkins/send_dingding.sh"
name: "dingding-script-volume"
subPath: "send_dingding.sh"
- mountPath: "/root/.aws/config"
name: "eks-config-volume"
subPath: "config"
- mountPath: "/root/.aws/credentials"
name: "eks-config-volume"
subPath: "credentials"
- mountPath: "/var/run/docker.sock"
name: "volume-0"
readOnly: false
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
nodeSelector:
env: devops
volumes:
- name: "aws-data-volume"
persistentVolumeClaim:
claimName: "birenchong-aws-data-pvc"
- name: "data-volume"
persistentVolumeClaim:
claimName: "birenchong-java-data-pvc"
- hostPath:
path: "/var/run/docker.sock"
name: "volume-0"
- emptyDir:
medium: ""
name: "workspace-volume"
- configMap:
items:
- key: "MavenSetting.xml"
path: "settings.xml"
name: "birenchong-maven-setting"
name: "config-volume"
- configMap:
items:
- key: "aws-config"
path: "config"
- key: "aws-credentials"
path: "credentials"
name: "birenchong-eks-config"
name: "eks-config-volume"
- configMap:
items:
- key: "send_dingding.sh"
path: "send_dingding.sh"
name: "birenchong-jenkins-dingding-script"
name: "dingding-script-volume"
'''
}
}
stages {
stage('构建') {
agent none
steps {
container('maven') {
sh 'mvn clean package -DskipTests'
}
}
}
stage('构建docker镜像并推送') {
agent none
steps {
container('base') {
sh 'docker build -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
withCredentials([usernamePassword(credentialsId : env.DOCKER_CREDENTIAL_ID ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
sh 'docker rmi $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
}
}
}
}
stage('配置aws和k8s凭证') {
agent none
steps {
container('base') {
sh 'cp /home/jenkins/aws/awscliv2.zip .'
sh 'unzip awscliv2.zip'
sh './aws/install'
sh 'aws eks update-kubeconfig --region eu-central-1 --name birenchong-k8s-cluster'
}
}
}
stage('部署到测试环境') {
agent none
when {
branch 'dev'
}
steps {
container('base') {
sh 'envsubst < deploy/dev-ol/deployment.yaml | kubectl apply -f -'
}
}
}
stage('部署到预发布环境') {
agent none
when {
branch 'grey'
}
steps {
container('base') {
sh 'envsubst < deploy/grey-ol/deployment.yaml | kubectl apply -f -'
}
}
}
stage('default-6') {
parallel {
stage('部署到生产环境') {
agent none
when {
branch 'master'
}
steps {
input(message: '部署到生产环境? ', submitter: 'birenchong')
container('base') {
sh 'envsubst < deploy/prod-ol/deployment.yaml | kubectl apply -f -'
}
}
}
stage('发送到钉钉审核') {
agent none
when {
branch 'master'
}
steps {
container('base') {
sh 'bash /home/jenkins/send_dingding.sh "构建审核"'
}
}
}
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'harbor-devops-id'
REGISTRY = 'harbor.birenchong.cn'
DOCKERHUB_NAMESPACE = 'birenchong'
APP_NAME = 'birenchong-java-gateway'
}
post {
success {
container('base') {
sh 'bash /home/jenkins/send_dingding.sh "构建成功"'
}
}
failure {
container('base') {
sh 'bash /home/jenkins/send_dingding.sh "构建失败"'
}
}
aborted {
container('base') {
sh 'bash /home/jenkins/send_dingding.sh "取消构建"'
}
}
}
}
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
将通过deployment.yaml文件部署改为只修改image
sh 'kubectl set image deployment/birenchong-test-java-gateway-v1 -n birenchong-test birenchong-java-gateway=$REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
1
# Dockerfile
FROM openjdk:17-jdk
LABEL maintainer=birenchong
ENV TZ=Asia/Shanghai
ENV JAVA_OPTS=-Djava.security.egd=file:/dev/./urandom
COPY target/*.jar /var/www/java/web/birenchong-gateway.jar
EXPOSE 18080
ENTRYPOINT ["/bin/sh","-c","java -XX:+UseZGC -Xmx3072m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/www/java/gc.hprof -jar /var/www/java/web/birenchong-gateway.jar --spring.config.location=/var/www/java/web/application.yml"]
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: birenchong-test-java-gateway-v1
namespace: birenchong-test
labels:
app: birenchong-test-java-gateway
version: v1
annotations:
kubesphere.io/creator: admin
spec:
selector:
matchLabels:
app: birenchong-test-java-gateway
version: v1
template:
metadata:
creationTimestamp: null
labels:
app: birenchong-test-java-gateway
version: v1
annotations:
logging.kubesphere.io/logsidecar-config: '{}'
spec:
volumes:
- name: volume-y0tu48
configMap:
name: birenchong-test-java-conf
items:
- key: birenchong-gateway-application
path: application.yml
defaultMode: 420
containers:
- name: birenchong-java-gateway
image: $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER
ports:
- name: tcp-18080
containerPort: 18080
protocol: TCP
resources:
limits:
cpu: 750m
memory: 3Gi
requests:
cpu: 500m
memory: 2Gi
volumeMounts:
- name: volume-y0tu48
readOnly: true
mountPath: /var/www/java/web/application.yml
subPath: application.yml
lifecycle:
preStop:
exec:
command:
- /bin/bash
- '-c'
- sleep 30
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 35
dnsPolicy: ClusterFirst
nodeSelector:
env: test
serviceAccountName: default
serviceAccount: default
securityContext: {}
imagePullSecrets:
- name: harbor-devops
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
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
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
Last Updated: 2023/11/08, 14:45:54