在日常开发过程中,经常会使用到jenkins进行项目部署,但对一些小项目来说,这就过于复杂,就可以使用Alibaba Cloud Toolkit插件配合shell脚本进行项目的远程部署工作。
一、下载Alibaba Cloud Toolkit插件

二、服务器安装nohup
1.安装coreutils
yum install coreutils
2.编辑 ~/.bash_profile,PATH=PATH:HOME/bin后面追加:/usr/bin
vi ~/.bash_profile

3.刷新配置
source ~/.bash_profile

三、创建脚本
1.新建一个shell脚本用于启动springboot项目,并将该文件放到一个指定目录/home/project/test
Lua
#!/bin/bash
# 检查传入的参数是否正确
if [ $# -ne 1 ]; then
echo "用法: $0 <jar文件路径>"
exit 1
fi
jar_file="$1"
# 检查是否传入的文件存在
if [ ! -f "$jar_file" ]; then
echo "指定的jar文件不存在: $jar_file"
exit 1
fi
# 获取项目所在目录
project_dir=$(dirname "$jar_file")
# 查找与指定的.jar文件相关的进程并杀死它们
pids=$(ps aux | grep "$jar_file" | grep -v grep | awk '{print $2}')
if [ -n "$pids" ]; then
echo "正在终止相关进程: $pids"
kill -9 $pids
fi
# 删除项目所在目录的nohup.out文件
if [ -f "$project_dir/nohup.out" ]; then
rm "$project_dir/nohup.out"
echo "已删除 $project_dir/nohup.out 文件."
fi
# 使用nohup启动.jar文件
nohup java -jar "$jar_file" > "$project_dir/nohup.out" 2>&1 &
echo "$jar_file 已启动."
2.添加文件可执行权限
Lua
chmod 777 /home/project/test/test.sh

四、部署到服务器
1.配置服务器主机信息View -> Tool Windows -> Alibaba Cloud View

2.添加host

3. 配置发布到主机 Tools -> Alibaba Cloud -> Deloy to Host

4.配置部署主机,以及项目部署路径和执行脚本
/home/project/test/test.sh :添加的脚本路径
SpingBootTest-0.1.1.jar:打包后的包名
Lua
#执行脚本
/home/project/test/test.sh SpingBootTest-0.1.1.jar

5.运行run,项目成功构建上传并运行
