-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile-backup01
70 lines (67 loc) · 2.54 KB
/
Jenkinsfile-backup01
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
def label = "slave-${UUID.randomUUID().toString()}"
podTemplate(label: label, containers: [
containerTemplate(name: 'maven', image: 'maven:3.6-alpine', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'kubectl', image: 'cnych/kubectl', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'helm', image: 'cnych/helm', command: 'cat', ttyEnabled: true)
], volumes: [
persistentVolumeClaim(mountPath: '/root/.m2', claimName: 'maven-pvc', readOnly: 'false'),
hostPathVolume(mountPath: '/home/jenkins/.kube', hostPath: '/root/.kube'),
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]) {
node(label) {
def myRepo = checkout scm
def gitCommit = myRepo.GIT_COMMIT
def gitBranch = myRepo.GIT_BRANCH
def imageTag = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
def dockerRegistryUrl = "192.168.4.112"
def imageEndpoint = "sc/service-eureka"
def image = "${dockerRegistryUrl}/${imageEndpoint}"
stage('单元测试') {
echo "测试阶段"
}
stage('代码编译打包') {
try {
container('maven') {
echo "2. 代码编译打包阶段"
sh "mvn -f pom.xml -s settings.xml clean package -Dmaven.test.skip=true"
}
} catch (exc) {
println "构建失败 - ${currentBuild.fullDisplayName}"
throw(exc)
}
}
container('构建 Docker 镜像') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'harbor',
usernameVariable: 'harborUser',
passwordVariable: 'harborPassword']]) {
container('docker') {
echo "3. 构建 Docker 镜像阶段"
sh """
docker login -u ${harborUser} -p ${harborPassword} ${dockerRegistryUrl}
docker build --add-host nexus.smile.com:192.168.4.113 -t ${image}:${imageTag} .
docker push ${image}:${imageTag}
"""
}
}
}
stage('运行 Kubectl') {
container('kubectl') {
echo "查看 K8S 集群 Pod 列表"
sh "kubectl get pods"
sh """
sed -i "s#<IMAGE>#${image}#g" mainfests/k8s-backup01.yaml
sed -i "s#<IMAGE_TAG>#${imageTag}#g" mainfests/k8s-backup01.yaml
kubectl apply -f mainfests/k8s-backup01.yaml --record
"""
}
}
stage('运行 Helm') {
container('helm') {
echo "查看 Helm Release 列表"
sh "helm list"
}
}
}
}