码迷,mamicode.com
首页 > 其他好文 > 详细

[ci]jenkins构建容器项目

时间:2017-12-01 18:33:02      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:$2   _id   UI   war   通过   分享图片   over   version   .com   

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

[ci]jenkins构建容器项目

标签:$2   _id   UI   war   通过   分享图片   over   version   .com   

原文地址:http://www.cnblogs.com/iiiiher/p/7943718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!