Spring-mvc/Spring-boot project automatic deployment implementation

    The author has recently completed two background projects, which need to be deployed on the Centos server of Alibaba Cloud at the same time. Project 1 is used for background content management, and project 2 is used to provide API calls for client APP. In order to facilitate operation and save time, the two projects The online deployment of the project is implemented by executing a "deploy.sh" automatic deployment script to realize the entire process of project source code pull, automatic packaging, and application startup. The following describes the directory tree structure of the project:

 

  • apache-tomcat-8.5.15 : used to start tomcat of the spring boot project from the outside, and the project package format is .war format.
  • logs log directory : specify the generation directory of the project log through log4j2.
  • project_deploy : project release directory, you need to edit the server.xml file in the tomcat/conf directory, and define the Context attribute in it: <Context docBase="/home/shitao/project_workspace/kaolayingyu-api/project_deploy/kaolayingyu-api" path=" " reloadable="true" />.
  • project_source : The source code directory, used to pull the latest source code from the remote Gittee repository.
  • deploy.sh : A script file that executes automatic deployment.

Automatic deployment steps :

In the first step, the modified code in the local development environment is submitted to the code warehouse on the Gitee site through GIT;

The second step is to enter the project_source directory, enter the git clone command in the console for the first time to establish a connection with the remote warehouse, and obtain all the source code of the project, and the subsequent automatic deployment is to obtain the latest from the code warehouse by executing the git pull command inside the deploy.sh script Modified source code;

The third step is to package the project source code into a .war format package through the mvn command.

The fourth step is to stop the running tomcat, copy the war package to the release directory project_deploy, decompress the war package, and restart tomcat.

Automatic deployment script deploy.sh :

project_name="kaolayingyu-api";
source_ws="/home/shitao/project_workspace/"$project_name"/project_source/"$project_name"/"
deploy_ws="/home/shitao/project_workspace/"$project_name"/project_deploy/"
tomcat_home="/home/shitao/project_workspace/"$project_name"/apache-tomcat-8.5.15"
cd $source_ws;
# 拉取最新的源代码
git pull
# 通过mvn命令生成项目的war包
mvn clean install -Dmaven.test.skip=true
cd target
# 杀死与项目相关的所有进程,与执行 sh ${tomcat_home}"/bin/shutdown.sh 效果等同
ps aux |grep -v grep |grep $project_name |awk '{print $2}'|xargs kill -9
#sh ${tomcat_home}"/bin/shutdown.sh"
rm -rf ${deploy_ws}$project_name
rm -rf ${deploy_ws}$project_name".war"
mkdir ${deploy_ws}$project_name
cp $project_name".war" ${deploy_ws}$project_name
cd ${deploy_ws}$project_name
jar xvf $project_name".war"
mv $project_name".war" ${deploy_ws}
cd WEB-INF/classes
# 修改项目配置文件,从开发环境切换到生产环境
sed -i 's/spring.profiles.active=dev/spring.profiles.active=prod/' application.properties
sh ${tomcat_home}"/bin/startup.sh"

    Although many spring-boot projects are now packaged as jar packages, they can be run directly through commands on the server side, but if your project needs to be run through an external tomcat, then the automatic deployment of the project can be realized through the above script file Yes, when deploying multiple different projects, you only need to replace the project-name in the script file with the new project name and you can happily do other things^_^

Guess you like

Origin blog.csdn.net/crazestone0614/article/details/126713793