wordpress 分布式服务 mysqli woocommerce scrapy react router vue绑定事件 jquery遍历对象 两个正态分布相乘 svn默认安装路径 dplayer不能全屏 yml文件注释 range函数python 河南普通话报名入口 python的random函数 java的数据结构 java抽象方法 如何安装java环境 java字符串替换 java创建集合 java实例方法 java字符串相等 linuxls命令 嵌入式开发教程 js删除数组指定元素 win10计算器下载 id解锁大师 笔记本外接显示器好吗 三维看图软件 网络克隆 电子书制作软件 fireworks8序列号 画图怎么添加文字 视频md5修改器 脚本编程 神牧属性 卧龙推广 内存条是什么 adb安装 远程桌面管理工具
当前位置: 首页 > 学习教程  > 编程语言

jenkins/git/maven/nexus实现自动化部署

2021/1/13 19:25:36 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

环境准备: 1、 jenkins机(外网环境) 10.69.92.89:用于搭建Jenkins的一套 centos7 jdk1.8.0_261 tomcat-8.5.57 maven-3.6.3 git-2.9.5 jenkins-2.164.3 nexus-3.25(必须为可连接外网的服务器,本次实验一并…

环境准备:

1、 jenkins机(外网环境)
10.69.92.89:用于搭建Jenkins的一套
centos7
jdk1.8.0_261
tomcat-8.5.57
maven-3.6.3
git-2.9.5
jenkins-2.164.3
nexus-3.25(必须为可连接外网的服务器,本次实验一并将其安装至10.69.92.89)
2、目的机(内网环境)
10.69.101.162:测试环境部署api代码的服务器
10.69.101.163:测试环境部署oss代码的服务器
3、gitlab地址:
git@10.69.138.12:zhangleipeng/ebtrust-app-iwealth.git

一、jdk/maven/git/jenkins(tomcat)

[root@bz-cf-zhgl-yy ~]# ll /home/ebapp/jenkins
total 234512
drwxrwxr-x 6 ebapp ebapp        99 Dec 15 17:27 apache-maven-3.6.3
-rw-rw-r-- 1 ebapp ebapp   9506321 Dec 15 17:25 apache-maven-3.6.3-bin.tar.gz
drwxrwxr-x 9 ebapp ebapp       220 Dec 15 17:27 apache-tomcat-8.5.57
-rw-rw-r-- 1 ebapp ebapp  10379806 Dec 15 17:25 apache-tomcat-8.5.57.tar.gz
lrwxrwxrwx 1 ebapp ebapp        12 Dec 17 09:24 jdk -> jdk1.8.0_261
drwxr-xr-x 8 ebapp ebapp       273 Jun 18  2020 jdk1.8.0_261
-rw-rw-r-- 1 ebapp ebapp 143111803 Dec 15 17:29 jdk-8u261-linux-x64.tar.gz
-rw-rw-r-- 1 ebapp ebapp  77133106 Dec 17 20:12 jenkins-2.164.3-1.1.noarch.rpm
lrwxrwxrwx 1 ebapp ebapp        18 Dec 17 08:59 maven -> apache-maven-3.6.3
lrwxrwxrwx 1 ebapp ebapp        20 Dec 17 08:59 tomcat -> apache-tomcat-8.5.57
[root@bz-cf-zhgl-yy ~]#  yum install vim gcc gcc-c++ wget net-tools lrzsz iotop lsof iotop bash-completion curl policycoreutils openssh-server openssh-clients postfix -y

1、编译安装git

#1、卸载旧git,安装新git:
#把git压缩包拉至/usr/local/git下,并解压
# cd git-2.9.5
#移除之前安装的git
# yum remove git
# cd /usr/local
# mkdir git && cd git
# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
# make prefix=/usr/local/git all
# make prefix=/usr/local/git install
#设置环境变量
# vim  /etc/profile
export PATH=/usr/local/git/bin:$PATH
# source  /etc/profile
#查看git的版本,确实为安装的2.9.5
# git --version
git version 2.9.5

#2、配置姓名邮箱
[ebapp@jenkins]$ git config --global user.name "wanglu"
[ebapp@jenkins]$ git config --global user.email 2425942026@qq.com

#3、配置私钥公钥,实现免密拉取代码:
[ebapp@jenkins]$ ssh-keygen -t rsa -C "2425942026@qq.com"
##将此公钥粘贴至gitlab仓库中:User Settings-->SSH Keys-->add key
[ebapp@jenkins]$ cat /root/.ssh/id_rsa.pub
##将此私钥粘贴至Jenkins网页版:Jenkin---->系统管理---->凭据---->jenkins---->全局凭据---->添加凭据(ssh认证,用户私钥)
[ebapp@jenkins]$ cat /root/.ssh/id_rsa
#验证1:不再需要用户名密码
[root@bz-cf-zhgl-yy gitdev]# git clone git@10.69.138.12:zhangleipeng/ebtrust-app-iwealth.git
Cloning into 'ebtrust-app-iwealth'...
#验证2:Jenkins网页创建任务

2、jdk卸载及重装

#1、卸载旧jdk
[ebapp@jenkins]$ rpm -qa |grep java
[ebapp@jenkins]$ rpm -e --nodeps java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86_64
[ebapp@jenkins]$ rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.66.1.13.0.el6.x86_64

#2、安装新jdk
[ebapp@jenkins]$ ll /home/ebapp/jenkins/
lrwxrwxrwx  1 ebapp ebapp        12 Dec 17 09:24 jdk -> jdk1.8.0_261
drwxr-xr-x  8 ebapp ebapp       273 Jun 18 14:59 jdk1.8.0_261
-rw-rw-r--  1 ebapp ebapp 143111803 Dec 15 17:29 jdk-8u261-linux-x64.tar.gz
#配置环境变量
[ebapp@jenkins]$  vim /etc/profile
export JAVA_HOME=/home/ebapp/jenkins/jdk 
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
[ebapp@jenkins]$ source /etc/profile 
#验证版本是否为新版本
[ebapp@jenkins]$  ln -sv /home/ebapp/jenkins/jdk/bin/java /usr/bin/
[ebapp@jenkins]$ java -version

3、maven

[ebapp@jenkins]$ ll /home/ebapp/jenkins
total 230688
drwxrwxr-x  6 ebapp ebapp        99 Dec 15 17:27 apache-maven-3.6.3
-rw-rw-r--  1 ebapp ebapp   9506321 Dec 15 17:25 apache-maven-3.6.3-bin.tar.gz
lrwxrwxrwx  1 ebapp ebapp        18 Dec 17 08:59 maven -> apache-maven-3.6.3
[ebapp@jenkins]# ln -sv /home/ebapp/jenkins/maven/bin/mvn /usr/bin/
#配置环境变量:
[ebapp@jenkins]$  vim /etc/profile
export MAVEN_HOME=/home/ebapp/jenkins/maven
export PATH=$MAVEN_HOME/bin:$PATH
[ebapp@jenkins]$ source /etc/profile 

#验证
[ebapp@jenkins]$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /home/ebapp/jenkins/maven
Java version: 1.8.0_261, vendor: Oracle Corporation, runtime: /home/ebapp/jenkins/jdk1.8.0_261/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1127.el7.x86_64", arch: "amd64", family: "unix"

#配置setting文件--配置maven私服nexus:
# vim /home/ebapp/jenkins/maven/conf/settings.xml
  <servers>  
  		<server>
       			 <id>nexus-snapshots</id>
        		 <username>admin</username>
        		 <password>admin123</password>
  		</server>
  <servers>  
 
  </mirrors>
        <mirror>
                <id>maven-public</id>
                <mirrorOf>central</mirrorOf>
                <name>nexus aliyun</name>
                <url>http://10.69.92.89:8081/repository/maven-public</url>
        </mirror>
         <mirror>
                <id>aliyun</id>
                <mirrorOf>aliyun</mirrorOf>
                <name>aliyun</name>
                <url>http://10.69.92.89:8081/repository/aliyun</url>
        </mirror>
  </mirrors>
!!!二、nexus完成后再继续以下打包操作

4、服务器上进行测试:git拉取代码、maven进行打包编译

[ebapp@jenkins]$ mkdir gitlab && cd gitlab
[ebapp@jenkins]$ git clone http://10.69.138.12/gitlab/zhangleipeng/ebtrust-app-iwealth
[ebapp@jenkins]$ cd ebtrust-app-iwealth/
[ebapp@jenkins]$ ll
# vim /home/ebapp/gitlab/ebtrust-app-iwealth/iwealth-common/pom.xml
        <dependency>
            <groupId>com.csii.payment</groupId>
            <artifactId>client</artifactId>
            <version>1.0.0</version>
        </dependency>
# mvn clean install -P test
# mvn clean install -U -e

##脚本编写及将其布置在Jenkins网页上

5、jenkins安装、启动

(1)jenkins安装、启动

#Jenkins三种安装启动方式:
#方法一、war包直接启动:
[ebapp@jenkins]$ java -jar jenkins.war
#网址:http://10.69.92.89:8080

#方法二、war包tomcat下安装启动:
#将Jenkins.war放至tomcat/webapps目录下
[ebapp@jenkins]$ sh tomcat/bin/startup.sh
#网址:http://10.69.92.89:8080/jenkins/

#方法三:编译安装启动(推荐!!):
#3.1、centos安装启动:
 wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
 rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
 yum install jenkins -y
#3.2、直接安装rpm包(本次安装使用该方式!!)
 rpm -ivh jenkins-2.164.3-1.1.noarch.rpm
 #配置文件
 vim /etc/sysconfig/jenkins
 #使用Jenkins用户可能有很多权限没有,所以建议使用root用户
 JENKINS_USER="root"
 JENKINS_PORT="8888"
[root@bz-cf-zhgl-yy ~]# rpm -qa jenkins
jenkins-2.164.3-1.1.noarch
#启动Jenkins
[root@bz-cf-zhgl-yy ~]# systemctl start jenkins
[root@bz-cf-zhgl-yy ~]# systemctl status jenkins
#登录网址:http://10.69.92.89:8888

(2)Jenkins网页设置:

网址:http://10.69.92.89:8888
解锁Jenkins–默认选择的插件–创建Jenkins管理员(账号jenkinsadmin密码jenkinsadmin)—Jenkins URL(http://10.69.92.89:8888)–开始使用Jenkins–登录

1、插件安装及管理

#查看当前插件
[11:20:08 root@centos7-jenkins ~]#pwd
/root
[11:20:08 root@centos7-jenkins ~]#ll .jenkins/plugins/

安装插件:(GitLab,Gitlab Authentication,Gitlab Hook,Blue Ocean)
登录Jenkins–系统管理–插件管理–可选插件(搜索git)–直接安装–安装完成后重启Jenkins(空闲时)

2、配置jinkens权限管理
安装插件:Role-based Authorization Strategy
Jenkins—系统管理—管理用户-新建用户

3、更改认证方式:
Jenkins—系统管理—全局安全配置— 勾选Role-Based Strategy(角色认证)

4、创建角色:
Jenkins—系统管理–Manage and Assign Roles—Manage Roles—添加custom-role角色,read全部权限
Jenkins—系统管理–Manage and Assign Roles—Assign Roles—User/group to add wanglu,勾选custom-role角色–使用wanglu用户登录进行测试

5、系统配置–邮箱配置
Jenkins—系统管理–系统配置–Jenkins Location:系统管理员邮件地址(2425942026@qq.com)
Jenkins—系统管理–系统配置–邮件通知-测试发邮件–详见文档4.1.13

6、全局工具配置
路径必须正确,否则后续构建会报错。配置后点击保存。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7、配置 jenkins 自动拉取代码(见上“编译安装git”):
jenkins 服务器添加证书:
Jenkin---->系统管理---->凭据---->jenkins---->全局凭据---->添加凭据(ssh认证,用户私钥)

8、新建任务:
(1)自由风格项目:test-1
(2)流水线项目:test-2
(3)实战项目:
自由风格项目–源码:
Repository URL:git@192.168.149.134:test-service/test-project.git
Credentials:wanglu(gitlab)
Branches to build:根据实际场景选择对应的分支
构建-增加构建步骤-执行shell(把调用的shell都准备好):
在这里插入图片描述

测试构建项目:
立即构建---->#1---->控制台输出
服务器验证数据:

# ps -ef |grep java
tomcat    5001     1  1 17:07 ?        00:00:43 java -Xms4096m -Xmx4096m -jar iwealth-web-api-1.0.0-SNAPSHOT.jar --spring.profiles.active=test --server.port=8081
tomcat    5234  3746  0 17:59 pts/5    00:00:00 grep --color java
# tail -f application/logs/api_out.log 
# ll backup/
drwxrwxr-x 2 tomcat tomcat 4096 1月  13 10:55 2021-01-13

二、Nexus

1、Nexus安装

[15:51:11 root@sptapp apps]#tar xvf nexus-3.25.0-03-unix.tar.gz 
[15:51:58 root@sptapp apps]#ll
total 156308
-rw-r--r-- 1 root  root  160022971 Jul 29 17:54 nexus-3.25.0-03-unix.tar.gz
drwxr-xr-x 9 root  root       4096 Dec  8 15:51 nexus-3.25.0-03
drwxr-xr-x 3 root  root       4096 Dec  8 15:51 sonatype-work
[15:52:10 root@sptapp apps]#cd nexus-3.25.0-03
[13:49:32 root@sptapp apps]#vim /etc/profile
export RUN_AS_USER=root
[13:51:47 root@sptapp apps]#source /etc/profile
#启动nexus
[13:53:08 root@sptapp apps]#nexus-3.25.0-03/bin/nexus start
#nexus服务启动成功后,需要稍等一段时间,8081端口才起来
[13:57:04 root@sptapp apps]#lsof -i:8081
#Nexus的工作目录是sonatype-work
[14:34:14 root@sptapp ~]#ls apps/sonatype-work/nexus3
backup blobs cache db elasticsearch etc generated-bundles health-check instances keystores lock log orient port tmp
#设置-repository-blobcd .. storiesccd对应路径:
[14:34:14 root@sptapp ~]#ls /apps/sonatype-work/nexus3/blobs

2、Nexus网页设置:

1、网址访问:http://10.69.92.89:8081/
(如果出现404,就访问http://localhost:8081/nexus)
2、登录:点击右上角“sign in”,输入默认用户名admin和默认密码admin123登录
(如果出现报错:Incorrect username or password, or no permission to use the application.将所提示的密码文件/apps/sonatype-work/nexus3/admin.password中的密码输入即可,之后根据提示修改密码即可)
3、可以点击上面的“设置”图标,在“设置”里可以添加用户、角色,对接LDAP等的设置
4、设置-repositories–create repository:
选择要创建的类型:maven2(proxy)
Name:就是为代理起个名字
Remote Storage: 代理的地址,Maven的地址为: https://repo1.maven.org/maven2/
Blob Store: 选择代理下载包的存放路径
5、放必要jar包

-------------------!!!!!!!!未完!!!!!!!-----

三、目的机

telnet 目标机的22端口不通,修改目标机为10022端口

[root@osstest ~]# vim /etc/ssh/sshd_config 
Port 22
Port 10022
#重启sshd服务
[root@osstest ~]# /etc/init.d/sshd  restart
#验证:
#telnet通即可

四、shell脚本

1、10.69.101.162脚本:

[root@apptest ~]#  su - tomcat

#备份脚本
[tomcat@apptest ~]$ vim application/backup.sh
#!/bin/bash
cd /home/tomcat/application/
backdir="backup/`date "+%Y-%m-%d"`"
jardir="/home/tomcat/application/iwealth-web-api-1.0.0-SNAPSHOT.jar"
mkdir $backdir
cp $jardir $backdir
echo "已将$jardir备份至目录$backdir"

#重启脚本:
[tomcat@apptest ~]$ vim application/restart.sh
#!/bin/bash
cd /home/tomcat/application/
pid=`ps axu | grep iwealth-web-api-1.0* | grep -v grep| awk '{print$2}'`
if [ "$pid" = "" ];then
    echo "java服务没有启动!"
else
    kill -9 $pid
    pid1=`ps axu | grep iwealth-web-api-1.0* | grep -v grep| awk '{print$2}'`
    if [ "$pid1" = "" ];then
            echo "成功杀死进程:" $pid
        else
                echo "进程杀死失败!"
                exit 1
            fi
fi
sleep 3;
nohup java  -Xms4096m -Xmx4096m -jar iwealth-web-api-1.0.0-SNAPSHOT.jar --spring.profiles.active=test --server.port=8081 >/home/tomcat/application/logs/api_out.log 2>&1 &
pid2=`ps axu | grep iwealth-web-api-1.0* | grep -v grep| awk '{print$2}'`
echo "成功启动进程:" $pid2

2、10.69.101.163脚本:

[root@osstest ~]#  su - tomcat

#备份脚本
[tomcat@osstest ~]$ vim application/backup.sh
#!/bin/bash
backdir="backup/`date "+%Y-%m-%d"`"
jardir="application/iwealth-web-oss-1.0.0-SNAPSHOT.jar"
mkdir $backdir
cp $jardir $backdir
echo "已将$jardir备份至目录$backdir"

#重启脚本:
[tomcat@osstest ~]$ vim application/restart.sh
#!/bin/bash
cd /home/tomcat/application
pid=`ps axu | grep iwealth-web-oss-1-1.0* | grep -v grep| awk '{print$2}'`
if [ "$pid" = "" ];then
    echo "java服务没有启动!"
else
    kill -9 $pid
    pid1=`ps axu | grep iwealth-web-oss-1-1.0* | grep -v grep| awk '{print$2}'`
    if [ "$pid1" = "" ];then
            echo "成功杀死进程:" $pid
        else
                echo "进程杀死失败!"
                exit 1
            fi
fi
sleep 1;
nohup java -jar /home/tomcat/application/iwealth-web-oss-1.0.0-SNAPSHOT.jar --spring.profiles.active=test >/home/tomcat/application/logs/oss_out.log 2>&1 &
pid2=`ps axu | grep iwealth-web-oss-1-1.0* | grep -v grep| awk '{print$2}'`
echo "成功启动进程:" $pid2

3、10.69.92.89脚本:

#部署脚本
[root@bz-cf-zhgl-yy ~]# cd /home/ebapp/apps
#实现远程ssh免密登录
[root@bz-cf-zhgl-yy apps]# yum install -y sshpass

[root@bz-cf-zhgl-yy apps]# vim deployapi.sh
#!/bin/bash
apifile="/var/lib/jenkins/workspace/APP-test-api自动化部署/iwealth-web-api/target/iwealth-web-api-1.0.0-SNAPSHOT.jar"
targetdir="/home/tomcat/application/"
echo -e " \033[31m================开始部署APP-test-api======================== \033[0m"
echo -e "\033[31m1)备份中...\033[0m"
sshpass -p 'tomcat' ssh -p 10022 tomcat@10.69.101.162 "source /etc/profile;sh /home/tomcat/application/backup.sh"
sleep 3
echo -e "\033[31m2)部署中...\033[0m"
sshpass -p 'tomcat' scp -P 10022 $apifile tomcat@10.69.101.162:$targetdir
echo "已将$apifile放至$targetdir"
sleep 3
echo -e "\033[31m3)重启中...\033[0m"
sshpass -p 'tomcat' ssh -p 10022 tomcat@10.69.101.162 "source /etc/profile;sh /home/tomcat/application/restart.sh"
sleep 3
echo -e " \033[31m================部署完成APP-test-api======================== \033[0m"

[root@bz-cf-zhgl-yy apps]# vim deployoss.sh 
#!/bin/bash
ossfile="/var/lib/jenkins/workspace/APP-test-oss自动化部署/iwealth-web-api/target/iwealth-web-oss-1.0.0-SNAPSHOT.jar"
targetdir="/home/tomcat/application/"
echo -e " \033[31m================开始部署APP-test-oss======================== \033[0m"
echo -e "\033[31m1)备份中...\033[0m"
sshpass -p 'tomcat' ssh -p 10022 tomcat@10.69.101.163 "source /etc/profile;sh /home/tomcat/application/backup.sh"
echo "已将$ossfile放至$targetdir"
sleep 3
echo "-------备份完成-------"
echo -e "\033[31m2)部署中...\033[0m"
sshpass -p 'tomcat' scp -P 10022 $ossfile tomcat@10.69.101.163:$targetdir
sleep 3
echo "-------部署完成-------"
echo -e "\033[31m3)重启中...\033[0m"
sshpass -p 'tomcat' ssh -p 10022 tomcat@10.69.101.163 "source /etc/profile;sh /home/tomcat/application/restart.sh"
sleep 3
echo "-------重启jar包完成-------"
echo -e " \033[31m================部署完成APP-test-oss======================== \033[0m"

本文链接: http://www.dtmao.cc/news_show_600068.shtml

附件下载

上一篇:kvm的基本命令使用

下一篇:金融之路

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?