Experience from 0-1 Jenkins + Docker + Git + Registry CI achieve automation release

I. Introduction

Jenkins is an open source CI & CD software, used to automate a variety of tasks, including build, test and deploy software. Jenkins support various operation modes, the Java program can be a separate package through the system, or by Docker.
Jenkins is a widely used continuous build Web visualization tool for building automation project is to compile, package, deploy. It is done through standardization, simple, tedious, time-wasting duplication of effort.

 Jenkins saying: build a great, all-powerful! 
Jenkins User Manual Portal: https://jenkins.io/zh/doc/

Second, the publishing process

 Detailed work process:

Third, prepare the environment

Server Role IP addresses CPU name Run Service system version
 Jenkins/Docker  192.168.115.21  jenkins Installation Docker, Tomcat running Jenkins, Git client, JDK, Maven CentOS 7.4 
 Git/Registry  192.168.115.22  git  Installation Docker, Git service, Registry of private container warehouse  CentOS 7.4
 Docker  192.168.115.23  docker  Installation Docker, pull mirroring run Tomcat Java project  CentOS 7.4

Fourth, the idea of ​​combing deployment

Fifth, the three machines operating

i. yum source configured to install the necessary number of system tools

[root@jenkins ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

 ii. Adding information source software

[root@jenkins ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

iii. Docker-CE update and install

[root@jenkins ~]# yum makecache fast
[root@jenkins ~]# yum -y install docker-ce

iv. Docker open service

[root@jenkins ~]# systemctl start docker
[root@jenkins ~]# systemctl enable docker

v. Check the docker version

Note: The above step three servers need the same operation.

Six, Git machine operation

6.1, deploying Git repository, Git operations on the machine

i. install Git

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

ii. Create a Git user and password

[root@git ~]# useradd git
[root@git ~]# passwd git
Changing password for user git.
New password: *******
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: *******
passwd: all authentication tokens updated successfully.

iii. Creating a warehouse

[root @ Go ~] # su - Go to 
[Go Go @ ~] $ mkdir test- jenkins.git 
[Go Go @ ~] $ cd test-jenkins.git / 
[@ Go Test Go -jenkins.git] $ Go - - bare init 
Initialized empty repository Jump in /home/git/test-jenkins.git/

6.2 Git repository access test, operate the machine on Jenkins

i. install the client Git

[root@jenkins ~]# yum install git -y

ii. 生成公钥,拷贝到Git服务器

[root@jenkins ~]# ssh-keygen -t rsa   // 三个回车
[root@jenkins ~]# ssh-copy-id git@192.168.115.22   // 注意是git用户

iii. 拉取Git项目测试

[root@jenkins ~]# git clone git@192.168.115.22:/home/git/test-jenkins.git
Cloning into 'test-jenkins'...
warning: You appear to have cloned an empty repository.

6.3、部署Docker私有仓库,Git机器上操作

i. 创建Registry容器

[root@git ~]# docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name rregistry registry

ii. 检查Registry容器状态,已经成功启动了

七、Docker机器上操作

i. 配置私有仓库

[root@docker ~]# cat  /etc/docker/daemon.json 
{"registry-mirrors": ["http://abcd1234.m.daocloud.io"],
 "insecure-registries": [ "192.168.115.22:5000"]}

注:第2行是192.168.115.22 Docker私有仓库地址。

ii. 重启Docker服务

[root@docker ~]# systemctl restart docker

iii. 配置JDK
JDK1.8下载:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

[root@docker ~]# tar zxvf jdk-8u221-linux-x64.tar.gz
[root@docker ~]# mv jdk1.8.0_221/ /usr/local/jdk

iv. 构建Tomcat基础镜像

[root@docker ~]# cat Dockerfile
# 基础镜像
FROM centos:7
# 维护者信息
MAINTAINER test-Jenkins        
# Tomcat版本
ENV VERSION=8.5.45    
# jdk目录    
ENV JAVA_HOME /usr/local/jdk    
# 安装wget命令
RUN yum install wget -y        

# 下载Tomcat
RUN wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz && \     
# 解压tomcat
tar zxf apache-tomcat-${VERSION}.tar.gz && \                    
# 移动解压出来的tomcat文件
mv apache-tomcat-${VERSION} /usr/local/tomcat-jenkins && \        
# 删除不需要的文件,减轻容器体积
rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat-jenkins/webapps/* && \        
# 创建ROOT,tomcat默认目录
mkdir /usr/local/tomcat-jenkins/webapps/ROOT            

# 映射端口
EXPOSE 8080        
# 启动tomcat    
CMD ["/usr/local/tomcat-jenkins/bin/catalina.sh", "run"]

v. 构建镜像上传Registry仓库

[root@docker ~]# docker build -t 192.168.115.22:5000/tomcat-jenkins -f Dockerfile .

vi. 推送到镜像仓库

[root@docker ~]# docker push 192.168.115.22:5000/tomcat-jenkins

vii. 验证是否上传Registry镜像仓库

[root@docker ~]# curl -XGET http://192.168.115.22:5000/v2/_catalog
{"repositories":["tomcat-jenkins"]}

八、Jenkins机器上操作

8.1、配置私有仓库、Git、JDK、Maven

i. 配置私有仓库

[root@docker ~]# cat  /etc/docker/daemon.json 
{"registry-mirrors": ["http://abcd1234.m.daocloud.io"],
 "insecure-registries": [ "192.168.115.22:5000"]}
 
重启Docker服务:
[root@docker ~]# systemctl restart docker

注:第2行是192.168.115.22 Docker私有仓库地址。

ii. 安装JDK 
JDK1.8下载:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

[root@jenkins ~]# tar zxvf jdk-8u221-linux-x64.tar.gz
[root@jenkins ~]# mv jdk1.8.0_221/ /usr/local/jdk

iii. 配置JDK

[root@jenkins ~]# 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

iv. 更新系统环境变量

[root@jenkins ~]# source /etc/profile

v. 验证

[root@jenkins ~]# java -version

vi. 安装Maven 
Maven下载连接:http://maven.apache.org/download.cgi

[root@jenkins ~]# tar zxvf apache-maven-3.6.1-bin.tar.gz
[root@jenkins ~]# mv apache-maven-3.6.1 /usr/local/maven

8.2、安装Jenkins和Tomcat

Jenkins下载链接:https://jenkins.io/zh/download/ 
Tomcat下载链接:http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.45/bin/apache-tomcat-8.5.45.tar.gz

i. 解压和拷贝Jenkins.war到Tomcat目录下

[root@jenkins ~]# tar zxvf apache-tomcat-8.5.45.tar.gz
[root@jenkins ~]# mv apache-tomcat-8.5.45  /usr/local/tomcat-jenkins
[root@jenkins ~]# rm -rf /usr/local/tomcat-jenkins/webapps/*
[root@jenkins ~]# unzip jenkins.war -d /usr/local/tomcat-jenkins/ROOT

ii. 启动Tomcat

[root@jenkins ~]# /usr/local/tomcat-jenkins/bin/startup.sh

8.3、配置Jenkins

i. 访问Jenkins(http://IP:8080

ii. 获取密码

[root@jenkins ~]# cat /var/jenkins_home/secrets/initialAdminPassword
e3218946860a4e62b5b4808db0a7f2b6

iii. 选择插件的安装(不熟悉的情况下推荐安装即可,省得麻烦)

iv. 创建用户

v. 配置Maven、jdk、git环境

注:系统管理 —>  全局工具配置,指定JDK、Maven路径,Git保持默认

vi. Jenkins安装必要插件

注:系统管理 —> 管理插件(安装SSH与Git Parameter插件)

插件说明:

SSH:用于SSH远程Docker主机执行Shell命令;
Git Parameter:动态获取Git仓库Branch、Tag;

vii. 配置SSH插件

viii. 输入Docker主机的用户名/密码

ix. 添加SSH远程主机

注:记得点击左下角“保存”按钮。

九、上传JAVA项目代码到Git仓库

先下载并安装git:https://git-scm.com/download/

9.1、使用IDEA创建测试项目

创建新项目:

选择Spring Initializr:本地提前配置好JDK环境和Maven环境

选择war:

选择Web —> Sping Web Starter:

随便起个项目名称:

来个简单的“Hello World”:

代码如下:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @RequestMapping
    public String hello() {
        return "Hello Jenkins v1.0";
    }
}

本地运行测试项目:

本地浏览器访问测试:

9.2、连接Git仓库上传代码

i. 在IDEA中设置Git,在File-->Setting->Version Control-->Git-->Path to Git executable选择你的git安装后的git.exe文件,然后点击Test,测试是否设置成功。

ii. 选择要上传的项目

iii. 将本项目添加到本地仓库

注:我们在项目文件名上右键选择 git,通过 add 添加到本地库的跟踪区,再comment 正式提交到本地仓库。

iv. 添加Git仓库地址

v. 上传代码

vi. 在Jenkins机器上下载项目验证是否上传成功

[root@jenkins ~]# git clone git@192.168.115.22:/home/git/test-jenkins.git
[root@jenkins ~]# cd test-jenkins/
[root@jenkins test-jenkins]# ls
mvnw  mvnw.cmd  pom.xml  src

9.3、给项目添加Tag

9.3.1、IDEA测试项目连接Git添加Tag

i. 进入Version Control-->log

ii. 在之前版本中,右键,新建标签

iii. Push标签

注:由于不是在当前最新版本打入的标签,push 时需要选择 push tags (all),不然不能push。

iv. 在Git仓库上查看Tag验证一下

[root@git ~]# su - git
[git@git ~]$ cd test-jenkins.git/
[git@git test-jenkins.git]$ git tag
v1.0

9.3.2、如果是直接上传项目到Git仓库,加Tag方法

[root@git ~]# su - git
[git@git ~]$ cd test-jenkins.git/
[git@git test-jenkins.git]$ git branch
[git@git test-jenkins.git]$ git tag v1.0
[git@git test-jenkins.git]$ git tag
v1.0

额外补充删除Tag:

[git@git test-jenkins.git]$ git tag -d v1.0

十、Jenkins发布测试

10.1、配置Jenkins项目

安装Maven插件,构建一个Maven项目:

配置项目(新建任务 -> 输入任务名称,构建一个Maven项目)

配置Git参数化构建:

动态获取Git仓库Tag,与用户交互选择Tag发布:

指定项目Git仓库地址:

注:修改*/master为$Tag,Tag是上面动态获取的变量名,表示根据用户选择打代码版本。

设置maven构建命令选项(clean package -Dmaven.test.skip=true):

Jenkins本机镜像构建和推送到私有镜像仓库:

代码如下:

# 定义变量
REPOSITORY=192.168.115.22:5000/tomcat-jenkins:${Tag}

# 开始构建镜像
cat > Dockerfile << EOF
# 拉取私有仓库的镜像
FROM 192.168.115.22:5000/tomcat-jenkins:latest
# 删除Tomcat下不需要的ROOT项目
RUN rm -rf /usr/local/tomcat-jenkins/webapps/ROOT
# 将jenkins编译好的war包拷贝到Tomcat目录下
COPY target/*.war /usr/local/tomcat-jenkins/webapps/ROOT.war
# 运行Tomcat
CMD ["/usr/local/tomcat-jenkins/bin/catalina.sh", "run"]
EOF

# 清除Jenkins主机旧的构建镜像
docker image rm 192.168.115.22:5000/tomcat-jenkins:${Tag}
# 构建自定义镜像
docker build -t $REPOSITORY .
# 开始上传镜像
docker push $REPOSITORY

SSH远程在Docker主机上执行创建容器命令:

代码如下:

# 定义变量
REPOSITORY=192.168.115.22:5000/tomcat-jenkins:${Tag}

# 删除之前容器和镜像
docker rm -f tomcat-jenkins |true
docker image rm $REPOSITORY |true

# 开始部署容器
docker container run -d --name tomcat-jenkins -v /usr/local/jdk:/usr/local/jdk -p 80:8080 $REPOSITORY

10.2、开始构建

点击项目名称 —> 选择版本 —> 开始构建

查看构建过程以及信息输出:

查看是否构建成功:

验证项目是否可以访问:

十一、后面再废话两句

到此就简单体验了Jenkins+Docker+Git+Registry实现一套CI自动化发布流程,本来打算Jenkins放在容器上运行的,发现Jenkins容器里面需要Docker命令,但是比较繁琐,也感觉没必要好像,就直接采用方法一了。
感兴趣的可以通过以下方式实现:
- 不使用任何Jenkins镜像,宿主机安装Jenkins [宿主机有Docker服务]
- 不使用官方Jenkins镜像,自己构造带有Docker服务的Jenkins镜像
- Docker-in-Docker [DinD]
- Docker-outside-of-Docker [DooD]
- 使用Jenkins的Docker插件

注:如有问题请指点出来,非常感谢。 
注:本文参考这篇博客,非常感谢博主:https://blog.51cto.com/lizhenliang/2159817

 

Guess you like

Origin www.cnblogs.com/l-hh/p/11455870.html