-
Notifications
You must be signed in to change notification settings - Fork 22
/
Jenkinsfile
178 lines (170 loc) · 8.97 KB
/
Jenkinsfile
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
#!groovy
pipeline {
agent any
parameters {
string(name: 'repoUrl', defaultValue: 'https://github.com/GuoGuang/madao_service.git', description: 'git代码路径')
string(name: 'repoBranch', defaultValue: 'dev', description: 'git分支名称')
string(name: 'pomPath', defaultValue: 'pom.xml', description: 'pom.xml的相对路径')
string(name: 'warLocation', defaultValue: 'rpc/war/target/*.war', description: 'war包的相对路径 ')
choice(name: 'server', choices: '192.168.1.107,9090,*****,*****\n192.168.1.60,9090,*****,*****', description: '测试服务器列表选择(IP,JettyPort,Name,Passwd)')
choice(name: 'project', choices: [
'madao-server-config:9009',
'madao-service-user:9007:9099',
'madao-web-gateway:8080',
'madao-service-base:9008',
'madao-service-article:9003',
'madao-authentication-server:8090',
], description: '选择微服务')
string(name: 'lineCoverage', defaultValue: '20', description: '单元测试代码覆盖率要求(%),小于此值pipeline将会失败!')
booleanParam(name: 'isCommitQA', description: '是否邮件通知测试人员进行人工验收', defaultValue: false)
}
tools {
maven 'maven3'
}
environment {
FRESH_HOST = "registry.cn-hongkong.aliyuncs.com"
REMOTE_SCRIPT = 'sshpass -f /var/jenkins_home/password.txt ssh -t -t -o StrictHostKeyChecking=no root@${INSTANCE_IP}'
REMOTE_IP = "120.53.222.40"
DOCKER_IMAGE = "${params.project}"
DOCKER_CONTAINER = "${params.project}"
QA_EMAIL = "[email protected]"
BUILD_USER_EMAIL = "[email protected]"
BUILD_USER = "构建人"
ITEST_JOBNAME = "InterfaceTest_ExpertPatient"
CRED_ID="*****-****-****-****-*********"
}
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
triggers {
pollSCM('H 4 * * 1-5')
}
stages {
stage('代码获取') {
steps {
script {
def split = params.project.split(":")
serviceName = split[0]
servicePort = split[1]
echo "is-------${params.project}"
if("${serviceName}" == "madao-service-user"){
secondPort = split[2]
}
}
echo "开始从 ${params.repoUrl} 获取代码......"
sh "rm -rf ./*"
sh "git clone -b dev --depth=1 https://github.com/GuoGuang/madao_service.git"
}
}
stage("Maven构建") {
steps {
echo "构建--->${serviceName}"
sh "pwd"
sh "/bin/cp -f /var/jenkins_home/service-config/config-server.jks madao_service/madao-server-config/src/main/resources/"
sh "/bin/cp -f /var/jenkins_home/service-config/bootstrap.yml madao_service/madao-server-config/src/main/resources/"
sh "/bin/cp -f /var/jenkins_home/service-config/application.yml madao_service/madao-authentication-server/src/main/resources/"
sh "/bin/cp -f /var/jenkins_home/service-config/JWT.keystore madao_service/madao-authentication-server/src/main/resources/"
sh "mvn -B -DskipTests install -f madao_service/madao-common-parent"
sh "mvn -B -DskipTests install -f madao_service/madao-common"
sh "mvn -B -DskipTests install -f madao_service/madao-service-api"
sh "mvn -B -DskipTests install -f madao_service/${serviceName}"
echo '-->> -->>maven打包构建完成!'
}
}
stage("Docker构建") {
steps {
script {
def container = sh(returnStdout: true, script: "docker ps -a | grep $serviceName | awk '{print \$1}'").trim()
if (container.size() > 0) {
sh "docker ps -a | grep $serviceName | awk '{print \$1}' | xargs docker stop"
sh "docker ps -a | grep $serviceName | awk '{print \$1}' | xargs docker rm"
echo '-->> 1#停止并删除容器 -->>'
}
def image = sh(returnStdout: true, script: "docker images | grep $serviceName | awk '{print \$3}'").trim()
if (image.size() > 0) {
sh "docker images | grep $serviceName | awk '{print \$3}' | xargs docker rmi -f"
echo '-->> 2#停止并删除镜像 -->>'
}
}
sh "pwd"
echo "/${WORKSPACE}"
dir(path: "/${WORKSPACE}/madao_service/${serviceName}") {
sh "pwd"
sh "docker build -t ${serviceName}:${env.BUILD_ID} ."
script {
sh "docker login [email protected] --password ${DOCKER_HUB_PASSWORD} registry.cn-beijing.aliyuncs.com"
sh "docker tag ${serviceName}:${env.BUILD_ID} registry.cn-beijing.aliyuncs.com/madaoo/${serviceName}:${env.BUILD_ID}"
sh "docker push registry.cn-beijing.aliyuncs.com/madaoo/${serviceName}:${env.BUILD_ID}"
echo "构建并推送到远程服务器成功--->"
}
}
}
}
stage('单元测试') {
steps {
echo "starting unitTest......"
}
}
stage('静态检查') {
steps {
echo "starting codeAnalyze with SonarQube......"
}
}
stage('部署正式环境') {
steps {
echo "开始部署到----> ${serviceName}......"
script {
echo "即将进入"
// jenkinsci/blueocean镜像是基于Alpine Linux系统
// sh "sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories"
// sh "apk update"
// sh "apk add sshpass"
// jenkins/jenkins镜像是基于Ubuntu系统
// sh "sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list"
// sh "apt-get update"
// sh "apt-get install sshpass"
// debian系统
sh "apt update"
sh " apt install sshpass"
def container = sh(returnStdout: true, script: "${REMOTE_SCRIPT} docker ps -a | grep $serviceName | awk '{print \$1}'").trim()
if (container.size() > 0) {
sh "${REMOTE_SCRIPT} docker ps -a | grep $serviceName | awk '{print \$1}' | xargs ${REMOTE_SCRIPT} docker stop"
sh "${REMOTE_SCRIPT} docker ps -a | grep $serviceName | awk '{print \$1}' | xargs ${REMOTE_SCRIPT} docker rm"
echo '-->> 1#停止并删除远程服务器容器 -->>'
}
def image = sh(returnStdout: true, script: "${REMOTE_SCRIPT} docker images | grep $serviceName | awk '{print \$3}'").trim()
if (image.size() > 0) {
sh "${REMOTE_SCRIPT} docker images | grep $serviceName | awk '{print \$3}' | xargs ${REMOTE_SCRIPT} docker rmi -f"
echo '-->> 2#停止并删除远程服务器镜像 -->>'
}
sh "pwd"
sh "${REMOTE_SCRIPT} pwd "
sh "${REMOTE_SCRIPT} docker -v "
sh "${REMOTE_SCRIPT} docker login [email protected] --password ${DOCKER_HUB_PASSWORD} registry.cn-beijing.aliyuncs.com"
sh "${REMOTE_SCRIPT} docker pull registry.cn-beijing.aliyuncs.com/madaoo/${serviceName}:${env.BUILD_ID}"
if("${serviceName}" == "madao-service-user"){
sh "${REMOTE_SCRIPT} docker run -p ${servicePort}:${servicePort} -p ${secondPort}:${secondPort} --name ${serviceName} -d registry.cn-beijing.aliyuncs.com/madaoo/${serviceName}:${env.BUILD_ID}"
}else{
sh "${REMOTE_SCRIPT} docker run -p ${servicePort}:${servicePort} --name ${serviceName} -d registry.cn-beijing.aliyuncs.com/madaoo/${serviceName}:${env.BUILD_ID}"
}
echo '-->> #远程主机构建成功-->>'
}
}
}
stage('接口自动化测试') {
steps{
echo "starting interfaceTest......"
}
}
stage('性能自动化测试 ') {
steps {
echo "starting performanceTest......"
}
}
stage('SUCCESS') {
steps{
echo "done......"
}
}
}
}