Jenkins deploys SpringBoot application to remote server
Using SpringBoot and SpringCloud to write background services also introduces the popular concept of micro services, and there are many modules. Jenkins is used as a continuous integration tool to facilitate early testing and later online deployment and update.
structure
Server structure
- Test machine: several external network test or production hosts.
- Jenkins server: build Jenkins server host
- git code base: the code cloud I use here
Test environment directory structure
Create data in the root directory
/data: General catalogue of the project /data/exec: Execute script directory /data/pid: Record the running time of each program pid /data/work: Project application storage directory
Directory structure idea
- Building projects with maven
- After building, deploy to the remote server using Publish Over SSH:
- Close app
- The backup is applied to the lastDepoly directory
- Upload a new version of Jar package (or war package)
- Start application
- Among them, closing, backing up, deleting and starting applications are all completed by scripts, and the deployment script deploy SH is placed under / data/exec (see relevant script chapters for details).
- The execution order of push over SSH is to upload files first and then execute scripts, so I set two transfer sets for each application server:
- Execute the cleanup script first: close, back up and delete the old application jar package
deploy.sh cps-platform-1.0.0 clean
- Then execute the startup script to start the application
deploy.sh chl-tss start
cps-platform-1.0.0 package the name of the project and replace its own * jar name is enough.
jenkins
Download Jenkins
stay https://jenkins.io/download/ Download the latest version of Jenkins. It is recommended to download lts (longtermsuport) Jenkins war.
Create Jenkins directory structure and deploy to Jenkins server
Create the following structure on the Jenkins server
| - /jenkins/ | + pid # Record jenkins running pid | + conf # jenkins working directory | + log | jenkins.sh # jenkins.sh is for the convenience of starting and stopping Jenkins server | jenkins.war
- Start jenkins:
./jenkins.sh start
- Close jenkins:
./jenkins.sh stop
Jenkins.com will be downloaded Upload the war package to the Jenkins server and start it with the following command:
nohup java -Xms256m -Xmx1024m -XX:MaxPermSize=512m -jar jenkins.war --ajp13Port=-1 --httpPort=8099 > jenkins.out 2>&1 &
– httpPort=8099 specifies the port where jenkins starts listening. Here, it is changed to 8099 (8080 by default).
-Xms256m -Xmx1024m -XX:MaxPermSize=512m sets the JVM parameters (which need to vary according to the environment).
The basic installation process will not be described in detail. You can also refer to it here, springboot uses Jenkins for Automated Deployment Is using tomcat deployment
Add Publish Over SSH plug-in
Search Publish Over SSH in system settings - > plug-in management - > optional plug-ins, select and click Install Now to install.
Set up SSH Remote Server
Click "advanced" to set the port and password of Push Over SSH
- Check "Use password authentication, or use a different key"
- Password / password set password
- Port set port
Project configuration
Check Send files or execute commands over SSH after the build runs in the build environment, and then click Add Server to add the target remote server:
- Name: new remote server
- Source files: files that need to be uploaded. Wildcards and Jenkins variables can be used.
- Exec command: script executed on the remote server. My idea here is to clean up the remote server, shut down the service and back up the program.
cd /data/exec sh ./deploy.sh cps-platform-1.0.0 clean
Click Add Transfer Set to add a set of settings:
- Source files: files to be uploaded**/ cps-platform-1.0.0.jar. That is, the jar package file generated by maven packaging. The path is relative to the root directory of the maven project.
- Exec command: a script executed on a remote server. Here, you need to upload the file to the server first, and then execute the startup script
cd /data/exec sh ./deploy.sh cps-platform-1.0.0 start
- Click "advanced" and check flat files to flatten the file. Only upload the file, not the folder to which the file belongs. Otherwise, one more level of target directory will be added after uploading to the remote folder.
Related script
jenkins.sh
#!/bin/sh ## java env export JAVA_HOME=/usr/java/jdk1.8.0_261-amd64 export JRE_HOME=$JAVA_HOME/jre export JENKINS_HOME=/jenkins/conf ## exec shell name EXEC_SHELL_NAME=jenkins\.sh ## service name SERVICE_NAME=jenkins SERVICE_DIR=/jenkins JAR_NAME=$SERVICE_NAME\.war PID=pid/$SERVICE_NAME\.pid cd $SERVICE_DIR case "$1" in start) nohup java -Xms256m -Xmx1024m -XX:MaxPermSize=512m -jar $JAR_NAME --ajp13Port=-1 --httpPort=8099 > log/jenkins.out 2>&1 & echo $! > $SERVICE_DIR/$PID echo "#### start $SERVICE_NAME" ;; stop) kill `cat $SERVICE_DIR/$PID` rm -rf $SERVICE_DIR/$PID echo "#### stop $SERVICE_NAME" sleep 8 TEMP_PID=`ps -ef | grep -w "$SERVICE_NAME" | grep "java" | awk '{print $2}'` if [ "$TEMP_PID" == "" ]; then echo "#### $SERVICE_NAME process not exists or stop success" else echo "#### $SERVICE_NAME process pid is:$TEMP_PID" kill -9 $TEMP_PID fi ;; restart) $0 stop sleep 2 $0 start echo "#### restart $SERVICE_NAME" ;; esac exit 0
deploy.sh
#!/bin/sh ## java env export JAVA_HOME=/usr/java/jdk1.8.0_261-amd64 export JRE_HOME=$JAVA_HOME/jre ## exec shell name EXEC_SHELL_NAME=$1\.sh ## service name SERVICE_NAME=$1 SERVICE_DIR=/data JAR_NAME=$SERVICE_DIR/$SERVICE_NAME\.jar PID=pid/$SERVICE_NAME\.pid WORK_DIR=$SERVICE_DIR/work #function start start(){ cd $WORK_DIR if [ ! -d "log" ]; then mkdir log fi nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar $JAR_NAME --spring.profiles.active=test >log/$SERVICE_NAME.out 2>&1 & echo $! > $SERVICE_DIR/$PID echo "#### start $SERVICE_NAME" } # function stop stop(){ cd $WORK_DIR if [ -f "$SERVICE_DIR/$PID" ]; then kill `cat $SERVICE_DIR/$PID` rm -rf $SERVICE_DIR/$PID fi echo "#### stop $SERVICE_NAME" sleep 6 TEMP_PID=`ps -ef | grep -w "$SERVICE_NAME" | grep "java" | awk '{print $2}'` echo "#### TEMP_PID $TEMP_PID" if [ "$TEMP_PID" == "" ]; then echo "#### $SERVICE_NAME process not exists or stop success" else echo "#### $SERVICE_NAME process pid is:$TEMP_PID ." kill -9 $TEMP_PID fi } # function clean clean(){ cd $WORK_DIR if [ ! -d "lastDeploy" ]; then mkdir lastDeploy else rm lastDeploy/$SERVICE_NAME* fi if [ -f "$JAR_NAME" ]; then mv $JAR_NAME lastDeploy fi } case "$2" in start) start ;; stop) stop ;; restart) stop sleep 2 start echo "#### restart $SERVICE_NAME" ;; clean) stop sleep 2 clean echo "#### clean $SERVICE_NAME" ;; *) ## restart stop sleep 2 start ;; esac exit 0