构建config-tmp-release项目,此项目执行完成后,自动构建关联的下级项目config-tmp-ms,config-tmp-ms项目会Jenkins中自动拉取最新代码,触发rsync同步脚本,rsync脚本里触发docker镜像build和push操作。Jenkins最终构建完成后,即可将镜像仓库拉取最新交付的镜像。
* 创建基础持续集成项目-gateway
2.1 创建自由风格的软件项目-Job,创建gateway-tmp-release
2.2 创建maven项目-Job,创建基础项目gateway-tmp-ms
2.3 配置gateway-tmp-ms基础项目
基于jdk8,配置gateway-tmp-ms项目Git代码仓库源,并选取release分支。
配置关联项目gateway-tmp-release:
配置构建前操作脚本,构建命令,构建后操作脚本:
2.4配置脚本
构建前配置模板替换,配置成相应的provider,这里为gateway provider:
脚本:sh /home/jenkins/docker-tmp/script/configuration.sh gateway-tmp-ms api-gateway
其中gateway-tmp-ms必须跟项目job名称一样。$1为此项目job名称“gateway-tmp-ms”,$2为此项目代码git@gitlab.alaxiaoyou.com:xxx.git名称"api-gateway"。目的是自动替换各个provider配置,接下来后面各个provider自动替换。
模板文件:
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构建后配置,项目包(maven打包)同步脚本rsync,从git仓库服务器拉取代码打包后,包同步到docker-tmp服务器(rancher服务器)中:
项目jar包同步脚本:sh /home/jenkins/docker-tmp/script/rsync.sh gateway-tmp-ms api-gateway
expect脚本远程触发rancher服务器上脚本: /home/jenkins/docker-tmp/script/image.exp
cat /home/jenkins/docker-tmp/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"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 fi2.5 构建操作