CI-CD/java-项目.md
2025-04-28 22:36:46 +08:00

236 lines
6.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h2><center>持续集成-交付-部署-Java 项目</center></h2>
------
## 一:环境
| 服务器 | IP地址 | 说明 |
| ------- | --------------- | -------------- |
| GitLab | 192.168.159.136 | 私有仓库 |
| Jenkins | 192.168.159.137 | 自动化部署 |
| web | 192.168.159.138 | 项目所属服务器 |
三台服务器相互传ssh私钥
## 二GitLab
### 1. 配置SSH
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280075.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280076.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280077.png)
### 2. 创建项目
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280078.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280079.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280080.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280081.png)
## 三Jenkins
### 1. 安装插件
```bash
• Maven插件 Maven Integration plugin
• 发布插件 Deploy to container Plugin
• Git 插件 可能已经安装
• Pipeline Maven Integration:jenkins利用Maven编译打包所需插件
• Publish Over SSH:通过ssh连接
```
### 2. 配置凭证
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280062.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280063.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280064.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280065.png)
**用户密码模式**
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280066.png)
**SSH密钥模式**
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280067.png)
### 3. 配置SSH
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280068.png)
Jenkins服务器私钥传给web服务器
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280069.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280070.png)
### 4. jdk git maven配置
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280071.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280072.png)
Jenkins服务器安装git
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280073.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280074.png)
## 四web
### 1. 拉取仓库
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280082.png)
```bash
[root@web ~]# cd /opt
[root@web opt]# git clone http://192.168.159.136/root/easy-springmvc-maven.git
```
### 2. 获取测试文件
```bash
[root@web ~]# wget http://182.92.143.66:40072/directlink/img/java-demo/easy-springmvc-maven-master.zip
[root@web ~]# cp -r easy-springmvc-maven-master/* /opt/easy-springmvc-maven/
```
### 3. 上传代码
```bash
[root@web easy-springmvc-maven]# git add .
[root@web easy-springmvc-maven]# git commit -m "first"
[root@web easy-springmvc-maven]# git push
```
### 4. 安装jdk tomcat
```bash
[root@web ~]# tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@web ~]# ln -s /usr/local/jdk1.8.0_211/ /usr/local/jdk8
[root@web ~]# vim /etc/profile
JAVA_HOME=/usr/local/jdk8
MAVEN_HOME=/usr/local/maven
PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
export PATH JAVA_HOME MAVEN_HOME
[root@web ~]# source /etc/profile
[root@web ~]# mkdir -p /data/application
[root@web ~]# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.104/bin/apache-tomcat-9.0.104.tar.gz
[root@web ~]# tar xf apache-tomcat-9.0.104.tar.gz -C /data/application/
[root@web ~]# mv /data/application/apache-tomcat-9.0.104/ /data/application/tomcat
[root@web ~]# vim /data/application/tomcat/conf/server.xml
取消8009的注释
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector protocol="AJP/1.3"
address="::1"
port="8009"
redirectPort="8443"
maxParameterCount="1000"
/>
```
### 5. 编写脚本
```bash
[root@web ~]# vim /opt/jenkins.sh
#!/usr/bin/bash
#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@web ~]# chmod +x /opt/jenkins
```
## 五jenkins创建项目
### 1. 创建项目
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280083.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280084.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280085.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280086.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280087.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280088.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280089.png)
### 2. 构建
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280090.png)
![](http://182.92.143.66:40072/directlink/img/CI-CD/image-202504280091.png)