Jenkins版本回退
关于Jenkins回退,我的思路是在进行打包之后进行备份操作,之后在判断是否需要回退,这个步骤我觉得还是有优化的地方,我这个写法不太好。给大家看下
流水线 pipeline Declarative Pipeline语法
pipeline {
agent any
stages {
stage('pull code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'id', url: 'git@:jenkinstest/web-demo.git']]])
echo '拉取代码'
}
}
stage('sonar code'){
steps{
script {
// 引入sonar 工具
scannerHome = tool 'sonar-scanner'
}
//
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('package code') {
steps {
sh 'mvn clean package'
echo '打包代码'
}
}
stage('copy code') {
steps {
sh "/var/lib/jenkins/workspace/copycode.sh ${WORKSPACE} ${BUILD_NUMBER} "
}
}
stage('roback code') {
steps {
echo "Status:${Status}"
echo "version:${version}"
sh "/var/lib/jenkins/workspace/roback.sh ${WORKSPACE} ${Status} ${version} "
}
}
stage('publish code') {
steps {
deploy adapters: [tomcat8(credentialsId: '', path: '', url: 'http:///')], contextPath: null, war: 'target/*.war'
echo '上传代码1'
}
}
}
}
可以看出来我是在这里做了备份与回退判断
备份shell copycode.sh
#!/bin/bash
date
echo "Hello world"
workspace=$1
echo "打印工作空间 $workspace"
url=$1/bak/$2
echo "打印路径 ${url}"
if [ ! -d ${url} ];then
mkdir -p ${url}
echo "创建路径"
cp -f ${workspace}/target/*.war ${url}
echo "备份war包成功"
else
echo "文件夹已经存在-----"
fi
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
回退shell roback.sh
#!/bin/bash
workspace=$1
robackstatus=$2
version=$3
robackurl=$workspace/bak/$version
warurl=$workspace/target/
if [ "roback" != $robackstatus ];
then
echo "无回滚操作"
else
echo "执行回滚操作"
if [ ! -d $robackurl ];
then
echo "文件夹不存在"
else
echo "文件存在继续操作回滚"
echo "回滚地址 $robackurl"
echo "war包地址 $warurl"
rm -rf ${warurl}/*.war
cp -f ${robackurl}/*.war ${warurl}
echo "回滚完成"
fi
fi
我这个注释已经很完整了,别告诉我看不懂奥,兄弟萌
|