首先安装git和maven,设置好git的账号密码保证可以pull代码。
创建文件:
touch test.sh
然后写入:
#!/bin/bash echo ================================= #下面用来测试的项目git地址:https://gitee.com/zhuhongliang/WebHelloWorld.git echo WebHelloWorld自动化部署脚本启动 echo ================================= echo 进入/data/git/WebHelloWorld #这个地址clone到本地路径 cd /data/git/WebHelloWorld echo 开始pull版本 git pull echo 开始编译文件 mvn clean package -Dmaven.test.skip=true echo 进入编译完成文件 cd /data/git/WebHelloWorld/target echo 移动文件至:/data/application/web_hello_world-0.0.1.jar mv web_hello_world-0.0.1.jar /data/application/web_hello_world-0.0.1.jar current_pid=$( netstat -ntlp | grep :8080 | awk '{print $7}' | awk -F"/" '{ print $1 }') #我的这个springboot的默认启动端口是8080 if [ $current_pid ] then echo 发现应用正在运行,先关闭应用 #这里的逻辑是先用端口来查找程序的pid,如果pid为空则说明程序没有运行,如果pid不为空则kill程序 kill -9 $current_pid fi echo 重启服务 nohup java -jar /data/application/web_hello_world-0.0.1.jar > /dev/null 2>&1 & #jar包的启动命令,可以在再加上springboot配置和jvm配置 echo ================================= echo 部署完成 echo =================================
保存然后让程序可执行:
chmod +x ./test.sh
然后执行:
./test.sh
看到屏幕输出:
[root@centos7641 data]# ./test.sh ================================= WebHelloWorld自动化部署脚本启动 ================================= 进入/data/git/WebHelloWorld 开始pull版本 remote: Enumerating objects: 17, done. remote: Counting objects: 100% (17/17), done. remote: Compressing objects: 100% (6/6), done. remote: Total 9 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (9/9), done. 来自 https://gitee.com/zhuhongliang/WebHelloWorld c9d92e6..50420da master -> origin/master 更新 c9d92e6..50420da Fast-forward src/main/java/com/zhl/web_hello_world/Controller.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 开始编译文件 [INFO] Scanning for projects... [INFO] [INFO] ----------------------< com.zhl:web_hello_world >----------------------- [INFO] Building web_hello_world 0.0.1 [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ web_hello_world --- [INFO] Deleting /data/git/WebHelloWorld/target [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ web_hello_world --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ web_hello_world --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 4 source files to /data/git/WebHelloWorld/target/classes [INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ web_hello_world --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ web_hello_world --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ web_hello_world --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ web_hello_world --- [INFO] Building jar: /data/git/WebHelloWorld/target/web_hello_world-0.0.1.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:repackage (repackage) @ web_hello_world --- [INFO] Replacing main artifact with repackaged archive [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.171 s [INFO] Finished at: 2019-09-11T23:51:02+08:00 [INFO] ------------------------------------------------------------------------ 进入编译完成文件 移动文件:/data/application/web_hello_world-0.0.1.jar 发现应用正在运行,先关闭应用 启动服务 ================================= 部署完成 =================================