Jenkins+Docker+Git集成平台搭建

Posted hululu160810

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins+Docker+Git集成平台搭建相关的知识,希望对你有一定的参考价值。

一、环境准备

以我本地为例【Jenkins:192.168.132.52           Docker:192.168.10.143】

1、一台运行Jenkins服务的服务器,需安装插件:git plugin、Git Parameter Plug-In、GitLab Plugin、Publish Over SSH、SSH Slaves plugin

2、一台或若干Docker宿主机服务器

3、一台提供Git服务的服务器(直接使用github服务)

二、搭建Docker宿主机

(1)、安装并启动docker

#sudo yum install docker
#sudo service docker start

(2)、创建docker账户、密码

#useradd docker -d /home/docker -s /bin/bash
#echo "docker:docker" | chpasswd

(3)、将docker创建到指定账户下

#mkdir /data/dockerfiles/scripts -p

(4)、将脚本deploy.sh复制到/data/dockerfiles/scripts下

#vi /data/dockerfiles/scripts/deploy.sh
#chmod +x /data/dockerfiles/scripts/deploy.sh
#chown docker:docker /data/dockerfiles -R
#ls -al /data/dockerfiles/

delpoy.sh 源码

  1 #!/bin/bash
  2 ############################################
  3 # this script function is :               
  4 # deploy new docker container            
  5 #                                         
  6 # USER        YYYY-MM-DD - ACTION         
  7 # huzhifei    2017-11-20 - CREATED       
  8 #                                         
  9 ############################################
 10  
 11 parasnum=5
 12 # function
 13 help_msg()
 14 {
 15 cat << help
 16 +----------------------------------------------------+
 17 + Error Cause:
 18 + you enter $# parameters
 19 + the total paramenter number must be $parasnum
 20 + 1st :DOCKER_NAME
 21 + 2nd :PROJECT_NAME
 22 + 3rd :PROJECT_VERSION
 23 + 4th :SOURCE_PORT
 24 + 5th :DESTINATION_PORT
 25 +----------------------------------------------------+
 26 help
 27 }
 28  
 29 # ----------------------------------------------------
 30 # Check parameter number
 31 # ----------------------------------------------------
 32 if [ $# -ne ${parasnum} ]
 33 then
 34         help_msg
 35         exit
 36 fi
 37  
 38 # ----------------------------------------------------
 39 # Initialize the parameter.
 40 # ----------------------------------------------------
 41 DOCKER_NAME=$1
 42 PROJECT_NAME=$2
 43 PROJ_VERSION=$3
 44 SPORT=$4
 45 DPORT=$5
 46  
 47 PROJ_VERSION=${PROJ_VERSION/"origin/"/""}
 48  
 49 DOCKER_FILE="/data/dockerfiles/${DOCKER_NAME}/Dockerfile"
 50 DOCKER_FILE_DIR=/data/dockerfiles/${DOCKER_NAME}
 51 if [ ! -d ${DOCKER_FILE_DIR} ]; then
 52         mkdir -p ${DOCKER_FILE_DIR}
 53 fi
 54  
 55 # ----------------------------------------------------
 56 # check docker images
 57 # ----------------------------------------------------
 58 DOCKER_IMAGE=`/usr/bin/docker images | grep ${DOCKER_NAME} | awk -F ‘ ‘ ‘{print $3}‘`
 59 if [ -n "${DOCKER_IMAGE}" ]; then
 60         # check docker container
 61         for dc in `/usr/bin/docker ps -a | grep ${DOCKER_NAME} | awk -F " " ‘{print $1}‘`
 62         do
 63                 echo "Stop container: ${dc}"
 64                 /usr/bin/docker stop ${dc}
 65                 # delete while docker container was exists
 66                 echo "##Delete exists Container_Id: "${dc}
 67                 /usr/bin/docker rm ${dc}
 68         done
 69  
 70         # delete while docker image was exists
 71         echo "##Delete exists Image: "${DOCKER_IMAGE}
 72         /usr/bin/docker rmi ${DOCKER_IMAGE}
 73 fi
 74 echo ""
 75  
 76 # ----------------------------------------------------
 77 # Init dockerfile
 78 # ----------------------------------------------------
 79 echo "**Init dockerfile start: "${DOCKER_FILE}
 80 echo "FROM tomcat" > ${DOCKER_FILE}
 81 echo ‘MAINTAINER hzf "[email protected]"‘ >> ${DOCKER_FILE}
 82 echo "ADD *.war /usr/local/tomcat/webapps/${PROJECT_NAME}.war" >> ${DOCKER_FILE}
 83 echo "EXPOSE 8080" >> ${DOCKER_FILE}
 84 echo "CMD /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out" >> ${DOCKER_FILE}
 85 cat ${DOCKER_FILE}
 86 echo "**Init dockerfile end."
 87  
 88 # ----------------------------------------------------
 89 # Build dockerfile
 90 # ----------------------------------------------------
 91 cd ${DOCKER_FILE_DIR}
 92 rm *.war -rf
 93 mv /data/dockerfiles/war/${DOCKER_NAME}/*.war ./
 94 echo ""
 95 echo "##Build dockerfile for "${DOCKER_NAME}
 96 /usr/bin/docker build -t ${DOCKER_NAME}:${PROJ_VERSION} .
 97  
 98  
 99 # ----------------------------------------------------
100 # Run docker container
101 # ----------------------------------------------------
102 echo ""
103 echo "##Running docker container: "${DOCKER_NAME}
104 /usr/bin/docker run --name ${DOCKER_NAME}_d1 -d -p ${SPORT}:${DPORT} ${DOCKER_NAME}:${PROJ_VERSION}
105  
106 #/usr/bin/docker run ${cmd}
107  
108 echo ""

 

三、Jenkins+Docker+Git集成

(1)、配置jenkins全局配置项(Maven、Git、JDK)

(2)、系统配置

技术分享图片技术分享图片

(3)、创建并配置job

技术分享图片

技术分享图片

技术分享图片

 

(4)、构建并查看控制台输出

技术分享图片

(5)、访问网站:http://192.168.10.143:12000/docker-centit/

技术分享图片

以上是关于Jenkins+Docker+Git集成平台搭建的主要内容,如果未能解决你的问题,请参考以下文章

CI/CD——构建企业级Docker+Jenkins+Git+Harbor流水线自动化持续集成持续发布平台

Git+Docker+Jenkins持续集成

Git+Docker+Jenkins持续集成

从零开始搭建Jenkins+Docker自动化集成环境

Gitlab+Jenkins+Docker+Harbor+K8s集群搭建CICD平台

Jenkins + Git + Maven + tomcat集成环境搭建