[ci]jenkins构建容器项目java-helloworld-非docker plugin模式

栗子思路说明:

  • 不使用任何docker plugin
  • 使用jenkins server本地(含mvn环境)构建,无jenkins slave
  • jenkins server本地构建的war包,推送deploy机,使用sh动态生成镜像(含镜像名/标签[dev/test]等),并删掉原来的镜像,启动.

jenkins构建容器项目

初始化web节点目录

$ tree /data/dockerfiles/
/data/dockerfiles/
├── first_docker_proj01
│   ├── Dockerfile
│   └── trucks.war
├── scripts
│   └── deploy.sh            # 当执行这个脚本时候,将会将这里的*.war干掉,将最新的war目录下东西移进来.并开始构建镜像
└── war
    └── first_docker_proj01 ## jenkins server构建好后,将war包通过publish over ssh插件推送到这个目录.

系统管理->系统设置->Publish over SSH

新建maven风格的项目

https://github.com/lannyMa/trucks.git

$release_branch

clean package -DskipTests=true

target/*.war
target/
/war/first_docker_proj01
/data/dockerfiles/scripts/deploy.sh first_docker_proj01 hello_docker $release_branch 11000 8080

效果

可以实现一键切换脚本, web节点在启动新容器时候会把老版本容器干掉.

deploy.sh

主要是这个脚本牛x

#!/bin/bash
############################################
# this script function is :               
# deploy new docker container            
#                                         
# USER        YYYY-MM-DD - ACTION         
# maotai    2018-01-25 - CREATED
#                                         
############################################ 
parasnum=5
# function
help_msg()
{
cat << help
+----------------------------------------------------+
+ Error Cause:
+ you enter $# parameters
+ the total paramenter number must be $parasnum
+ 1st :DOCKER_NAME
+ 2nd :PROJECT_NAME
+ 3rd :PROJECT_VERSION
+ 4th :SOURCE_PORT
+ 5th :DESTINATION_PORT
+----------------------------------------------------+
help
} 
# ----------------------------------------------------
# Check parameter number
# ----------------------------------------------------
if [ $# -ne ${parasnum} ]
then
        help_msg
        exit
fi 
# ----------------------------------------------------
# Initialize the parameter.
# ----------------------------------------------------
DOCKER_NAME=$1
PROJECT_NAME=$2
PROJ_VERSION=$3
SPORT=$4
DPORT=$5 
PROJ_VERSION=${PROJ_VERSION/"origin/"/""} 
DOCKER_FILE="/data/dockerfiles/${DOCKER_NAME}/Dockerfile"
DOCKER_FILE_DIR=/data/dockerfiles/${DOCKER_NAME}
if [ ! -d ${DOCKER_FILE_DIR} ]; then
        mkdir -p ${DOCKER_FILE_DIR}
fi 
# ----------------------------------------------------
# check docker images
# ----------------------------------------------------
DOCKER_IMAGE=`/usr/bin/docker images | grep ${DOCKER_NAME} | awk -F ' ' '{print $3}'`
if [ -n "${DOCKER_IMAGE}" ]; then
        # check docker container
        for dc in `/usr/bin/docker ps -a | grep ${DOCKER_NAME} | awk -F " " '{print $1}'`
        do
                echo "Stop container: ${dc}"
                /usr/bin/docker stop ${dc}
                # delete while docker container was exists
                echo "##Delete exists Container_Id: "${dc}
                /usr/bin/docker rm ${dc}
        done 
        # delete while docker image was exists
        echo "##Delete exists Image: "${DOCKER_IMAGE}
        /usr/bin/docker rmi ${DOCKER_IMAGE}
fi
echo "" 
# ----------------------------------------------------
# Init dockerfile
# ----------------------------------------------------
echo "**Init dockerfile start: "${DOCKER_FILE}
echo "FROM tomcat" > ${DOCKER_FILE}
echo 'MAINTAINER maotai "maotai@sina.com"' >> ${DOCKER_FILE}
echo "ADD *.war /usr/local/tomcat/webapps/${PROJECT_NAME}.war" >> ${DOCKER_FILE}
echo "EXPOSE 8080" >> ${DOCKER_FILE}
echo "CMD /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out" >> ${DOCKER_FILE}
cat ${DOCKER_FILE}
echo "**Init dockerfile end." 
# ----------------------------------------------------
# Build dockerfile
# ----------------------------------------------------
cd ${DOCKER_FILE_DIR}
rm *.war -rf
mv /data/dockerfiles/war/${DOCKER_NAME}/*.war ./
echo ""
echo "##Build dockerfile for "${DOCKER_NAME}
/usr/bin/docker build -t ${DOCKER_NAME}:${PROJ_VERSION} . 
# ----------------------------------------------------
# Run docker container
# ----------------------------------------------------
echo ""
echo "##Running docker container: "${DOCKER_NAME}
/usr/bin/docker run --name ${DOCKER_NAME}_d1 -d -p ${SPORT}:${DPORT} ${DOCKER_NAME}:${PROJ_VERSION} 
#/usr/bin/docker run ${cmd} 
echo ""

参考:
http://blog.itpub.net/7607759/viewspace-2134637/
http://www.cnblogs.com/iiiiher/p/7943097.html

posted @ 2017-12-01 17:48  _毛台  阅读(715)  评论(0编辑  收藏  举报