欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

基于 Jenkins+Docker+Git 的CI流程初探

程序员文章站 2022-05-18 10:53:51
在如今的互联网时代,随着软件开发复杂度的不断提高,软件开发和发布管理也越来越重要。目前已经形成一套标准的流程,最重要的组成部分就是持续集成(Continuous Integration,CI)及持续部署、交付(CD)。在此,我们来以一个案例初步了解 CI 流程。那么什么是 CI 呢?简单来讲,CI ......

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

ci 流程设计图:

基于 Jenkins+Docker+Git 的CI流程初探

基于 Jenkins+Docker+Git 的CI流程初探

工作流程:

1. 开发人员提交代码到git版本仓库;
2. jenkins人工/定时触发项目构建;
3. jenkins拉取代码、代码编码、打包镜像、推送到镜像仓库;
4. jenkins在docker主机创建容器并发布

主机环境规划:

docker-jenkins:    10.0.0.98

docker-git-harbor:10.0.0.99

1. 部署git代码版本仓库

 安装:

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

配置git用户:

[root@docker-git-harbor ~]# useradd git
[root@docker-git-harbor ~]# passwd git

创建库:

[git@docker-git-harbor ~]$ mkdir tomcat-java-demo.git
[git@docker-git-harbor ~]$ cd tomcat-java-demo.git/
[git@docker-git-harbor tomcat-java-demo.git]$ git --bare init
initialized empty git repository in /home/git/tomcat-java-demo.git/
[git@docker-git-harbor tomcat-java-demo.git]$ ls
branches  config  description  head  hooks  info  objects  refs
[git@docker-git-harbor tomcat-java-demo.git]$

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

[root@docker-jenkins ~]# git clone git@10.0.0.99:/home/git/tomcat-java-demo.git
cloning into 'solo'...
the authenticity of host '10.0.0.99 (10.0.0.99)' can't be established.
ecdsa key fingerprint is sha256:xnwqhgsasqd84k/6oyv3xl1+mpgjtassxev1yvlzvas.
ecdsa key fingerprint is md5:b4:bd:16:2b:de:e7:7c:fd:c5:dd:91:75:20:ff:3e:0a.
are you sure you want to continue connecting (yes/no)? yes
warning: permanently added '10.0.0.99' (ecdsa) to the list of known hosts.
git@10.0.0.99's password: 
warning: you appear to have cloned an empty repository.
[root@docker-jenkins ~]# ls
anaconda-ks.cfg  tomcat-java-demo.git
[root@docker-jenkins ~]# ls tomcat-java-demo.git
[root@docker-jenkins ~]# 

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

[root@docker-jenkins ~]# mv tomcat-java-demo tomcat-java-demo.bak
[root@docker-jenkins ~]# git clone https://github.com/dingkai163/tomcat-java-demo.git
[root@docker-jenkins tomcat-java-demo]# cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = git@10.0.0.99:/home/git/tomcat-java-demo.git  # 修改为本地的git库地址
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[root@docker-jenkins tomcat-java-demo]# git add .
[root@docker-jenkins tomcat-java-demo]# git status
# on branch master
nothing to commit, working directory clean
[root@docker-jenkins tomcat-java-demo]# git commit -m "all"
# on branch master
nothing to commit, working directory clean
[root@docker-jenkins tomcat-java-demo]# git push origin master
git@10.0.0.99's password: 
counting objects: 229, done.
compressing objects: 100% (185/185), done.
writing objects: 100% (229/229), 4.52 mib | 0 bytes/s, done.
total 229 (delta 25), reused 229 (delta 25)
to git@10.0.0.99:/home/git/tomcat-java-demo.git
 * [new branch]      master -> master
[root@docker-jenkins tomcat-java-demo]#

2. jenkins环境部署 

部署jdk环境及maven

[root@docker-jenkins ~]# mkdir tools
[root@docker-jenkins ~]# cd tools
[root@docker-jenkins tools]# rz -y
[root@docker-jenkins tools]# tar zxvf jdk-8u45-linux-x64.tar.gz
[root@docker-jenkins tools]# mv jdk1.8.0_45 /usr/local/jdk
[root@docker-jenkins tools]# vim /etc/profile
java_home=/usr/local/jdk  
path=$path:$java_home/bin  classpath=.:$java_home/lib/dt.jar:$java_home/lib/tools.jar  
export java_home path classpath
[root@docker-jenkins tools]# source /etc/profile
[root@docker-jenkins tools]# java -version
java version "1.8.0_45"
java(tm) se runtime environment (build 1.8.0_45-b14)
java hotspot(tm) 64-bit server vm (build 25.45-b02, mixed mode)

[root@docker-jenkins tools]# tar zxf apache-maven-3.5.0-bin.tar.gz 
[root@docker-jenkins tools]# mv apache-maven-3.5.0 /usr/local/maven

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

[root@docker-jenkins tools]# wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
[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
[root@docker-jenkins tools]# tar zxf apache-tomcat-8.5.38.tar.gz 
[root@docker-jenkins tools]# ls
apache-tomcat-8.5.38  apache-tomcat-8.5.38.tar.gz  jdk-8u45-linux-x64.tar.gz  jenkins.war
[root@docker-jenkins tools]# mv apache-tomcat-8.5.38 /usr/local/tomcat-jenkins
[root@docker-jenkins tools]# ls /usr/local/tomcat-jenkins/webapps/
docs  examples  host-manager  manager  root
[root@docker-jenkins tools]# rm -rf /usr/local/tomcat-jenkins/webapps/*
[root@docker-jenkins tools]# mv jenkins.war /usr/local/tomcat-jenkins/webapps/root.war
[root@docker-jenkins tools]# ll /usr/local/tomcat-jenkins/webapps/
total 75520
-rw-r--r--. 1 root root 77330344 mar 15 00:55 root.war
[root@docker-jenkins tools]# cd /usr/local/tomcat-jenkins/bin/
[root@docker-jenkins bin]# ./startup.sh 
using catalina_base:   /usr/local/tomcat-jenkins
using catalina_home:   /usr/local/tomcat-jenkins
using catalina_tmpdir: /usr/local/tomcat-jenkins/temp
using jre_home:        /usr/local/jdk1.8
using classpath:       /usr/local/tomcat-jenkins/bin/bootstrap.jar:/usr/local/tomcat-jenkins/bin/tomcat-juli.jar
tomcat started.
[root@docker-jenkins bin]#

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

基于 Jenkins+Docker+Git 的CI流程初探

3. 部署私有镜像仓库

企业级harbor镜像仓库部署:

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

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

[root@docker-git-harbor ~]# cd tomcat
[root@docker-git-harbor tomcat]# cat dockerfile-tomcat
from centos:7
maintainer www.cnblogs.com/kaye/

env version=8.5.38

run yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \
    yum clean all && \ 
    rm -rf /var/cache/yum/*
run wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v${version}/bin/apachetomcat-${version}.tar.gz && \
    tar zxf apache-tomcat-${version}.tar.gz && \
    mv apache-tomcat-${version} /usr/local/tomcat && \
    rm -rf apache-tomcat-${version}.tar.gz /usr/local/tomcat/webapps/* && \
    mkdir /usr/local/tomcat/webapps/test && \
    echo "ok" > /usr/local/tomcat/webapps/test/status.html && \
    sed -i '1a java_opts="-djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \
    ln -sf /usr/share/zoneinfo/asia/shanghai /etc/localtime

env path $path:/usr/local/tomcat/bin

expose 8080
cmd ["catalina.sh", "run"]
[root@docker-git-harbor tomcat]# docker build -t tomcat:v1 -f dockerfile-tomcat .
[root@docker-git-harbor tomcat]# docker tag tomcat:v1 reg.dingkai.com/library/tomcat:v1
[root@docker-git-harbor tomcat]# docker login reg.dingkai.com
[root@docker-git-harbor tomcat]# docker push reg.dingkai.com/library/tomcat:v1

4. jenkins安装必要插件

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

基于 Jenkins+Docker+Git 的CI流程初探

基于 Jenkins+Docker+Git 的CI流程初探

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

基于 Jenkins+Docker+Git 的CI流程初探

submit后点击available,check now此时我们可以看到很多可获得插件

基于 Jenkins+Docker+Git 的CI流程初探

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

基于 Jenkins+Docker+Git 的CI流程初探

 

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

 5. 项目创建

创建jobs

 基于 Jenkins+Docker+Git 的CI流程初探

选择流水线类型

基于 Jenkins+Docker+Git 的CI流程初探

 

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

基于 Jenkins+Docker+Git 的CI流程初探

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

基于 Jenkins+Docker+Git 的CI流程初探

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

[root@docker-jenkins bin]# ssh-keygen 
generating public/private rsa key pair.
enter file in which to save the key (/root/.ssh/id_rsa): 
enter passphrase (empty for no passphrase): 
enter same passphrase again: 
your identification has been saved in /root/.ssh/id_rsa.
your public key has been saved in /root/.ssh/id_rsa.pub.
the key fingerprint is:
sha256:1vd8xm3ldyfmrxgabwptlwb0fl+oex8cfgtykpg4odi root@docker-jenkins
the key's randomart image is:
+---[rsa 2048]----+
|   . o==o.o ...  |
|e o o +=++ .+o . |
| o . . .*o o+.. .|
|       oo*oo o.=o|
|       .s.*o+.o =|
|       . ..b.+   |
|          o =    |
|           .     |
|                 |
+----[sha256]-----+
[root@docker-jenkins bin]# cd
[root@docker-jenkins ~]# 
[root@docker-jenkins ~]# 
[root@docker-jenkins ~]# ls .ssh/
id_rsa  id_rsa.pub  known_hosts
[root@docker-jenkins ~]# ssh-copy-id git@10.0.0.99
/usr/bin/ssh-copy-id: info: source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: info: attempting to log in with the new key(s), to filter out any that are already installed
/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
git@10.0.0.99's password: 

number of key(s) added: 1

now try logging into the machine, with:   "ssh 'git@10.0.0.99'"
and check to make sure that only the key(s) you wanted were added.

[root@docker-jenkins ~]#

基于 Jenkins+Docker+Git 的CI流程初探

 基于 Jenkins+Docker+Git 的CI流程初探

 

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

基于 Jenkins+Docker+Git 的CI流程初探

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

node { 
   // 拉取代码
   stage('git checkout') { 
       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']]])
   }
   // 代码编译
   stage('maven build') {
        sh '''
        export java_home=/usr/local/jdk
        /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}
cat > dockerfile << eof
from reg.dingkai.com/library/tomcat:v1 
maintainer kai
run rm -rf /usr/local/tomcat/webapps/*
add target/*.war /usr/local/tomcat/webapps/root.war
eof
docker build -t $repository .
docker login reg.dingkai.com -u admin -p 123456
docker push $repository
'''
   }
   // 部署到docker主机
   stage('deploy to docker') {
        sh '''
        repository=reg.dingkai.com/library/tomcat-java-demo:${branch}
        docker rm -f tomcat-java-demo |true
        docker pull $repository
        docker container run -d --name tomcat-java-demo -p 88:8080 $repository
        '''
   }
}

基于 Jenkins+Docker+Git 的CI流程初探

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

基于 Jenkins+Docker+Git 的CI流程初探

然后保存配置。

6. 开始构建任务

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

基于 Jenkins+Docker+Git 的CI流程初探

可以通过console output输出查看jenkins构建流程

基于 Jenkins+Docker+Git 的CI流程初探

成功构建会提示: success

基于 Jenkins+Docker+Git 的CI流程初探

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

基于 Jenkins+Docker+Git 的CI流程初探

通过浏览器来访问tomcat-java-demo项目:http://10.0.0.98:88/

基于 Jenkins+Docker+Git 的CI流程初探

可以看到可以正常访问,至此基于 jenkins+docker+git 的ci流程已经成功部署了。