CI/CD:
    持续集成/持续发布
    实验环境:
        centos7.7   192.168.1.103   4G内存    jenkins
        centos7.7   192.168.1.102   2G内存    tomcat
        centos7.7   192.168.1.101   2G内存    gitlab
        1.部署git
            安装依赖包:
                [root@jenkins ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker -y
            git官网下载最新版本git:
                [root@jenkins ~]# wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
            安装git:
                [root@jenkins ~]# tar fx git-2.9.5.tar.gz       # 解压
                [root@jenkins ~]# cd git-2.9.5/         # 切换目录
                [root@jenkins git-2.9.5]# make prefix=/usr/local/git all
                [root@jenkins git-2.9.5]# make prefix=/usr/local/git install
                [root@jenkins ~]# vim /etc/profile      #文件末尾添加
                    PATH=$PATH:$HOME/bin:/usr/local/git/bin
                [root@jenkins ~]# source /etc/profile
            验证:
                [root@jenkins ~]# git --version
                git version 1.8.3.1
        2.jdk环境部署
            下载jdk-8u171-linux-x64.tar.gz
            卸载本机的openjdk
                [root@jenkins ~]# yum remove java-1.* -y
            解压安装:
                [root@jenkins ~]# tar xf jdk-8u171-linux-x64.tar.gz
                [root@jenkins ~]# mv jdk1.8.0_171/ /usr/local/jdk
            设置环境变量:
                [root@jenkins ~]# vim /etc/profile      # 在末尾添加
                    JAVA_HOME=/usr/local/jdk
                    export PATH=$PATH:$JAVA_HOME/bin
                [root@jenkins ~]# source /etc/profile       # 重载
            验证:
                [root@jenkins ~]# java -version
                java version "1.8.0_171"
                Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
                Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
        3.部署maven
            下载maven:
                [root@jenkins ~]# wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
            解压安装:
                [root@jenkins ~]# tar xf apache-maven-3.5.4-bin.tar.gz
                [root@jenkins ~]# mv apache-maven-3.5.4 /usr/local/maven
            添加环境变量:
                [root@jenkins ~]# vim /etc/profile      # 添加以下内容
                    export M2_HOME=/usr/local/maven
                    export M2=$M2_HOME/bin
                    PATH=$M2:$PATH:$HOME/bin:/usr/local/git/bin
                    export JAVA_HOME=/usr/local/jdk
                    export PATH
            验证:
                [root@jenkins ~]# mvn -version
                Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
                Maven home: /usr/local/maven
                Java version: 1.8.0_171, vendor: Oracle Corporation, runtime: /usr/local/jdk/jre
                Default locale: en_US, platform encoding: UTF-8
                OS name: "linux", version: "3.10.0-1062.el7.x86_64", arch: "amd64", family: "unix"
        4.部署tomcat
            下载tomcat-9.0.1:
                [root@jenkins ~]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.1/bin/apache-tomcat-9.0.1.tar.gz
            解压并移动到指定目录:
                [root@jenkins ~]# tar xf apache-tomcat-9.0.1.tar.gz
                [root@jenkins ~]# mv apache-tomcat-9.0.1 /usr/local/tomcat
            定义tomcat所需的环境变量:
                [root@jenkins ~]# vim /etc/profile      # 末尾添加
                    CATALINA_HOME=/usr/local/tomcat
                    export CATALINA
                [root@jenkins ~]# source /etc/profile
            启动tomcat:
                [root@jenkins ~]# /usr/local/tomcat/bin/startup.sh
            测试:
                http://192.168.1.103:8080        5.部署jenkins-2.190
            下载路径:
                http://updates.jenkins-ci.org/download/war/
            上传jenkins.war到服务器上:
                [root@jenkins ~]# cp jenkins.war /usr/local/tomcat/webapps/
                [root@jenkins ~]# /usr/local/tomcat/bin/shutdown.sh
                [root@jenkins ~]# /usr/local/tomcat/bin/startup.sh
            tomcat会自动解压/usr/local/tomcat/webapps/目录下的war包
            登录jenkins:
                初次登录需要初始化密码,密码文件位置:
                    [root@jenkins ~]# cat /root/.jenkins/secrets/initialAdminPassword
                    42e1066fee8a47e78b4c332ba754d1e5
        6.管理插件
            简介:
                此处配置的事git+maven方式的自动化部署,所以git和maven的相关插件是必须的,还有一个ssh用于机器间的文件传送
            关于插件:
                GIT plugin:可能已经默认安装了
                Publish Over SSH:远程Shell
                Maven Intergration plugin
            安装插件Deploy to container:
                作用:
                    支持自动化将代码部署到tomcat容器
                安装过程:
                    系统管理——>插件管理——>可选插件——>Deploy container——>勾选,点击下边的按钮,直接安装,这个可能时间较久,等待即可
            安装插件Maven intergration:
                Maven Intergration
            安装git相关插件:
                GitHub Authentication plugin
                GitHub Branch Source Plugin
                GitHub Organization Folder Plugin
            安装插件:Publish Over SSH:
        7.检查错误配置
            点击系统管理:
                1.如果有“编码问题”错误,在Tomcat->conf->server.xml文件中修改即可
                    [root@jenkins ~]# head -1 /usr/local/tomcat/conf/server.xml
                        <?xml version="1.0" encoding="UTF-8"?>
                2.如果有“反向代理设置”错误,请忽略
        8.Jenkins系统设置
            这里没有强调的都设置为默认即可
            路径:
                系统管理->(全局工具配置)Global Tool Configuration,配置jdk,git,maven的根目录
            步骤:
                1.找到全局配置工具
                2.配置jdk目录
                    点击新增jdk——>取消自动安装——>填写Name(别名)为jdk,Path(路径)为/usr/local/jdk
                3.配置git目录
                    修改Path为/usr/local/git/bin/git
                4.配置maven目录
                    填写Name为maven,Path为/usr/local/maven
                5.保存退出
        9.SSH设置
            目的:
                简介:
                    (192.168.1.103)jenkins服务器上的maven将开发产生的*.war包,通过SSH自动推送到远程tomcat服务器上(192.168.1.102)。需要手动配置ssh key。配合自动化推送
                    192.168.1.103是jenkins服务器
                    192.168.1.102是tomcat网站服务器,代表业务服务器
                步骤:
                    1.jenkins服务器准备秘钥认证:
                        [root@jenkins ~]# ssh-keygen -t rsa         # 生成秘钥
                        [root@jenkins ~]# ssh-copy-id -i 192.168.1.102          # 将秘钥拷贝到tomcat服务器上
                        [root@jenkins ~]# ssh 192.168.1.102             # 验证
                        Last login: Mon Jul 27 06:10:45 2020 from 192.168.1.103
                        [root@tomcat ~]#
                    2.在jenkins上配置ssh信息
                        配置jenkins使其可以免密登录tomcat服务器
                        系统管理——系统配置——下拉至最后,配置Publish over SSH第三项,填写jenkins服务器的私钥——取消勾选Disable exec——勾选SSH Servers——一次填写Name,Hostname,Username,Remote Directory(/)——Test Configuration——保存
        10.新建任务
            创建任务并且定义名称和类型:
                新建任务——输入任务名称——构建一个maven项目——确定
            设置maven:
                1.公有git仓库(例如github):
                    设置描述:

持续集成 jenkins 持续集成jenkinsCI,CD_tomcat


                   

  选择版本控制器和仓库地址:

持续集成 jenkins 持续集成jenkinsCI,CD_持续集成 jenkins_02


                    设置触发器(保持默认):

                    设置构建(编译打包):

持续集成 jenkins 持续集成jenkinsCI,CD_tomcat_03


 

            

    设置maven的关键字:
                            clean package -Dmaven.test.skip=true
                    构建后操作:

持续集成 jenkins 持续集成jenkinsCI,CD_git_04


                        Name:ssh server因为之前的配置会默认出现tomcat业务服务器的名字

                        Source files:构建之后,在jenkins服务器上是可以自动看到war包的。(该路径不需要创建)

                            示例:ls /root/.jenkins/jobs/testjob1/builds/target/*.war

                        Remove prefix:自动删除路径前缀(不需要创建路径前缀)

                        Remote directory:tomcat业务服务器上的路径,需要提前在192.168.1.102上面创建该目录,用来存放网站源代码。(需要后台创建)

                            示例:mkdir -p /jenkins/war

                        Exec command:tomcat业务服务器(192.168.1.102)在接收到源码之后的自定义动作,比如:将源码拷贝到网站的主目录(/jenkins),并执行一些其他操作如重启服务等(或创建文件touch)(需要后台创建)

                            示例:

                                mkdir /jenkins/sh/

                                cat /jenkins/sh/deploy.sh

                                cp -r /jenkins/war/*.war /jenkins

                                touch /tmp/aaaaa.txt

                                chmod +x /jenkins/sh/deploy.sh

持续集成 jenkins 持续集成jenkinsCI,CD_maven_05


                2.私有git仓库(例如git):

        11.构建任务

            保存完成之后——立即构建——等待,很大几率会失败,如果失败需要查看日志分析原因