1.环境准备:
我在vmware开了三台ubuntu虚拟机:
192.168.1.101 安装jenkins,jdk1.8,maven3.5.4,安装git
192.168.1.102 安装gitlab
192.168.1.103 安装docker

安装jenkins
安装gitlab

然后将101上的id_rsa.pub添加到gitlab的sshkey中,同时授权101,102主机拉取103的gitlab仓库

ssh-keygen  #会在~/.ssh目录下生成id_rsa和id_rsa.pub,分别是私匙和公匙  最后三台机器都执行,公私匙都是会用到的
ssh-copy-id root@192.168.1.102  #101执行
ssh-copy-id root@192.168.1.102  #103执行

随意在101一个文件夹clone一个开源的博客代码用作测试:

git clone https://github.com/b3log/solo.git

然后将这个提交到我们的gitlab私有仓库

git remote add origin ssh://git@192.168.1.102:2222/liwd/solo.git  #ssh://git@192.168.1.102:2222/liwd/solo.git是我的私人仓库,这里要换成自己的
git add .
git commit -m "all"
git push origin master

2.配置jenkins
注意要提前下载Git Parameter Plug-In和ssh的jenkins插件,然后
系统管理—-全局工具配置:

  • setting.xml和jdk的配置,配置为你安装的相应目录哦

这里写图片描述

  • maven配置
  • 这里写图片描述
    系统管理–全局配置
    这里写图片描述

3.创建一个maven项目,没有该选项需要下载jenkins的maven插件
-丢弃旧的构建,只保存5个,也可保存任意个,自己合理配置即可
这里写图片描述
- 参数化构建,用来选择代码的分支来构建不同的项目名

这里写图片描述

  • 源码管理
    这里写图片描述
  • maven编译

这里写图片描述

  • 构建后操作

这里写图片描述

3.docker脚本
在安装docker那台机器的/data/dockerfiles/scripts目录下写一个脚本:

#!/bin/bash
############################################
# this script function is :                
# deploy new docker container             
#                                          
# USER        YYYY-MM-DD - ACTION          
# junsansi    2016-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 junsansi "junsansi@sina.com"' >> ${DOCKER_FILE}
echo "RUN rm -rf /usr/local/tomcat/webapps/ROOT" >> ${DOCKER_FILE}
echo "ADD *.war /usr/local/tomcat/webapps/ROOT.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 ""

写过dockerfile和bash脚本的都很容易看懂这个脚本做了什么:
拉取tomcat镜像–将war包放到tomcat下的webapps目录下–启动镜像,
jenkins全部布置好后,直接点击项目buildwithparams,点击下面正在编译的进程,然后再点击控制台输出会看到相关日志,等到build success最后访问192.168.1.103:11000即可访问项目,然后会报错,需要修改项目src/main/resources/latke.properties文件:

serverHost=192.168.1.103

serverPort=11000

重新提交,然后重新buildwithparams,重新访问192.168.1.103:11000

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐