jenkins+gitlab+docker持續整合部署maven專案

lightTrace發表於2018-08-26

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

相關文章