fzy-blog

部署操作 jenkins job 任务 pipeline script 脚本

2019-05-24

部署操作 jenkins job 任务 pipeline script 脚本

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
env.GIT='ssh://git@git.xxx.com:2222/bop/fms-group/fms.git'
env.name='fms-p'
env.docker_path='fms-p/docker'
env.jar_name='fms-p'
env.dockerfile_jar_name='fms-p'

env.node1='xxx1.com'
env.node2='xxx2.com'
def timeStamp = Calendar.getInstance().getTime().format('YYYYMMddhhmmss',TimeZone.getTimeZone('Asia/Shanghai'))
env.build_version = timeStamp

node("150g"){
stage('Git Checkout'){
sh '''
rm -rf ./*
'''
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxxxxxx-xxxx-xxxx',url: env.GIT]]])
}
stage('Maven Build'){
echo "Maven Build..."
sh '''
mvn clean install
'''
}

stage('Build and Push Image'){
withCredentials([string(credentialsId: 'xxxxxxx-xxxx-xxxx', variable: 'HUB_TOKEN')]) {
sh '''

docker login -u bop_mail_alert -p ${HUB_TOKEN} http://registry.api.xxx.com
cd ${docker_path}
rm -rf ../target/${jar_name}-1.0*-javadoc.jar ../target/${jar_name}-1.0*-sources.jar

touch 200.sh .
touch 503.sh .

cp ../target/${jar_name}-1.0*.jar ./${dockerfile_jar_name}.jar

docker build -t registry.api.xxx.com/bop-k8s/${name}:${build_version} .
docker push registry.api.xxx.com/bop-k8s/${name}:${build_version}
'''
}

}

}


node(env.node1){

stage('Pull Image And Run'){
withCredentials([string(credentialsId: 'xxxxxxx-xxxx-xxxx', variable: 'HUB_TOKEN')]) {
sh '''
set +e
sleep 5

docker rmi ${name}-backup
docker commit ${name} ${name}-backup
docker stop --time=20 ${name}
docker rm -f ${name}
docker pull registry.api.xxx.com/bop-k8s/${name}:${build_version}
docker run -d -v /data0/${name}/logs:/data/logs --name ${name} --net="host" --ulimit nofile=200000 -v /etc/localtime:/etc/localtime:ro -m 1536M --memory-swap 2048M -e JAVA_OPTS="-server -Xmx1024m" registry.api.xxx.com/bop-k8s/${name}:${build_version}
'''
}

}

}
node(env.node2){

stage('Pull Image And Run'){
withCredentials([string(credentialsId: 'xxxxxxx-xxxx-xxxx', variable: 'HUB_TOKEN')]) {
sh '''
set +e

docker rmi ${name}-backup
docker commit ${name} ${name}-backup
docker stop --time=20 ${name}
docker rm -f ${name}
docker pull registry.api.xxx.com/bop-k8s/${name}:${build_version}
docker run -d -p 80:80 -v /data0/${name}/logs:/data/logs --name ${name} --net="host" --ulimit nofile=200000 -v /etc/localtime:/etc/localtime:ro -m 1536M --memory-swap 2048M -e JAVA_OPTS="-server -Xmx1024m" registry.api.xxx.com/bop-k8s/${name}:${build_version}
'''
}

}

}

回滚操作 jenkins job 任务 pipeline script 脚本

jenkins 新建任务 fms-g-rollback 复制 fms-g job 任务
配置 勾选参数化构建过程 选择文本参数
定义名称:tag
默认值: backup
描述:请输入回滚的镜像版本号!默认回滚备份的上一版本 docker 镜像!

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
env.name='fms-g'

env.node1='xx1.com'
env.node2='xx2.com'

node(env.node1){

stage('Rolling back node1'){
withCredentials([string(credentialsId: 'xxxxx-xxxxx', variable: 'HUB_TOKEN')]) {
sh '''
set +e
sleep 5

rollImages=registry.api.xxx.com/bop-k8s/${name}:${tag}
if [ "${tag}" = "backup" ]
then
rollImages=${name}-backup
fi

if [ "$(docker images -q ${rollImages} 2> /dev/null)" == "" ]
then
echo "回滚的镜像name为:${rollImages} 不存在,无法回滚";
exit 1
fi

docker stop --time=20 ${name}
docker rm -f ${name}
docker run -d -v /data0/${name}/logs:/data/logs --name ${name} --net="host" --ulimit nofile=200000 -v /etc/localtime:/etc/localtime:ro -m 1536M --memory-swap 2048M -e JAVA_OPTS="-server -Xmx1024m" ${rollImages}
'''
}

}

}
node(env.node2){

stage('Rolling back node2'){
withCredentials([string(credentialsId: 'xxxxx-xxx', variable: 'HUB_TOKEN')]) {
sh '''
set +e

rollImages=registry.api.xxx.com/bop-k8s/${name}:${tag}
if [ "${tag}" = "backup" ]
then
rollImages=${name}-backup
fi

if [ "$(docker images -q ${rollImages} 2> /dev/null)" == "" ]
then
echo "回滚的镜像name为:${rollImages} 不存在,无法回滚";
exit 1
fi

docker stop --time=20 ${name}
docker rm -f ${name}
docker run -d -p 80:80 -v /data0/${name}/logs:/data/logs --name ${name} --net="host" --ulimit nofile=200000 -v /etc/localtime:/etc/localtime:ro -m 1536M --memory-swap 2048M -e JAVA_OPTS="-server -Xmx1024m" ${rollImages}
'''
}

}

}
Tags: CI&CD
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章