如何用shell脚本自动部署Springboot项目
在开发Springboot项目时,我们可能需要经常更新服务器上的代码,并把项目部署在服务器上,而每次都需要输入一连串的命令,这样效率实在不高,所以写个脚本,利用脚本执行所有的命令,可以大大提升速度(bi ge)。
环境
- 云服务器操作系统为Centos/Linux系统
- 代码使用git管理
- 打成jar包
- 之前,手动部署过项目,运行环境没有问题(java/nohup/git等都能正常使用)
编写shell脚本
1. 在项目根目录下创建脚本
touch build.sh
2. 授权
chmod 755 build.sh
3. 编写脚本
这里使用的是8081端口,Springboot项目打包后,会多出一个target目录,jar包就存放在该目录下,所以我把项目运行后的日志也存放在target下。因为是重新部署,所以需要停止原来的项目,杀死占用8081端口的进程,具体见以下脚本。
#!/bin/bash
echo '自动部署Springboot项目脚本...'
echo '1. 拉取github代码...'
git pull
echo '2. 检查8081端口是否被占用...'
pid_blog=`lsof -i :8081|grep -v "PID"|awk '{print $2}'`
if [ "$pid_blog" != "" ];
then
echo '8081端口被占用'
echo $pid_blog
kill -9 "$pid_blog"
echo $pid_blog '进程已被杀死'
else
echo "端口未被占用"
fi
echo '3. 清理原有项目...'
mvn clean
echo '4. 打包...'
mvn install
echo '5. 后台运行jar包...'
nohup java -jar target/my-blog-3.0.1-SNAPSHOT.jar > target/blog.out &
4. 执行
./build.sh
打开浏览器访问你的地址或者打开日志查看启动是否成功
实际测试结果
[root@VM_0_12_centos blog]# ./test.sh
自动部署Springboot项目脚本...
1. 拉取github代码...
Already up-to-date.
2. 检查8081端口是否被占用...
8081端口被占用
14923
14923 进程已被杀死
3. 清理原有项目...
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.my.blog.website:my-blog >---------------------
[INFO] Building my-blog 3.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ my-blog ---
[INFO] Deleting /liqkjm/project/github/blog/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.787 s
[INFO] Finished at: 2018-08-28T13:16:45+08:00
[INFO] ------------------------------------------------------------------------
4. 打包...
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.my.blog.website:my-blog >---------------------
[INFO] Building my-blog 3.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-blog ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] Copying 185 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my-blog ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 92 source files to /liqkjm/project/github/blog/target/classes
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[3,16] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[4,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[3,16] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[4,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[3,16] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[4,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[60,20] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[67,38] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/DateKit.java: /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/DateKit.java uses or overrides a deprecated API.
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/DateKit.java: Recompile with -Xlint:deprecation for details.
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/MapCache.java: Some input files use unchecked or unsafe operations.
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/MapCache.java: Recompile with -Xlint:unchecked for details.
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResour