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

Jenkins+Docker+Git集成平台搭建

时间:2018-04-19 15:41:09      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:chm   function   initial   class   version   ota   chmod   file   yum   

一、环境准备

以我本地为例【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 "hzf@centit.com"‘ >> ${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集成平台搭建

标签:chm   function   initial   class   version   ota   chmod   file   yum   

原文地址:https://www.cnblogs.com/hululu160810/p/8883212.html

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