* 环境:
CentOS7
代码仓库:Gitlab
持续集成代码更新:Jenkins
构建打包:Maven
镜像自动交付脚本:Shell
基础环境软件安装:Gitlab、Jenkins、Maven配置,安装参考网上,这里安装略,重点介绍项目持续集成和镜像交付。
结合上一博文:https://www.linuxidc.com/Linux/2019-02/157106.htm
这里配置了两个基础服务容器的自动化构建,一个常规业务服务容器的自动化构建,其他的容器服务持续集成类似操作,这不做重复操作。
* 创建基础持续集成项目-config
1.1 创建自由风格的软件项目-Job,创建config-tmp-release
1.2 创建maven项目-Job,创建基础项目config-tmp-ms
1.3 配置config-tmp-ms基础项目
基于jdk8,配置config-tmp-ms项目Git代码仓库源,并选取release分支。
配置关联项目config-tmp-release:
构建命令及构建后操作脚本:
1.4配置脚本
项目jar包同步脚本:/home/jenkins/docker-tmp/script/rsync.sh config-tmp-ms config-service
其中config-tmp-ms必须跟项目job名称一样。$1为此项目job名称“config-tmp-ms”,$2为此项目代码git@gitlab.alaxiaoyou.com:xxx/alaxiaoyou-config-service.git, provider:config-service。目的是自动替换各个provider配置。
expect脚本远程触发rancher服务器上脚本: /home/jenkins/docker-pro/script/image.exp
cat /home/jenkins/docker-pro/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] spawn ssh name@$ipaddress; expect "password:"; send "$passwd\r"; expect "#" send "sudo nohup /data/docker/layout-script/pro-all.sh $msname $msnameb >> /data/docker/logs/$msname.log &\r" expect eof {exit 1}rancher server服务器上脚本,构建,推送push到阿里镜像仓库:/data/docker/layout-script/pro-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 fi1.5 构建操作