Jenkins deploys SpringBoot and supports version rollback

Original address www.freesion.com

Environmental description:

Preparation conditions:

1. Create a new free style project

New task: tes-rollback
![][img-0]

2. Discard strategy

Insert image description here

3. Configure parameterized build process

3-1. Add option parameters

Check This project is parameterized -> Add parameter- > Choice Parameter

![][img-2]

Set parameters as shown below:

![][img-3]

3-2. Add character parameters

Add parameter- >Select String Prameter
![][img-4]

Set parameters as shown below:
![][img-5]

4. Source code management

4-1. Fill in the svn address of the project

Source code management -> Select Subversion -> Fill in the svn address of the project
Note : appending @HEAD after the svn address will pull the latest code each time

![][img-6]

4-2. Enter svn username and password

Insert image description here

![][img-8]

4-3. Select the configured user

![][img-9]

5-1. maven packaging configuration

Choose to call the top-level Maven goal
![][img-10]

Fill in the packaging command in the target: clean install -Dmaven.test.skip=true -Pdev
![][img-11]

Note: -Pdev is because my project distinguishes the environment, so I don’t need to add it.

5-2. Execute shell configuration (backup)

Add build step in build -> Execute shell
Insert image description here

Fill in the following script:

case $Status in
  Deploy)
    echo "Status:$Status"
	path="${WORKSPACE}/bak/${BUILD_NUMBER}"
	if [ -d $path ];
	then
		echo "The files is aready exists "
	else
		mkdir -p $path
	fi
	\cp -f ${WORKSPACE}/target/*.jar $path
	echo "Completing"
  ;;
  Rollback)
    echo "Status:$Status"
	echo "Version:$Version"
	cd ${WORKSPACE}/bak/$Version
	\cp -f *.jar $path ${WORKSPACE}/target
  ;;
  ;;
  *)
  exit
  ;;
esac

After this is released, it will be backed up as follows
![][img-14]

![][img-15]

5-3. Delete backup scripts regularly

Project backup cannot be unlimited, as this will quickly fill up the disk, so we must have a mechanism to clear old backups. A script is configured here, which will be executed once for each release to determine whether the number of backups exceeds 5. If so, delete the old backups and keep only the latest 5.
Add another shell execution step and fill in the following script. The number of backups can be modified to reduce or increase.

ReservedNum=5  
FileDir=${WORKSPACE}/bak/
date=$(date "+%Y%m%d-%H%M%S")

cd $FileDir   
FileNum=$(ls -l | grep '^d' | wc -l)   

while(( $FileNum > $ReservedNum))
do
    OldFile=$(ls -rt | head -1)         
    echo  $date "Delete File:"$OldFile
    rm -rf $FileDir/$OldFile
    let "FileNum--"
done

6. Deployment ( jenkins和应用在同一台服务器,直接部署即可)

Add a build-time shell script:

echo "==========定义常量=========="
APP_NAME=jenkins-demo.jar

echo "==========重命名 =========="
mv target/*.jar $APP_NAME

echo "==========  杀掉之前的进程 =========="
BUILD_ID=dontKillMe
PIDS=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}'` 
for pid in $PIDS;
 do kill -9 $pid;
 done
echo "========== 进程停止结束 =========="

tempPath=logs
if [ ! -d "$tempPath" ]; then
mkdir $tempPath
fi

#ps -ef | grep "$APP_NAME" | grep -v grep |awk "{print $2}" |xargs kill -9
echo "========== 开始启动 java 项目 =========="

#echo "********************************************************************"
#echo "****如果脚本使用nohup启动,则必须在他之前指定BUILD_ID(名字可以随便定义)**"
#echo "********************************************************************"
BUILD_ID=jenkins-demo
nohup java -jar $APP_NAME --server.port=81 > ./logs/nohup.log 2>&1 &

echo "==========项目启动成功,开始输出日志,请稍后 ……  =========="
sleep 3 
tail -n 800 ./logs/nohup.log 

7. Deployment-( jenkins和应用不在同一台服务器)

Add post-build actions

![][img-17]

![][img-18]

7. Project deployment server script

7-1. Write scripts

Execute the following commands on the server 192.168.122.4:

mkdir -p /root/Jenkins-in/
cd /root/Jenkins-in/
vi remit-channel.sh

The script content is as follows:

#!/bin/bash
DATE=$(date +%Y%m%d%H%M%S)
export JAVA_HOME PATH CLASSPATH
JAVA_HOME=/data/jdk1.8.0_261
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
DIR="/data/P4/p4-remit-channel"
JARFILE="p4-remit-channel-0.0.1-SNAPSHOT.jar"

if [ ! -d $DIR/backup ];then
   mkdir -p $DIR/backup
fi
cd $DIR

ps -ef | grep $JARFILE | grep -v grep | awk '{print $2}' | xargs kill -9
mv -f $JARFILE backup/$JARFILE$DATE
mv -f /root/Jenkins-in/$JARFILE .

java -jar  -Xms512m -Xmx512m -XX:MaxNewSize=256m -XX:MaxPermSize=256m $JARFILE > out.log &
if [ $? = 0 ];then
        sleep 30
        tail -n 50 out.log
fi

cd backup/
ls -lt|awk 'NR>5{print $NF}'|xargs rm -rf

7-2. Script executable authorization

chmod +x test-job.sh

  • Return to the main project interface and click Build with Parameters

![][img-19]

  • To publish, select Deploy—>Start building to start publishing.

![][img-20]

  • To rollback, select Rollback—> Enter the rollback version ----> Start building, and select an input version number from the build history

Insert image description here

1. Use Jenkins to automatically deploy the SpringBoot project: https://blog.csdn.net/zjh_746140129/article/details/80904876
2. Jenkins version rollback: https://www.jianshu.com/p/af5fecaa8357

Guess you like

Origin blog.csdn.net/caidingnu/article/details/129478166