Jenkins 配置业务代码发布

2022-06-18  本文已影响0人  Rami

1. dev主机至gitlab-ce密钥配置

1.2 dev主机生成密钥对

# ssh-keygen
# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD4kVDnVDU/K3bidZJQy6EiGkGrwUI/m7X5vuhZBepW0KaYOouznICuXqKxjc9kiSVv+rAKtP8AkQFJcbZM0ige5rS1LrsIkKIxbGW8hUDyrRuz1iTMl96Upk+FkG907RDrxFUGGkly9Ykjkhh0RRLNdrlw95zXXr4o9I2fCBHMa5u2cZCSVMACgTLnnn8ntE37b3Zrh4fHzFPhFK8SF/eyso8EJXXXfSXLjqZimr7Fb0AMgvLWa0OTTYmBessvS4aU0Fq4P6Kfiua6fJSBa0nn67UqZumUoTC/cUfYJyYk/FYavfEW/2OULBRZ9LyXNSpz97JhEZDBeDj+DBv0WO1/ root@dev
image.png image.png

2.创建项目数据库

2.1 安装mysql (略)

2.2 创建业务库并授权

MySQL [(none)]> create database if not exists solo default charset utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.02 sec)

MySQL [(none)]> grant all on solo.* to 'root'@'%' identified by "123456";
Query OK, 0 rows affected, 1 warning (0.05 sec)

MySQL [(none)]> grant all on solo.* to 'root'@'localhost' identified by "123456";
Query OK, 0 rows affected, 2 warnings (0.01 sec)

3.获取项目代码并修改

# 1.代码获取
# git clone --recurse-submodules https://gitee.com/dl88250/solo.git

#2.代码修改
# ls
solo
# vim solo/src/main/resources/local.properties  修改数据库信息
#
# Solo - A small and beautiful blogging system written in Java.
# Copyright (c) 2010-present, b3log.org
#
# Solo is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
#

#
# Description: Solo local environment configurations.
# Version: 1.1.3.15, Mar 17, 2019
# Author: Liang Ding
#

#### MySQL runtime ####     修改这里的信息
runtimeDatabase=MYSQL
jdbc.username=root
jdbc.password=123456
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.URL=jdbc:mysql://10.0.0.31:3306/solo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true

#### H2 runtime ####
#runtimeDatabase=H2
#jdbc.username=root
#jdbc.password=
#jdbc.driver=org.h2.Driver
#jdbc.URL=jdbc:h2:~/solo_h2/db;MODE=MYSQL

# The minConnCnt MUST larger or equal to 3
jdbc.minConnCnt=5
jdbc.maxConnCnt=10

# The specific table name prefix
jdbc.tablePrefix=b3_solo

4.项目代码上传到gitlab

image.png
git config --global user.name "Administrator"
git config --global user.email "admin@example.com"

[root@dev solo]# git remote remove origin

[root@dev solo]# git remote add origin git@10.0.0.102:root/solo.git   这里ip是gitlab的

[root@dev solo]# git add -A .
[root@dev solo]# git commit -m "new"
[master 96d18ba] new
 1 file changed, 1 insertion(+), 1 deletion(-)

[root@dev solo]# git tag 1.0.0
[root@dev solo]# git push origin 1.0.0
[root@dev solo]# git push -u origin --all
image.png

5.构建项目运行基础应用容器镜像

在harbor主机上操作

5.1.创建项目目录

[root@harbor ~]# mkdir /tomcatdir
[root@harbor ~]# cd /tomcatdir/

5.2 生成Dockerfile文件

root@harbor tomcatdir]# echo "tomcat is running" >> index.html
[root@harbor ~]# tar xf jdk-8u231-linux-x64.tar.gz 
[root@harbor ~]# mv jdk1.8.0_231 /tomcatdir/jdk

[root@harbor tomcatdir]# vim  Dockerfile 
FROM centos:centos7

MAINTAINER "www.zege.com"

ENV VERSION=8.5.81
ENV JAVA_HOME=/usr/local/jdk
ENV TOMCAT_HOME=/usr/local/tomcat

RUN yum -y install wget

RUN  wget https://dlcdn.apache.org/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz --no-check-certificate

RUN tar xf apache-tomcat-${VERSION}.tar.gz

RUN mv apache-tomcat-${VERSION} /usr/local/tomcat

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

RUN mkdir /usr/local/tomcat/webapps/ROOT

ADD ./index.html /usr/local/tomcat/webapps/ROOT/

ADD ./jdk /usr/local/jdk


RUN echo "export TOMCAT_HOME=/usr/local/tomcat" >> /etc/profile

RUN echo "export JAVA_HOME=/usr/local/jdk" >> /etc/profile

RUN echo "export PATH=${TOMCAT_HOME}/bin:${JAVA_HOME}/bin:$PATH" >> /etc/profile

RUN echo "export CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar" >> /etc/profile


RUN source /etc/profile

EXPOSE 8080

CMD ["/usr/local/tomcat/bin/catalina.sh","run"]

[root@harbor tomcatdir]# ls
Dockerfile  index.html  jdk

5.3 使用docker build构建容器镜像

[root@harbor tomcatdir]# docker build -t 10.0.0.10/library/tomcat:8581 .

5.4 构建时如果拉取基础镜像失败解决方法

#创建或修改 /etc/docker/daemon.json 文件,修改为如下形式
{
    "registry-mirrors": [
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"
    ]
}

# systemctl  daemon-reload
# systemctl  restart docker

5.5 推送镜像至harbor仓库

# docker push 10.0.0.10/library/tomcat:8581

5.6 验证容器镜像可用性

[root@harbor tomcatdir]# docker run -d 10.0.0.10/library/tomcat:8581
2881b67f14f65b407b1cd867918e11bbeffbd2a73197987fce72477414fafe65

[root@harbor tomcatdir]# docker ps
CONTAINER ID   IMAGE                                                     COMMAND                  CREATED          STATUS                 PORTS                                   NAMES
2881b67f14f6   10.0.0.10/library/tomcat:8581                         "/usr/local/tomcat/b…"   46 seconds ago   Up 45 seconds          8080/tcp                                eloquent_gates

[root@harbor tomcatdir]# docker inspect 2881b67f14f6|grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

[root@harbor tomcatdir]# curl 172.17.0.2:8080
tomcat is running

6.jenkins项目构建及发布

6.1 项目构建及发布步骤

第一步:jenkins从gitlab上拉取项目代码
第二步:jenkins对项目代码编译,由maven完成
第三步:jenkins使用docker对编译完成的项目代码进行打包,打包成容器应用镜像
第四步:jenkins把打包的容器应用镜像上传到harbor
第五步:jenkins通过ssh插件完成对web-server进行运行容器应用镜像的操作

6.2 创建项目任务

image.png image.png image.png image.png image.png image.png
Dockerfile:
REPOSITORY=10.0.0.10/library/solo:${Tag}
# 构建镜像
cat > Dockerfile << EOF
FROM 10.0.0.10/library/tomcat:8575
RUN rm -rf /usr/local/tomcat/webapps/ROOT
COPY target/*.war /usr/local/tomcat/webapps/ROOT.war
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
EOF
docker build -t $REPOSITORY .

# 上传镜像
docker login 10.0.0.10 -u admin -p Harbor12345
docker push $REPOSITORY
docker logout 10.0.0.10
shell script:
REPOSITORY=10.0.0.10/library/solo:${Tag}
# 部署
docker rm -f blog-solo |true
docker image rm $REPOSITORY |true
docker container run -d --name blog-solo -p 85:8080 $REPOSITORY
image.png image.png image.png
上一篇 下一篇

猜你喜欢

热点阅读