Docker+Jenkins实现spring-boot项目持续集成

  • 环境
    Jenkins安装,准备一台server,安装略,百度一下。
    Docker安装,准备一台server,安装略,百度一下。

  • Jenkins上配置
    1. 新建一Maven的Job项目。
      Docker+Jenkins实现spring-boot项目持续集成
  1. Jenkins配置-源码
    Docker+Jenkins实现spring-boot项目持续集成

  2. Jenkins配置-构建触发器
    Docker+Jenkins实现spring-boot项目持续集成

  3. Jenkins配置-构建环境
    Docker+Jenkins实现spring-boot项目持续集成
    构建前,从集中存放配置文件的GitLab中拉取最新配置文件,包含Dockerfile,后面开始构建会引用最新文件。

    cd /data/httpd/release/b2b-configuration;git pull
    sleep 3;
    yes | cp -rfp  /data/httpd/release/b2b-configuration/Spring-partner/QA/application.yml  /home/jenkins/.jenkins/workspace/b2b-partner-test/src/main/resources/
    yes | cp -rfp  /data/httpd/release/b2b-configuration/Spring-partner/QA/Dockerfile  /home/jenkins/.jenkins/workspace/b2b-partner-test/
  4. Jenkins配置-构建和构建后操作
    Docker+Jenkins实现spring-boot项目持续集成
    先同步jar包和Dockerfile到Docker那台server上,然后用expect交互命令将shell脚本发送到Docker那台server上,进行docker容器的构建、启动、push到阿里云镜像仓库等操作。
    Jenkins这台server的rsync脚本:bash /home/jenkins/docker-test/script/b2b-rsync.sh b2b-partner-test 后带参数。
    rsync同步脚本:
    #!/bin/bash
    IP=10.10.10.10
    PASSWD=p@ssword1
    echo -e "开始同步jar包..."
    rsync -vzrtopg --numeric-ids --progress --password-file=/etc/rsyncd.password6 /home/jenkins/.jenkins/workspace/$1/target/b2b-partner.jar root@$IP::$1
    echo -e "开始同步Dockerfile..."
    rsync -vzrtopg --numeric-ids --progress --password-file=/etc/rsyncd.password6 /home/jenkins/.jenkins/workspace/$1/Dockerfile root@$IP::$1
    expect /home/jenkins/docker-test/script/image_b2b.exp $IP $PASSWD $1

    expect交互命令脚本:/home/jenkins/docker-test/script/image_b2b.exp

    #!/usr/bin/expect -f
    set ipaddress [lindex $argv 0]
    set passwd [lindex $argv 1]
    set jobname [lindex $argv 2]
    spawn ssh xiaoyu@$ipaddress;
    expect "password:";
    send "$passwd\r";
    expect "#"
    send "sudo nohup /data/docker/layout-script/docker-qa.sh $jobname  >> /data/docker/logs/$jobname.log &\r"

    注意:执行上面expect脚本前,需将ssh的用户,到对方server的visudo配置中加入:xiaoyu ALL=(ALL) NOPASSWD: ALL, 要不然用户无法ssh过去。

  • Docker配置
    查看同步到Docker的Dockerfile,maven构建好的jar包也同步到此目录下。
    root@docker-qa b2b-partner-test]# ll
    总用量 61476
    -rw-rw-r-- 1 1010 1010 62944090 Jul 26 19:46 b2b-partner.jar
    -rw-rw-r-- 1 1010 1010 237 Jul 26 10:00 Dockerfile
    cat Dockerfile

    FROM registry.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8
    EXPOSE 8081
    MAINTAINER b2b-partner-test yuhuanghui@alaxiaoyou.com
    ADD b2b-partner.jar /data/httpd/
    WORKDIR /data/httpd/
    ENTRYPOINT java -Xmx256m -Xss512k -jar b2b-partner.jar

    关键在Docker server上执行的docker-qa.sh脚本

    #!/bin/bash
    JOBMS=$1
    VERSION=latest
    JOBREPO=/data/docker/$1
    IMGREPO=xmbaby-test
    IMGNAME=$1
    DTIME=`date +%Y-%m-%d" "%H":"%M":"%S`
    CONTAINER_NAME=b2b-partner-test
    if [ ! -d $JOBREPO ];then
    mkdir -p $JOBREPO
    scp jenkins@10.10.10.10:~/.jenkins/workspace/$1/target/b2b-partner.jar $JOBREPO
    scp jenkins@10.10.10.10:~/.jenkins/workspace/$1/Dockerfile $JOBREPO
    echo -e "\n[$JOBMS]" >> /etc/rsyncd.conf
    echo "path=/data/docker/$JOBMS/" >> /etc/rsyncd.conf
    echo "comment = update
    ignore errors
    read only = no
    list = no
    hosts allow = 10.10.10.10/255.255.255.0
    auth users = root
    uid = root
    gid = root
    secrets file = /etc/rsyncd.secrets" >> /etc/rsyncd.conf
    else
    echo -e "\n时间$DTIME,开始构建job-docker镜像"
    fi
    cd $JOBREPO
    docker build -t ms-b2b/$JOBMS .
    if [ $? -ne 0 ];then
    echo -e "时间$DTIME,$JOBMS 镜像构建失败,请检查dockerfile !"
    exit
    else
    echo -e "时间$DTIME,开始删除之前job-Docker容器."
    docker rm $CONTAINER_NAME -f
    echo -e "时间$DTIME,开始运行job-Docker容器."
    docker run --name $CONTAINER_NAME -v $JOBREPO:$JOBREPO -d -p 8081:8081 ms-b2b/$JOBMS
    sleep 5
    echo -e "时间$DTIME,$CONTAINER_NAME容器创建完成.开始推送到阿里镜像仓库中..."
    IMAGEID=`docker images |grep "ms-b2b/$JOBMS" |awk '{print $3}'`
    echo "Image镜像ID:$IMAGEID"
    CONTAINERID=`docker images |grep "ms-b2b/$JOBMS" |awk '{print $1}'`
    echo "Container容器:$CONTAINERID"
    docker login --username=username --password=PASSWD registry.cn-hangzhou.aliyuncs.com
    docker tag $IMAGEID registry.cn-hangzhou.aliyuncs.com/$IMGREPO/$IMGNAME:$VERSION
    docker push registry.cn-hangzhou.aliyuncs.com/$IMGREPO/$IMGNAME:$VERSION
    fi
  • Jenkins上构建
    构建
    Docker+Jenkins实现spring-boot项目持续集成
    Jenkins控制台查看构建log
    Docker+Jenkins实现spring-boot项目持续集成
上一篇:出题人的女装(牛客练习赛38题B) (概率+分式运算)


下一篇:理解ADFS相关概念