jenkins+docker+git+harbor构建及代码回滚(未完)

目录

  • 一.部署
    • 环境工作流程介绍
    • 部署harbor

一.部署

前提环境说明

192.168.111.3 该机器为git本地仓库,及git远程仓库(git用户创建),及Harbor镜像仓库
192.168.111.4 该机器为Jenkins
192.168.111.5 该机器发布docker

Jenkins环境以及git仓库,以及docker已经准备好,三台机器都需要docker环境。第一台需要docker环境来运行Harbor,第二台Jenkins需要docker环境来制作镜像并上传到Harbor,第三台是运行docker容器。

仓库里测试页为自定义的测试页

git上传代码到远程仓库时是基于标签上传的,我们可以基于该标签联想到版本号,那么所谓的代码回退,在本案例中,无非就是将现有的容器干掉,从而将我们需要回退到的版本对应的镜像拉取并且运行。

环境工作流程介绍

开发根据git标签提交代码,Jenkins拉取代码构建镜像上传到Harbor中,然后docker主机从Harbor中拉取镜像,并根据指令运行容器。

部署harbor

1、在线安装包
    $ wget https://github.com/vmware/harbor/releases/download/v1.1.2/harbor-online-installer-v1.1.2.tgz
    $ tar xvf harbor-online-installer-v1.1.2.tgz
2、离线安装包
    $ wget https://github.com/vmware/harbor/releases/download/v1.1.2/harbor-offline-installer-v1.1.2.tgz
    $ tar xvf harbor-offline-installer-v1.1.2.tgz
[root@localhost ~]# tar xf harbor-offline-installer-v1.5.0.tgz 
[root@localhost ~]# mv harbor /usr/local/
[root@localhost ~]# vim /usr/local/harbor/harbor.cfg
    hostname = 192.168.111.3
    ui_url_protocol = http
  • 两个harbor部署docker-compose
curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m`
# mv docker-compose /usr/local/bin/docker-compose
# chmod +x !$
chmod +x /usr/local/bin/docker-compose
  • 启动配置
# sh /usr/local/harbor/install.sh 
#此过程比较慢

jenkins+docker+git+harbor构建及代码回滚(未完)_第1张图片

默认密码:admin:Harbor12345

在docker主机上指定镜像仓库为Harbor所在地址

[root@localhost ~]# vim /etc/docker/daemon.json 
{"insecure-registries":["192.168.111.3:80"]}
[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl restart docker

将我准备的centos镜像上传到Harbor,后面构建一个包含代码的镜像,需要基于该镜像进行构建

[root@localhost ~]# cat centos-7-x86_64.tar.gz | docker import - centos:7
sha256:612802f256a6d854b22faa406bda99a49ff1a5ddad2f0b29faf75f7e6d1f2ebd

[root@localhost ~]# docker tag centos:7 192.168.111.3:80/library/centos:7 

[root@localhost ~]# docker login 192.168.111.3:80
Username: admin
Password: 
Login Succeeded
#需要先登录上,在上传镜像

[root@localhost ~]# docker push 192.168.111.3:80/library/centos:7

jenkins+docker+git+harbor构建及代码回滚(未完)_第2张图片

接下来在Jenkins机器和docker主机上,部署jdk,Jenkins部署jdk是maven构建时要用到

[root@localhost ~]# tar xf jdk-8u191-linux-x64.tar.gz 

[root@localhost ~]# mv jdk1.8.0_191/ /usr/local/java

[root@localhost ~]# tar xf apache-tomcat-8.5.16.tar.gz 
[root@localhost ~]# mv apache-tomcat-8.5.16 /usr/local/tomcat
[root@localhost ~]# vim Dockerfile
    FROM centos:7
    MAINTAINER joinbest
    ENV TVER=8.5.16
    ENV JAVA_HOME=/usr/local/java
    ADD ./apache-tomcat-${TVER}.tar.gz /tmp
    RUN cd /tmp && \
     mv apache-tomcat-${TVER} /usr/local/tomcat && \
     rm -rf /usr/local/tomcat/webapps/* && \
     mkdir /usr/local/tomcat/webapps/ROOT
    EXPOSE 8080
    CMD ["/usr/local/tomcat/bin/catalina.sh","run"

[root@localhost ~]# docker build -t 192.168.111.3:80/library/tomcat-85 -f Dockerfile ./
[root@localhost ~]# docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
192.168.111.3:80/library/tomcat-85   latest              a9090e8402d9        6 seconds ago       611MB
centos                               7                   612802f256a6        2 hours ago         589MB
192.168.111.3:80/library/centos      7                   612802f256a6        2 hours ago         589MB
[root@localhost ~]# docker push 192.168.111.3:80/library/tomcat-85

需要修改一下Jenkins的运行用户为root,因为有些指令其他用户没有权限,并且在本环境中maven以及JDK也已经部署好,以及在全局工具配置里面添加Git

需要在Jenkins安装ssh插件以及git parameter插件

添加一个全局凭据用于连接docker主机使用的

添加系统配置,添加一个远程主机,将docker主机的ip,以及应用刚才的全局凭据。

  • 添加maven构建任务

execute shell 输入框填写,这个内容是在Jenkins主机上执行的

REPOSITORY=192.168.111.3:80/library/probe:${Tag}
cat << EOF > Dockerfile
FROM 192.168.111.3:80/library/tomcat-85:latest
RUN rm -rf /usr/local/tomcat/webapps/ROOT
COPY web/target/*.war /usr/local/tomcat/webapps/ROOT.war
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
EOF
docker build -t $REPOSITORY ./
docker login 192.168.111.3:80 -u admin -p Harbor12345 #这个用于登录harbor,只有登陆上才能上传镜像
docker push $REPOSITORY

post steps添加一个execute shell script on remote host using ssh,这是在docker主机上执行的

REPOSITORY=192.168.111.3:80/library/probe:${Tag}
docker rm -f probe | true
docker images rm $REPOSITORY | true 
docker container run -d --name probe -v /usr/local/java:/usr/local/java -p 8090:8080 $REPOSITORY

转载于:https://www.cnblogs.com/joinbestgo/p/11192094.html

你可能感兴趣的:(jenkins+docker+git+harbor构建及代码回滚(未完))