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
| String jobName = "${env.JOB_BASE_NAME}".toLowerCase()
String imageName = "swr.cn-east-2.myhuaweicloud.com/all-${deployEnv}/${jobName}:${BUILD_ID}"
String gitAddr = "http://gitlab.xxx.com/xxx/server.git"
pipeline{
agent any
parameters {
text(defaultValue: '0m', description: 'request_cpu', name: 'request_cpu')
text(defaultValue: '1Gi', description: 'request_memory', name: 'request_memory')
text(defaultValue: '1000m', description: 'limit_cpu', name: 'limit_cpu')
text(defaultValue: '2Gi', description: 'limit_memory', name: 'limit_memory')
// choice(choices: ['blade-xxx'], description: '服务名称', name: 'serverName')
// choice(choices: ['devops'], description: '命名空间', name: 'nameSpace')
// choice(choices: ['dev'], description: '部署环境[dev,test,prod]', name: 'deployEnv')
// choice(choices: ['dev'], description: '部署分支', name: 'branchName')
choice(choices: ['1','2'], description: '副本数量', name: 'replicas')
}
options {
timestamps() //显示日志时间
skipDefaultCheckout() //删除隐式checkout scm语句
disableConcurrentBuilds() //禁止并行
timeout(time: 20, unit: 'MINUTES') //流水线超时时间10分钟
}
stages{
stage("拉取代码"){
steps{
script{
wrap([$class: 'BuildUser']) {
currentBuild.description = "Started by user:${env.BUILD_USER},branch:${branchName}"
}
checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: "${gitAddr}"]]])
}
}
}
stage("编译"){
steps{
script{
mvnHome = tool "M3"
sh "${mvnHome}/bin/mvn clean package -U -pl `find ./ -name ${serverName}` -am -Dmaven.test.skip=true"
}
}
}
stage("制作镜像"){
steps{
script{
sh "cd `find ./ -name ${serverName}` && docker build -t ${imageName} ."
}
}
}
stage("上传镜像"){
steps{
script{
sh "docker push ${imageName}"
}
}
post{
always{
script{
sh "docker rmi ${imageName}"
}
}
}
}
stage("生成yaml"){
steps{
script{
//修改命名空间
sh "sed -i s#{nameSpace}#${nameSpace}# devops/${serverName}.yaml"
//修改镜像地址
sh "sed -i s#{imageName}#${imageName}# devops/${serverName}.yaml"
//部署环境
sh "sed -i s#{deployEnv}#${deployEnv}# devops/${serverName}.yaml"
//设置副本数量
sh "sed -i s#{replicas}#${replicas}# devops/${serverName}.yaml"
//资源限制
sh "sed -i s#{request_cpu}#${request_cpu}# devops/${serverName}.yaml"
sh "sed -i s#{request_memory}#${request_memory}# devops/${serverName}.yaml"
sh "sed -i s#{limit_cpu}#${limit_cpu}# devops/${serverName}.yaml"
sh "sed -i s#{limit_memory}#${limit_memory}# devops/${serverName}.yaml"
//修改gateway的域名
if ( "${serverName}" == "gateway") {
sh "sed -i s#{domainName}#${domainName}# devops/${serverName}.yaml"
}
}
}
}
stage("部署"){
steps{
script{
sh "cat devops/${serverName}.yaml"
switch("${deployEnv}") {
case "dev":
kubeConfig = "/root/.kube/test.config"
break
;;
case "test":
kubeConfig = "/root/.kube/test.config"
break
;;
case "prod":
kubeConfig = "/root/.kube/prod.config"
break
;;
break
println("未知环境")
;;
}
sh "kubectl apply -f devops/${serverName}.yaml --record --kubeconfig=${kubeConfig}"
}
}
}
stage("就绪检测"){
steps{
timeout(time: 10, unit: 'MINUTES'){ //步骤超时时间
script{
sh "kubectl rollout status -n ${nameSpace} deployment ${serverName} --kubeconfig=${kubeConfig}"
}
}
}
}
}
}
|