Original address www.freesion.com
Environmental description:
- Technology stack: svn + maven + jdk1.8 + springboot + Jenkins + linux shell
- Jenkins 插件:Subversion + Maven Integration plugin + Publish over SSH
- The host where Jenkins is located: 192.168.122.5
- Project deployment server: 192.168.122.4
Preparation conditions:
- Jenkins series tutorial one: Deploying Jenkins on Linux
- Jenkins series tutorial four: Configuring jdk and maven
- Jenkins series tutorial five: Jenkins installs the Maven project plugin: Maven Integration plugin
- Jenkins series tutorial six: Install and configure the Publish over SSH plug-in in Jenkins
- Jenkins series tutorial seven: Install and configure the svn plug-in for Jenkins
1. Create a new free style project
New task: tes-rollback
2. Discard strategy
3. Configure parameterized build process
3-1. Add option parameters
Check This project is parameterized -> Add parameter- > Choice Parameter
Set parameters as shown below:
3-2. Add character parameters
Add parameter- >Select String Prameter
Set parameters as shown below:
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
4-2. Enter svn username and password
4-3. Select the configured user
5-1. maven packaging configuration
Choose to call the top-level Maven goal
Fill in the packaging command in the target: clean install -Dmaven.test.skip=true -Pdev
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
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
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
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
- To publish, select Deploy—>Start building to start publishing.
- To rollback, select Rollback—> Enter the rollback version ----> Start building, and select an input version number from the build history
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