You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ci-cd/md/持续集成-交付-发布-项目.md

208 lines
9.1 KiB

<h1><center>持续集成-交付-发布-项目</center></h1>
**作者:行癫(盗版必究)**
------
## 一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---->勾选--->直接安装
<img src="%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-20220627235514357.png" alt="image-20220627235514357" style="zoom:50%;" />
<img src="%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-20220627235607259.png" alt="image-20220627235607259" style="zoom:50%;" />
#### 3.配置SSH
<img src="%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-20220627235758575.png" alt="image-20220627235758575" style="zoom:50%;" />
![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)
<img src="%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-20220628000008461.png" alt="image-20220628000008461" style="zoom:50%;" />
注意:
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.配置jdkmavengit
<img src="%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-20220628000213404.png" alt="image-20220628000213404" style="zoom:50%;" />
<img src="%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-20220628000256756.png" alt="image-20220628000256756" style="zoom:50%;" />
<img src="%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-20220628000323472.png" alt="image-20220628000323472" style="zoom:50%;" />
<img src="%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-20220628000358938.png" alt="image-20220628000358938" style="zoom:50%;" />
## 二:构建发布任务
#### 1.创建maven项目
<img src="%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-20220628000510980.png" alt="image-20220628000510980" style="zoom:50%;" />
<img src="%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-20220628000547600.png" alt="image-20220628000547600" style="zoom:50%;" />
#### 2.配置项目
<img src="%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-20220628000721842.png" alt="image-20220628000721842" style="zoom:50%;" />
<img src="%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-20220628000814797.png" alt="image-20220628000814797" style="zoom:50%;" />
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
<img src="%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-20220628003247070.png" alt="image-20220628003247070" style="zoom:50%;" />
<img src="%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-20220628003459003.png" alt="image-20220628003459003" style="zoom:50%;" />
<img src="%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-20220628003538519.png" alt="image-20220628003538519" style="zoom:50%;" />
调用mave命令
clean package -Dmaven.test.skip = true
<img src="%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-20220628003556160.png" alt="image-20220628003556160" style="zoom:50%;" />
保存:
<img src="%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-20220628003642699.png" alt="image-20220628003642699" style="zoom:50%;" />
#### 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)