持续集成-交付-发布-项目

**作者:行癫(盗版必究)** ------ ## 一:Jenkins部署 #### 1.安装插件 ```shell • Maven插件 Maven Integration plugin • 发布插件 Deploy to container Plugin 需要安装插件如下: 安装插件Deploy to container ---支持自动化代码部署到tomcat容器 Git 插件 可能已经安装 Maven Integration :jenkins利用Maven编译,打包所需插件 Publish Over SSH :通过ssh连接 ``` #### 2.安装过程 系统管理--->插件管理---->可选插件--->过滤Deploy to container---->勾选--->直接安装 image-20220627235514357 image-20220627235607259 #### 3.配置SSH image-20220627235758575 ![image-20220627235903284](%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90-%E4%BA%A4%E4%BB%98-%E5%8F%91%E5%B8%83-%E9%A1%B9%E7%9B%AE.assets/image-20220627235903284.png) image-20220628000008461 注意: ​ key:指的是jenkins服务器用哪个用户去远程连接程序服务器就是那个用户的私钥。(记得做免密 jenkins---> web-server) ![img](%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90-%E4%BA%A4%E4%BB%98-%E5%8F%91%E5%B8%83-%E9%A1%B9%E7%9B%AE.assets/K2KQE6euhjvTIomINthtvg.png) #### 4.配置jdk,maven,git image-20220628000213404 image-20220628000256756 image-20220628000323472 image-20220628000358938 ## 二:构建发布任务 #### 1.创建maven项目 image-20220628000510980 image-20220628000547600 #### 2.配置项目 image-20220628000721842 image-20220628000814797 https://github.com/bingyue/easy-springmvc-maven ​ ![img](%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90-%E4%BA%A4%E4%BB%98-%E5%8F%91%E5%B8%83-%E9%A1%B9%E7%9B%AE.assets/vxtF-uRF9X71FkT1Ac_JIA.png) 注意: ​ 新版本需要将默认分支改为main image-20220628003247070 image-20220628003459003 image-20220628003538519 调用mave命令 (clean package -Dmaven.test.skip = true) image-20220628003556160 保存: image-20220628003642699 #### 3.发布服务器部署 ```shell [root@java-server ~]# mkdir -p /data/application 上传jdk [root@java-server ~]# tar xzf jdk-8u191-linux-x64.tar.gz -C /usr/local/ [root@java-server ~]# cd /usr/local/ [root@java-server local]# mv jdk1.8.0_191/ java 下载tomcat [root@java-server ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.42/bin/apache-tomcat-8.5.42.tar.gz [root@java-server ~]# tar xzf apache-tomcat-8.5.42.tar.gz -C /data/application/ [root@java-server ~]# cd /data/application/ [root@java-server application]# mv apache-tomcat-8.5.42/ tomcat 设置环境变量 [root@java-server ~]# vim /etc/profile export JAVA_HOME=/usr/local/java export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar export TOMCAT_HOME=/data/application/tomcat [root@java-server ~]# source /etc/profile 测试: [root@java-server ~]# java -version java version "1.8.0_191" Java(TM) SE Runtime Environment (build 1.8.0_191-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode) 删除tomcat默认发布目录下面的内容: [root@java-server ~]# rm -rf /data/application/tomcat/webapps/* [root@java-server ~]# cd /data/application/tomcat/webapps/ [root@java-server webapps]# ls 创建目录和脚本: [root@java-server ~]# mkdir /opt/script #创建脚本目录 [root@java-server ~]# vim app-jenkins.sh #创建脚本 #!/usr/bin/bash #本脚本适用于jenkins持续集成,实现备份war包到代码更新上线!使用时请注意全局变量。 #================ #Defining variables export JAVA_HOME=/usr/local/java webapp_path="/data/application/tomcat/webapps" tomcat_run="/data/application/tomcat/bin" updata_path="/data/update/`date +%F-%T`" backup_path="/data/backup/`date +%F-%T`" tomcat_pid=`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'` files_dir="easy-springmvc-maven" files="easy-springmvc-maven.war" job_path="/root/upload" #Preparation environment echo "Creating related directory" mkdir -p $updata_path mkdir -p $backup_path echo "Move the uploaded war package to the update 2directory" mv $job_path/$files $updata_path echo "=========================================================" cd /opt echo "Backing up java project" if [ -f $webapp_path/$files ];then tar czf $backup_path/`date +%F-%H`.tar.gz $webapp_path if [ $? -ne 0 ];then echo "打包失败,自动退出" exit 1 else echo "Checking if tomcat is started" if [ -n "$tomcat_pid" ];then kill -9 $tomcat_pid if [ $? -ne 0 ];then echo "tomcat关闭失败,将会自动退出" exit 2 fi fi cd $webapp_path rm -rf $files && rm -rf $files_dir cp $updata_path/$files $webapp_path cd /opt $tomcat_run/startup.sh sleep 5 echo "显示tomcat的pid" echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`" echo "tomcat startup" echo "请手动查看tomcat日志。脚本将会自动退出" fi else echo "Checking if tomcat is started" if [ -n "$tomcat_pid" ];then kill -9 $tomcat_pid if [ $? -ne 0 ];then echo "tomcat关闭失败,将会自动退出" exit 2 fi fi cp $updata_path/$files $webapp_path $tomcat_run/startup.sh sleep 5 echo "显示tomcat的pid" echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`" echo "tomcat startup" echo "请手动查看tomcat日志。脚本将会自动退出" fi [root@java-server ~]# vim app-jenkins.sh 脚本内容在后面 [root@java-server ~]# chmod +x app-jenkins.sh #添加执行权限 [root@java-server ~]# mv app-jenkins.sh /opt/script/ ``` #### 4.开始构建任务 ![img](%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90-%E4%BA%A4%E4%BB%98-%E5%8F%91%E5%B8%83-%E9%A1%B9%E7%9B%AE.assets/3CiCTOTQ2tZ0n6pHy_9N8Q.png) ![img](%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90-%E4%BA%A4%E4%BB%98-%E5%8F%91%E5%B8%83-%E9%A1%B9%E7%9B%AE.assets/8Zvu0JAjLexNZw4McJs9mw.png) ![img](%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90-%E4%BA%A4%E4%BB%98-%E5%8F%91%E5%B8%83-%E9%A1%B9%E7%9B%AE.assets/xCOkP5CTpof6CmgXsPzrDQ.png) ![img](%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90-%E4%BA%A4%E4%BB%98-%E5%8F%91%E5%B8%83-%E9%A1%B9%E7%9B%AE.assets/vGcPCV6C-Ro_9NcJONgW7g.png)