Docker + Jenkins achieve spring-boot continuous integration project

  • Environmental
    Jenkins installation, prepare a server, install a little, look at Baidu.
    Docker installation, prepare a server, install a little, look at Baidu.

  • Jenkins configuration
    1. Create a Maven project of Job.
      Docker + Jenkins achieve spring-boot continuous integration project
  1. Jenkins Configuration - source
    Docker + Jenkins achieve spring-boot continuous integration project

  2. Jenkins Configuration - Construction of the flip-flop
    Docker + Jenkins achieve spring-boot continuous integration project

  3. Jenkins Configuration - build environment
    Docker + Jenkins achieve spring-boot continuous integration project
    before building, to store configuration files from a centralized GitLab pulls the latest configuration file that contains Dockerfile, start building behind the latest file references.

    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 configuration - Construction and Building After the operation
    Docker + Jenkins achieve spring-boot continuous integration project
    the first synchronization jar package and Dockerfile to Docker that sets server, then expect interactive command sends the shell script to the Docker that sets server, for docker container construction, start, Push to Ali cloud images warehouse and other operations.
    Jenkins this server is rsync script: After bash /home/jenkins/docker-test/script/b2b-rsync.sh b2b-partner-test parameters.
    rsync synchronization scripts:
    #!/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 interactive command script: /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"

    Note: expect before implementation of the above script, users need to ssh to the other server's configuration visudo added: xiaoyu ALL = (ALL) NOPASSWD: ALL, or else the user can not ssh in the past.

  • Docker configuration
    view synchronized to the Docker's Dockerfile, maven build good jar package also synchronize this directory.
    root @ docker-qa b2b-partner -test] # ll
    total volume of 61476
    -rw-r---RW. 1 19:46 1010 1010 62.94409 million B2B-Jul-26 is partner.jar
    -rw-r--. 1 1010 1010 RW-237 10:00 Dockerfile 26 jul
    CAT Dockerfile

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

    docker-qa.sh script executed on the key Docker server

    #!/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 [email protected]:~/.jenkins/workspace/$1/target/b2b-partner.jar $JOBREPO
    scp [email protected]:~/.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
  • Construction of the Jenkins
    build
    Docker + Jenkins achieve spring-boot continuous integration project
    Jenkins console to view build log
    Docker + Jenkins achieve spring-boot continuous integration project

Guess you like

Origin blog.51cto.com/10874766/2425252