持续基础怎么搞?Jenkins+Docker+Git实战

在如今的互联网时代,随着软件开发复杂度的不断提高,软件开发和发布管理也越来越重要。目前已经形成一套标准的流程,最重要的组成部分就是持续集成(Continuous Integration,CI)及持续部署、交付(CD)。在此,我们来以一个案例初步了解 CI 流程。那么什么是 CI 呢?简单来讲,CI 就是将传统的代码合并、构建、部署、测试都集成在一起,不断地执行这个过程,并对结果进行反馈。

CI 流程设计图:

工作流程:

  • 开发人员提交代码到Git版本仓库;

  • Jenkins人工/定时触发项目构建;

  • Jenkins拉取代码、代码编码、打包镜像、推送到镜像仓库;

  • Jenkins在Docker主机创建容器并发布

主机环境规划:

  • docker-jenkins:    10.0.0.98

  • docker-git-harbor:10.0.0.99

01 部署Git代码版本仓库

安装:

[root@docker-git-harbor ~]# yum install git -y

配置git用户:

 
  1. [root@docker-git-harbor ~]# useradd git

  2. [root@docker-git-harbor ~]# passwd git

创建库:

 
  1. [git@docker-git-harbor ~]$ mkdir tomcat-java-demo.git

  2. [git@docker-git-harbor ~]$ cd tomcat-java-demo.git/

  3. [git@docker-git-harbor tomcat-java-demo.git]$ git --bare init

  4. Initialized empty Git repository in /home/git/tomcat-java-demo.git/

  5. [git@docker-git-harbor tomcat-java-demo.git]$ ls

  6. branches config description HEAD hooks info objects refs

  7. [git@docker-git-harbor tomcat-java-demo.git]$

另一台docker-jenkins访问该仓库:

 
  1. [root@docker-jenkins ~]# git clone git@10.0.0.99:/home/git/tomcat-java-demo.git

  2. Cloning into 'solo'...

  3. The authenticity of host '10.0.0.99 (10.0.0.99)' can't be established.

  4. ECDSA key fingerprint is SHA256:XNWQhGsAsqd84k/6OYV3xl1+mPGjtASsxeV1YVLZVas.

  5. ECDSA key fingerprint is MD5:b4:bd:16:2b:de:e7:7c:fd:c5:dd:91:75:20:ff:3e:0a.

  6. Are you sure you want to continue connecting (yes/no)? yes

  7. Warning: Permanently added '10.0.0.99' (ECDSA) to the list of known hosts.

  8. git@10.0.0.99's password:

  9. warning: You appear to have cloned an empty repository.

  10. [root@docker-jenkins ~]# ls

  11. anaconda-ks.cfg tomcat-java-demo.git

  12. [root@docker-jenkins ~]# ls tomcat-java-demo.git

  13. [root@docker-jenkins ~]#

模拟生产项目,拉取github上的一个demo,并上传至本地git库

 
  1. [root@docker-jenkins ~]# mv tomcat-java-demo tomcat-java-demo.bak

  2. [root@docker-jenkins ~]# git clone https://github.com/dingkai163/tomcat-java-demo.git

  3. [root@docker-jenkins tomcat-java-demo]# cat .git/config

  4. [core]

  5. repositoryformatversion = 0

  6. filemode = true

  7. bare = false

  8. logallrefupdates = true

  9. [remote "origin"]

  10. url = git@10.0.0.99:/home/git/tomcat-java-demo.git # 修改为本地的git库地址

  11. fetch = +refs/heads/*:refs/remotes/origin/*

  12. [branch "master"]

  13. remote = origin

  14. merge = refs/heads/master

  15. [root@docker-jenkins tomcat-java-demo]# git add .

  16. [root@docker-jenkins tomcat-java-demo]# git status

  17. # On branch master

  18. nothing to commit, working directory clean

  19. [root@docker-jenkins tomcat-java-demo]# git commit -m "all"

  20. # On branch master

  21. nothing to commit, working directory clean

  22. [root@docker-jenkins tomcat-java-demo]# git push origin master

  23. git@10.0.0.99's password:

  24. Counting objects: 229, done.

  25. Compressing objects: 100% (185/185), done.

  26. Writing objects: 100% (229/229), 4.52 MiB | 0 bytes/s, done.

  27. Total 229 (delta 25), reused 229 (delta 25)

  28. To git@10.0.0.99:/home/git/tomcat-java-demo.git

  29. * [new branch] master -> master

  30. [ root edocker-jenkina tomcat- j ava -demo]#

02 jenkins环境部署

部署jdk环境及maven

 
  1. [root@docker-jenkins ~]# mkdir tools

  2. [root@docker-jenkins ~]# cd tools

  3. [root@docker-jenkins tools]# rz -y

  4. [root@docker-jenkins tools]# tar zxvf jdk-8u45-linux-x64.tar.gz

  5. [root@docker-jenkins tools]# mv jdk1.8.0_45 /usr/local/jdk

  6. [root@docker-jenkins tools]# vim /etc/profile

  7. JAVA_HOME=/usr/local/jdk

  8. PATH=$PATH:$JAVA_HOME/bin CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

  9. export JAVA_HOME PATH CLASSPATH

  10. [root@docker-jenkins tools]# source /etc/profile

  11. [root@docker-jenkins tools]# java -version

  12. java version "1.8.0_45"

  13. Java(TM) SE Runtime Environment (build 1.8.0_45-b14)

  14. Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

  15. [root@docker-jenkins tools]# tar zxf apache-maven-3.5.0-bin.tar.gz

  16. [root@docker-jenkins tools]# mv apache-maven-3.5.0 /usr/local/maven

在10.0.0.98主机安装Jenkins,下载Tomcat二进制包将war包到webapps下即可:

 
  1. [root@docker-jenkins tools]# wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war

  2. [root@docker-jenkins tools]# wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38.tar.gz

  3. [root@docker-jenkins tools]# tar zxf apache-tomcat-8.5.38.tar.gz

  4. [root@docker-jenkins tools]# ls

  5. apache-tomcat-8.5.38 apache-tomcat-8.5.38.tar.gz jdk-8u45-linux-x64.tar.gz jenkins.war

  6. [root@docker-jenkins tools]# mv apache-tomcat-8.5.38 /usr/local/tomcat-jenkins

  7. [root@docker-jenkins tools]# ls /usr/local/tomcat-jenkins/webapps/

  8. docs examples host-manager manager ROOT

  9. [root@docker-jenkins tools]# rm -rf /usr/local/tomcat-jenkins/webapps/*

  10. [root@docker-jenkins tools]# mv jenkins.war /usr/local/tomcat-jenkins/webapps/ROOT.war

  11. [root@docker-jenkins tools]# ll /usr/local/tomcat-jenkins/webapps/

  12. total 75520

  13. -rw-r--r--. 1 root root 77330344 Mar 15 00:55 ROOT.war

  14. [root@docker-jenkins tools]# cd /usr/local/tomcat-jenkins/bin/

  15. [root@docker-jenkins bin]# ./startup.sh

  16. Using CATALINA_BASE: /usr/local/tomcat-jenkins

  17. Using CATALINA_HOME: /usr/local/tomcat-jenkins

  18. Using CATALINA_TMPDIR: /usr/local/tomcat-jenkins/temp

  19. Using JRE_HOME: /usr/local/jdk1.8

  20. Using CLASSPATH: /usr/local/tomcat-jenkins/bin/bootstrap.jar:/usr/local/tomcat-jenkins/bin/tomcat-juli.jar

  21. Tomcat started.

  22. [root@docker-jenkins bin]#

启动后,浏览器访问http://10.0.0.98:8080/,按提示输入密码,登录即可。

03 部署私有镜像仓库

企业级harbor镜像仓库部署:

https://www.cnblogs.com/kaye/p/10524391.html

构建Tomcat基础镜像,并推送到harbor镜像库:

 
  1. [root@docker-git-harbor ~]# cd tomcat

  2. [root@docker-git-harbor tomcat]# cat Dockerfile-tomcat

  3. FROM centos:7

  4. MAINTAINER www.cnblogs.com/kaye/

  5. ENV VERSION=8.5.38

  6. RUN yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \

  7. yum clean all && \

  8. rm -rf /var/cache/yum/*

  9. RUN wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v${VERSION}/bin/apachetomcat-${VERSION}.tar.gz && \

  10. tar zxf apache-tomcat-${VERSION}.tar.gz && \

  11. mv apache-tomcat-${VERSION} /usr/local/tomcat && \

  12. rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/* && \

  13. mkdir /usr/local/tomcat/webapps/test && \

  14. echo "ok" > /usr/local/tomcat/webapps/test/status.html && \

  15. sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \

  16. ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  17. ENV PATH $PATH:/usr/local/tomcat/bin

  18. EXPOSE 8080

  19. CMD ["catalina.sh", "run"]

  20. [root@docker-git-harbor tomcat]# docker build -t tomcat:v1 -f Dockerfile-tomcat .

  21. [root@docker-git-harbor tomcat]# docker tag tomcat:v1 reg.dingkai.com/library/tomcat:v1

  22. [root@docker-git-harbor tomcat]# docker login reg.dingkai.com

  23. [root@docker-git-harbor tomcat]# docker push reg.dingkai.com/library/tomcat:v1

04 Jenkins安装必要插件

由于jenkins是离线安装,所有在此需要配置一下插件下载地址:系统管理-->插件管理-->Advanced

修改下方地址,将https修改为http 再点Submit

Submit后点击Available,Check now此时我们可以看到很多可获得插件

首先搜索并安装Pipeline插件
pipeline 是一套运行于jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视化。

再安装SCM to job 插件,同上步骤(搜索,安装)。

05 项目创建

创建jobs

选择流水线类型

到这里我们就开始配置Pipeline script,点击Pipeline语法,来自动生成我们需要的配置。

如下图,我们Git方式,配置Git仓库地址,再添加认证相关

这里我们使用的是秘钥认证方式,需要将jenkins上生成的公钥发送到git服务器上,然后将jenkins上的生成的私钥内容粘贴到下图Key中,这样jenkins就可以免交互的拉取git仓库中的代码了。

 
  1. [root@docker-jenkins bin]# ssh-keygen

  2. Generating public/private rsa key pair.

  3. Enter file in which to save the key (/root/.ssh/id_rsa):

  4. Enter passphrase (empty for no passphrase):

  5. Enter same passphrase again:

  6. Your identification has been saved in /root/.ssh/id_rsa.

  7. Your public key has been saved in /root/.ssh/id_rsa.pub.

  8. The key fingerprint is:

  9. SHA256:1vD8XM3lDYFmrxgAbwPTLwb0fl+oEx8cFgtykpg4ODI root@

  10. docker-jenkins

  11. The key's randomart image is:

  12. +---[RSA 2048]----+

  13. | . o==o.o ... |

  14. |E o o +=++ .+o . |

  15. | o . . .*o o+.. .|

  16. | oo*oo o.=o|

  17. | .S.*o+.o =|

  18. | . ..B.+ |

  19. | o = |

  20. | . |

  21. | |

  22. +----[SHA256]-----+

  23. [root@docker-jenkins bin]# cd

  24. [root@docker-jenkins ~]#

  25. [root@docker-jenkins ~]#

  26. [root@docker-jenkins ~]# ls .ssh/

  27. id_rsa id_rsa.pub known_hosts

  28. [root@docker-jenkins ~]# ssh-copy-id git@10.0.0.99

  29. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

  30. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

  31. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

  32. git@10.0.0.99's password:

  33. Number of key(s) added: 1

  34. Now try logging into the machine, with: "ssh 'git@10.0.0.99'"

  35. and check to make sure that only the key(s) you wanted were added.

  36. [root@docker-jenkins ~]#

配置完成后,我们就可以生成Pipeline脚本了。点击下方Generate Pipeline Script,然后复制方框内的内容。

编写我们所需要的Pipeline脚本如下,将其粘贴到script的拉取代码模块中,并修改分支master为${branch},其他模块内容自行编写。

 
  1. node {

  2. // 拉取代码 stage('Git Checkout') {

  3. checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c42214eb-db1e-49fb-83c9-f78d1f4310a2', url: 'git@10.0.0.99:/home/git/tomcat-java-demo.git']]])

  4. } // 代码编译 stage('Maven Build') {

  5. sh '''

  6. export JAVA_HOME=/usr/local/jdk

  7. /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true ''' } // 项目打包到镜像并推送到镜像仓库

  8. /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true ''' } // 项目打包到镜像并推送到镜像仓库 stage('Build and Push Image') {sh '''REPOSITORY=reg.dingkai.com/library/tomcat-java-demo:${branch}

  9. cat > Dockerfile << EOFFROM reg.dingkai.com/library/tomcat:v1

  10. EOF

  11. docker build -t $REPOSITORY .

  12. docker login reg.dingkai.com -u admin -p 123456

  13. docker push $REPOSITORY'''

  14. }

  15. // 部署到Docker主机

  16. '''

  17. }

  18. // 部署到Docker主机

  19. stage('Deploy to Docker') {

  20. sh '''

  21. REPOSITORY=reg.dingkai.com/library/tomcat-java-demo:${branch}

  22. docker rm -f tomcat-java-demo |true

  23. docker pull $REPOSITORY

  24. docker container run -d --name tomcat-java-demo -p 88:8080 $REPOSITORY

  25. '''

  26. }

  27. }

在Pipeline脚本里面我们指定了一个branch参数,所以我们需要传递一个参数变量,这里我们选择参数化构建,默认值为master分支。

然后保存配置。

06 开始构建任务

回到主界面,我们开始构建任务

可以通过Console Output输出查看jenkins构建流程

成功构建会提示: SUCCESS

我们也可以查看构建成功后的图形构建过程

 

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值