Jenkins+GitLab+Maven+Shell自动化构建实施案例(3)

构建gateway-tmp-release项目,此项目执行完成后,自动构建关联的下级项目gateway-tmp-ms,gateway-tmp-ms项目会Jenkins中自动拉取最新代码,触发rsync同步脚本,rsync脚本里触发docker镜像build和push操作。Jenkins最终构建完成后,即可将镜像仓库拉取最新交付的镜像。

* 创建持续集成项目-sms

3.1 创建maven项目-Job,创建sms-tmp-release

Docker微服务-Jenkins+Gitlab+Maven+Shell自动化构建实施案例

3.2 配置ms-tmp-release项目
基于jdk8,配置sms项目Git代码仓库源,并选取release分支。

Docker微服务-Jenkins+Gitlab+Maven+Shell自动化构建实施案例

构建基于pom.xml进行构建

Docker微服务-Jenkins+Gitlab+Maven+Shell自动化构建实施案例

3.3 创建maven项目-Job,创建sms-tmp-ms

Docker微服务-Jenkins+Gitlab+Maven+Shell自动化构建实施案例

3.4 配置sms-tmp-ms项目
基于jdk8,配置sms项目Git代码仓库源,并选取release分支:

Docker微服务-Jenkins+Gitlab+Maven+Shell自动化构建实施案例

配置关联项目sms-tmp-release:

Docker微服务-Jenkins+Gitlab+Maven+Shell自动化构建实施案例

配置构建前操作脚本,构建命令,构建后操作脚本:

Docker微服务-Jenkins+Gitlab+Maven+Shell自动化构建实施案例

3.5配置脚本
构建前配置模板替换,配置成相应的provider,这里为sms provider:
脚本:sh /home/jenkins/docker-tmp/script/configuration.sh sms-tmp-ms sms-provider

cat /home/jenkins/docker-tmp/script/configuration.sh sms-tmp-ms sms-provider #!/bin/bash sed s/module/alaxiaoyou-$2/g /home/jenkins/.jenkins/workspace/ms-tmp-configuration/module.yml > /home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.yml yes | cp -rfp /home/jenkins/.jenkins/workspace/ms-tmp-configuration/bootstrap.yml /home/jenkins/.jenkins/workspace/$1/src/main/resources/

模板文件:

cat /home/jenkins/.jenkins/workspace/ms-tmp-configuration/module.yml spring: application: name: module cloud: zookeeper: connect-string: zookeeper1:2181,zookeeper2:2181,zookeeper3:2181 config: uri: :20000

其中sms-tmp-ms必须跟项目job名称一样。$1为此项目job名称“sms-tmp-ms”,$2为此项目代码git@gitlab.alaxiaoyou.com:xxx.git名称"sms-provider"。目的是自动替换各个provider配置。

构建后配置,项目包(maven打包)同步脚本rsync,从git仓库服务器拉取代码打包后,包同步到docker-tmp服务器(rancher服务器)中:
脚本:sh /home/jenkins/docker-tmp/script/rsync.sh sms-tmp-ms sms-provider 256m

cat /home/jenkins/docker-tmp/script/rsync.sh sms-tmp-ms sms-provider 256m #!/bin/bash ip=10.1.1.1 #docker tmp server passwd=****** rsync -vzrtopg --numeric-ids --progress --password-file=/etc/rsyncd.password6 /home/jenkins/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar root@$ip::$1 expect /home/jenkins/docker-test/script/image.exp $ip $passwd $1 $2 $3

Jenkins和rancher不在同一台服务器行,用shell expect脚本远程触发rancher服务器上脚本:

cat /home/jenkins/docker-test/script/image.exp #!/usr/bin/expect -f set ipaddress [lindex $argv 0] set passwd [lindex $argv 1] set msname [lindex $argv 2] set msnameb [lindex $argv 3] set jvm [lindex $argv 4] spawn ssh name@$ipaddress; expect "password:"; send "$passwd\r"; expect "#" send "sudo nohup /data/docker/layout-script/all.sh $msname $msnameb $jvm >> /data/docker/logs/$msname.log &\r" expect eof {exit 1}

rancher server服务器上脚本,构建,推送push到阿里镜像仓库:/data/docker/layout-script/all.sh

#!/bin/bash namems=$1 namemsb=$2 version=latest msrepo=/data/docker/$1 imgrepo=xmbaby imgname=$1 if [ ! -d $msrepo ];then mkdir -p $msrepo scp jenkins@10.1.1.1:~/.jenkins/workspace/$1/target/alaxiaoyou-$2-0.0.1-SNAPSHOT.jar $msrepo echo "[$namems]" >> /etc/rsyncd.conf echo "path=/data/docker/$namems/" >> /etc/rsyncd.conf echo "comment = update ignore errors read only = no list = no hosts allow = 10.1.1.1/255.255.255.0 auth users = root uid = root gid = root secrets file = /etc/rsyncd.secrets" >> /etc/rsyncd.conf else echo "开始构建docker镜像" fi echo "FROM registry-internal.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8" > $msrepo/dockerfile echo "MAINTAINER $namems "name@alaxiaoyou.com"" >> $msrepo/dockerfile echo "ADD alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar /data/httpd/" >> $msrepo/dockerfile echo "WORKDIR /data/httpd/" >> $msrepo/dockerfile echo "ENTRYPOINT java -Xmx512m -Xss512k -jar alaxiaoyou-$namemsb-0.0.1-SNAPSHOT.jar" >> $msrepo/dockerfile cd $msrepo docker build -t ms/$namems . if [ $? -ne 0 ];then echo "$namems 镜像构建失败,请检查dockerfile !" exit else imageid=`docker images |grep "ms/$namems" |awk '{print $3}'` docker login --username=name@alaxiaoyou.com --password=****** registry-internal.cn-hangzhou.aliyuncs.com docker tag $imageid registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$version docker push registry-internal.cn-hangzhou.aliyuncs.com/$imgrepo/$imgname:$version fi

3.6 构建操作

Docker微服务-Jenkins+Gitlab+Maven+Shell自动化构建实施案例

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/26b79292d9bfe1885740597cc14ec236.html