基于Jenkins实现可腹部回滚的cicd平台
- Jenkins :是一个开源的实现持续集成的工具,可以实施监控持续集成过程中所存在的问题,提供详细的日志文件和提醒功能,还能用图表的形式直观的展示出项目构建的趋势和稳定性
- maven:只有在Java项目时才会使用,帮助实现Java项目的打包
- ansible:自动化运维工具,实现批量系统配置,批量程序部署,批量运行命令等
基于Jenkins+git+docker 发布Java项目
192.168.200.10 git Git/Docker registry
192.168.200.20 jenkins jenkins
192.168.200.14 docker docker
[root@docker ~]# setenforce 0
setenforce: SELinux is disabled
[root@docker ~]# iptables -F
[root@docker ~]# systemctl stop firewalld
[root@docker ~]# systemctl stop NetworkManager
配置Git模拟开发提交
[root@Git ~]# rpm -q git
git-1.8.3.1-19.el7.x86_64
[root@Git ~]# useradd git
[root@Git ~]# passwd git #设置密码123456
#配置jenkins主机上免交互拉取Git仓库
[root@jenkins ~]# ssh-keygen
[root@jenkins ~]# ssh-copy-id git@192.168.200.14
[root@jenkins ~]# ssh git@192.168.200.20 #测试git
[git@Git ~]$ exit
#在git服务器上创建probe仓库
[root@Git ~]# su - git
Last login: Thu Nov 4 00:13:20 PDT 2021 from server01 on pts/1
[git@Git ~]$ mkdir probe.git
[git@Git ~]$ cd probe.git/
[git@Git probe.git]$ git --bare init
Initialized empty Git repository in /home/git/probe.git/
#从GitHub拉取开源Java博客系统psi-probe代码
[root@jenkins ~]# git clone https://github.com/psi-probe/psi-probe.git #
psi-probe
[root@jenkins psi-probe]# ls
CODE_OF_CONDUCT.md LICENSE psi-probe-core psi-probe-tomcat9
CONTRIBUTING.md LICENSE_HEADER psi-probe-rest psi-probe-web
docs mvnw psi-probe-tomcat10 README.md
eclipse-java-google-style.xml mvnw.cmd psi-probe-tomcat7 src
intellij-java-google-style.xml pom.xml psi-probe-tomcat85
#移除旧地址,添加新的地址
[root@jenkins psi-probe]# git remote remove origin
[root@jenkins psi-probe]# git remote add origin git@192.168.200.20:/home/git/probe.git
#提交git仓库并创建tag
[root@jenkins psi-probe]# git add .
[root@jenkins psi-probe]# git config --global user.email "crushlinux@163.com"
[root@jenkins psi-probe]# git config --global user.name "crushlinux"
#创建标签
[root@jenkins psi-probe]# git tag 1.0.0
#推送git服务器
[root@jenkins psi-probe]# git push origin 1.0.0
Counting objects: 33856, done.
Compressing objects: 100% (9923/9923), done.
Writing objects: 100% (33856/33856), 22.14 MiB | 30.88 MiB/s, done.
Total 33856 (delta 20073), reused 33849 (delta 20067)
To git@192.168.200.20:/home/git/probe.git
* [new tag] 1.0.0 -> 1.0.0
配置docker仓库
#在git上部署docker私有仓库
[root@Git ~]# docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry
[root@Git ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ed58b50e06bf registry "/entrypoint.sh /etc…" 5 minutes ago Up 7 seconds 0.0.0.0:5000->5000/tcp registry
#在所有主机上配置连接git
[root@jenkins ~]# vim /etc/docker/daemon.json
{
"insecure-registries":["192.168.200.20:5000"]
}
[root@jenkins ~]# systemctl daemon-reload
[root@jenkins ~]# systemctl restart docker
#测试能否上传镜像到私有仓库git上
[root@docker ~]# cat centos-7-x86_64.tar.gz | docker import - centos:7
sha256:4240ca40f3e88720b2b64bbff21b2f5ec24a9d7dace62f17ea8bb9dd468709ec
[root@docker ~]# docker tag centos:7 192.168.200.20:5000/centos:7
[root@docker ~]# docker push 192.168.200.20:5000/centos:7
The push refers to repository [192.168.200.20:5000/centos]
c664eb1dcbf0: Pushed
7: digest: sha256:7a34258489e0bc4049641cd821aa18f2740a8d62c5a72b70ac7d6e645f5c195a size: 529
#查看registry上传的镜像
[root@docker ~]# curl http://192.168.200.20:5000/v2/_catalog
{"repositories":["centos"]}
[root@docker ~]# curl http://192.168.200.20:5000/v2/centos/tags/list
{"name":"centos","tags":["7"]}
#测试能否下载,因为有缓存所有速度特别快
[root@docker ~]# docker rmi 192.168.200.20:5000/centos:7
Untagged: 192.168.200.20:5000/centos:7
Untagged: 192.168.200.20:5000/centos@sha256:7a34258489e0bc4049641cd821aa18f2740a8d62c5a72b70ac7d6e645f5c195a
[root@docker ~]# docker pull 192.168.200.20:5000/centos:7
7: Pulling from centos
Digest: sha256:7a34258489e0bc4049641cd821aa18f2740a8d62c5a72b70ac7d6e645f5c195a
Status: Downloaded newer image for 192.168.200.20:5000/centos:7
192.168.200.20:5000/centos:7
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.200.20:5000/centos 7 4240ca40f3e8 5 minutes ago 589MB
centos 7 4240ca40f3e8 5 minutes ago 589MB
#构建Tomcat镜像
在Jenkins和docker上安装jdk;
[root@docker ~]# tar xf jdk-8u191-linux-x64.tar.gz
[root@docker ~]# mv jdk1.8.0_191 /usr/local/java
[root@docker ~]# vim Dockerfile
FROM centos:7
MAINTAINER crushlinux
ENV VERSIOL=8.5.16
ENV JAVA_HOME /usr/local/java
ADD ./apache-tomcat-${VERSION}.tar.gz /tmp
RUN cd /tmp && \
mv apache-tomcat-${VERSION} /usr/local/tomcat && \
rm -rf apache-tomcat-$(VERSION).tar.gz /usr/local/tomcat/webapps/* && \
mkdir /usr/local/tomcat/webapp/ROOT
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
[root@docker ~]# docker build -t 192.168.200.20:5000/tomcat-85 . //构建镜像