一步步入门Jenkins+Net Core3.1+Gitlab,实现 CICD

架构说明:

  由浅入深,我们暂时不考虑分布式,安装Jenkins到用户服务器进行CICD

  需要两台服务器

  Gitlab:192.168.232.128:12080

    源代码仓库,可以参考《安装gitlab

  用户服务器:192.168.232.130,

    通过Jenkins自动构建服务:1、git clone源码

                  2、Dockerfile编译发布生成镜像

                  3、shell控制更新服务

1、前期准备

  1.1用户服务器:CentOS系统

    安装Docker、Git环境

yum install -y docker-ce-18.06.0.ce-3.el7 #docker安装高版本,不然AS base这些命令无法使用,详细安装步骤参考《安装docker & 配置dotnet core 项目》
yum install -y git

  1.2创建测试项目,长传给gitlab

    我的项目如下:

    

    需要做几步操作

    1)定义站点端口,比如2202

      Program

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("http://*:2202");
                    webBuilder.UseStartup<Startup>();
                });

    2)添加Dockerfile

      

     记住一点,用VS生成的Dockerfile支持文件,一定要和.sln同目录

    (我就是VS生成在了MyApi/MyApi/Dockerfile,导致Docker build的时候找不到目录,然后进入无止境的修改Dockerfile内部命令的目录,最后发现只要保持和.sln同一级,就不会出错)

 

2、使用Jenkins之前,先进行一个简单的尝试

  将源文件打包成images,并生成容器服务,只有手动执行能成功,才能为后面Jenkins执行提供保障

cd /mysoft  #自己建个目录存一下
git clone http://192.168.232.128:12080/gitlab-instance-9d489b45/myapi.git
cd myapi
docker build -t myapi:V1.0 .
docker run --name myapi-server -p 2202:2202 -d --restart=always myapi:V1.0

  build的时候可能会发生不成功的情况,自己根据情况进行修正

  (其实无非就是docker版本和目录的问题,严格按照上面说的做,基本没问题)

[root@localhost mysoft]# docker ps
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                                     NAMES
a443b859a696        myapi:V1.0          "dotnet MyApi.dll"   18 minutes ago      Up 18 minutes       80/tcp, 443/tcp, 0.0.0.0:2202->2202/tcp   myapi-server

  正常运行,尝试访问一下:

  

  成功!!!

 

 3、安装Jenkins

  yum安装,官网步骤

  sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
  sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
  yum install fontconfig java-11-openjdk
  yum install jenkins

  默认的是8080端口,打开页面会进行初始化,

  初始化完成后,先不着急安装插件,做一些配置修改吧

  1)修改网络协议

    Jenkins的默认插件下载源地址为:https://updates.jenkins.io/update-center.json,有可能因为https的限制,导致一些插件下载失败,

    将https改成http即可

vim /var/lib/jenkins/hudson.model.UpdateCenter.xml

    或者替换为国内的插件下载源地址,如

https://mirror.xmission.com/jenkins/updates/update-center.json

  2)修改网络判定地址

  Jenkins在下载插件之前会先检查网络,通过访问https://www.google.com来判断,防止外网访问延时,可用将其改为http://www.baidu.com

vim /var/lib/jenkins/updates/default.json

  好了,回到界面,使用默认密码:登录->安装默认插件->创建用户...(最后截个图,前面的步骤忘记截图了)

  

  

4、构建我们的第一个任务吧

  选择【新建Item】->

  

  【描述】随便写

  【编码管理】选择git,输入地址,添加登录账号,指定分支要保持和gitlab上的名称一致

  

   【构建触发器】,先不填,手动触发就行

   【构建环境】,勾选build之前删除工作目录

  

  【Build Steps】

  选择Execute shell

  shell如下,这个很简单,主要是先简单跑起来,后面再改

echo "====开始构建===="
cd /var/lib/jenkins/workspace/myapi #clone会存在workspace下面
docker build -t myapi:V1.0 .
docker run --name myapi-server -p 2202:2202 -d --restart=always myapi:V1.0
echo "====构建结束===="

  (先把2创建容器和镜像删除掉,不然构建会冲突)

  构建产生的问题:

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post

  原因:jenkins调用docker需要授权

[root@localhost myapi]# sudo usermod -a -G docker jenkins
[root@localhost myapi]# systemctl restart jenkins

  成功!!!

5、完善shell

#!/bin/bash
echo "====开始构建===="
cd /var/lib/jenkins/workspace/myapi #clone会存在workspace下面
#删除容器
docker ps -a | grep "myapi-server"
if [ $? -eq 0 ];then
    docker container stop myapi-server
    docker container rm myapi-server
fi
docker build -t myapi:V1.0 .
docker run --name myapi-server -p 2202:2202 -d --restart=always myapi:V1.0
echo "====构建结束===="
echo "====删除虚悬镜像===="
docker rmi $(docker images -q -f dangling=true)

  指令说明

  1、#!/bin/bash:指定shell命令,需要以这个开头

  2、$? :指上一条指令的执行结果,成功为0,失败为1,可用来判断是否有返回结果

  3、虚悬镜像,镜像名为<none>,build的时候旧版镜像会被新版镜像替代,变成虚悬镜像

    -q:精确匹配ID或者Name

    -f:强制删除

    dangling:虚悬的意思

 

6、使用jenkins进行远程服务器服务构建

  6.1步骤(需要4台服务器)

    使用Jenkins(232.130)从gitlab(232.128)clone源码

    生成镜像并pull到Harbor(232.129),

    控制目标服务器(这里懒得新建一个,就用232.128代替,记得安装好docker)pull镜像,并运行

  6.2环境搭建:

    1)新增Harbor服务器:192.168.232.129,参考连接

    2)Jenkins服务器安装Maven:

wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
sudo yum insatll -y apache-maven

    3)Jenkins安装插件:Maven Integration、SSH

      Maven:构建maven任务

      SSH:访问远程主机

    4)配置Maven环境

      Global Tool Configuration最底部,输入MAVEN_HOME,切记取消自动安装

      mvn -v:可用查看MAVEN_HOME

            

    5)SSH连接服务器配置

      Configure System,中间位置,找到SSH remote hosts,新增SSH到目标服务器的链接

      

 

 

   6.3 开始构建Maven工程

    新增一个Item

     

    前面步骤同上单机部署,直到Build这一步

    需要给源码新增文件pom.xml,并push到gitlab仓库

    

    身为一名neter,真的不会Maven写pom.xml,主要是Jenkins执行的时候找不到这个文件就不会继续下去

    pom.xml,留个壳就行,会报解析错误,但是不影响程序跑下去,后面再研究

<?xml version="1.0" encoding="utf-8"?>
<project></project>

    好,,我们继续

    Post Steps 选择 Run only if build succeeds

    Add post-build - Execute shell

    这一步主要是构建镜像pull到Harbor

#!/bin/bash
#环境变量
harbor='192.168.232.129:80'
myapi_path='/var/lib/jenkins/workspace/maven-myapi' #clone源码存储地址
images=${harbor}'/v5/myapi'
tag='V1.0'

echo "====开始构建===="
cd ${myapi_path} 
docker build -t ${images}:${tag} .
docker rmi $(docker images -q -f dangling=true)
echo "====上传镜像===="
docker login -u kxy -p Aa123456 ${harbor}
docker push ${images}:${tag}

    Add post-build - Execute shell script on remote host using ssh

    SSH site 选择 6.2-5),新建的128目标服务器链接

    Command(粗糙写一下,能跑起来先)

#环境变量
harbor='192.168.232.129:80'
images=${harbor}'/v5/myapi'
tag='V1.0'

docker login -u kxy -p Aa123456 ${harbor}

docker ps -a | grep "myapi-server"
if [ $? -eq 0 ];then
    docker container stop myapi-server
    docker container rm myapi-server
fi

docker images | grep ${images}
if [ $? -eq 0 ];then
    docker rmi ${images}:${tag}
fi

docker pull ${images}:${tag}

docker run --name myapi-server -p 2202:2202 -d --restart=always ${images}:${tag}

    完成

    执行结果是成功的,但是pom.xml会返回解析错误,这个需要学习完maven再来补充

[ERROR] 'modelVersion' is missing. @ line 2, column 16
[FATAL] 'groupId' is missing. @ line 2, column 16
[FATAL] 'artifactId' is missing. @ line 2, column 16
[FATAL] 'version' is missing. @ line 2, column 16

 

 

 

 

end、碰到的一些错误记录

  1)启动jenkins不成功,提示:

[root@localhost ~]# systemctl start jenkins
Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.
[root@localhost ~]# systemctl status jenkins.service
● jenkins.service - Jenkins Continuous Integration Server
   Loaded: loaded (/usr/lib/systemd/system/jenkins.service; disabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since 六 2023-02-18 20:46:51 CST; 15s ago
  Process: 3566 ExecStart=/usr/bin/jenkins (code=exited, status=1/FAILURE)
 Main PID: 3566 (code=exited, status=1/FAILURE)

2月 18 20:46:51 localhost.localdomain systemd[1]: jenkins.service: main process exited, code=exited, status=1/FAILURE
2月 18 20:46:51 localhost.localdomain systemd[1]: Failed to start Jenkins Continuous Integration Server.
2月 18 20:46:51 localhost.localdomain systemd[1]: Unit jenkins.service entered failed state.
2月 18 20:46:51 localhost.localdomain systemd[1]: jenkins.service failed.
2月 18 20:46:51 localhost.localdomain systemd[1]: jenkins.service holdoff time over, scheduling restart.
2月 18 20:46:51 localhost.localdomain systemd[1]: Stopped Jenkins Continuous Integration Server.
2月 18 20:46:51 localhost.localdomain systemd[1]: start request repeated too quickly for jenkins.service
2月 18 20:46:51 localhost.localdomain systemd[1]: Failed to start Jenkins Continuous Integration Server.
2月 18 20:46:51 localhost.localdomain systemd[1]: Unit jenkins.service entered failed state.
2月 18 20:46:51 localhost.localdomain systemd[1]: jenkins.service failed.

    通过一下命令来查看详细错误

[root@localhost init.d]# cd /etc/init.d
[root@localhost init.d]# ./jenkins start
Starting Jenkins Running with Java 8 from /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64/jre, which is older than the minimum required version (Java 11).
Supported Java versions are: [11, 17]
See https://jenkins.io/redirect/java-support/ for more information.
                                                           [失败]

    我们明明安装的java11,这地方怎么是java8?

vim /etc/init.d/jenkins  #查看一下jenkins环境文件
#存在以下jdk路径配置 candidates
=" /etc/alternatives/java /usr/lib/jvm/java-1.8.0/bin/java /usr/lib/jvm/jre-1.8.0/bin/java /usr/lib/jvm/java-11.0/bin/java /usr/lib/jvm/jre-11.0/bin/java /usr/lib/jvm/java-11-openjdk-amd64 /usr/bin/java "

    jdk安装了两个版本,我们删掉一个

[root@localhost ~]# rpm -qa | grep java
java-11-openjdk-headless-11.0.18.0.10-1.el7_9.x86_64
java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64
java-1.8.0-openjdk-devel-1.8.0.362.b08-1.el7_9.x86_64
javassist-3.16.1-10.el7.noarch
tzdata-java-2022g-1.el7.noarch
java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64
javamail-1.4.6-8.el7.noarch
javapackages-tools-3.4.1-11.el7.noarch
java-1.8.0-openjdk-headless-1.8.0.362.b08-1.el7_9.x86_64
python-javapackages-3.4.1-11.el7.noarch
[root@localhost ~]# yum remove java-1.8.0-openjdk-devel-1.8.0.362.b08-1.el7_9.x86_64 java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64 java-1.8.0-openjdk-headless-1.8.0.362.b08-1.el7_9.x86_64

  重启成功,可用把java8的环境配置删掉了(不成功的留言沟通)

 

posted @ 2023-02-16 23:12  wskxy  阅读(647)  评论(0编辑  收藏  举报