Jenkins-java项目自动发布

path="${WORKSPACE}/git" # 创建目录 
if [ -d $path ]; 
then 
echo "The files is already exists " 
else 
mkdir -p $path 
fi

cd $path

path="${WORKSPACE}/git/bc-common" # 创建目录 
if [ -d $path ]; 
then 
echo "The files is already exists " 
else 
git clone  git@xxx:root/bc-common.git
fi

cd bc-common
pwd
git pull

cd BC-Comm-Basic
mvn clean package -Dmaven.test.skip=true
mvn install

cd ../BC-Comm-FeignEI
mvn clean package -Dmaven.test.skip=true
mvn install

path="${WORKSPACE}/git" # 创建目录 
if [ -d $path ]; 
then 
echo "The files is already exists " 
else 
mkdir -p $path 
fi

cd $path

path="${WORKSPACE}/git/bc-bas-configrepository" # 创建目录 
if [ -d $path ]; 
then 
echo "The files is already exists " 
else 
git clone git@xxx:root/bc-bas-configrepository.git
fi

cd $path
pwd
git pull
case $Status in 
	Deploy)
    	echo "Status:$Status" 
        path="${WORKSPACE}/bak/BC-BUS-PlatformServer/${BUILD_NUMBER}" # 创建每次要备份的目录 
        if [ -d $path ]; 
        then 
        	echo "The files is already exists " 
        else 
        	mkdir -p $path 
        fi 
        cp -f ${WORKSPACE}/BC-BUS-PlatformServer/target/*.jar $path # 将打包好的jar包备份到相应目录,覆盖已存在的目标 
        echo "Completing!" 
        ;; 
    Rollback) 
    	echo "Status:$Status" 
        echo "Version:$Version" 
        cd ${WORKSPACE}/bak/BC-BUS-PlatformServer/$Version # 进入备份目录 
        cp -f *.jar ${WORKSPACE}/BC-BUS-PlatformServer/target/ # 将备份拷贝到程序打包目录中,并覆盖之前的war包 
        ;; 
    *) 
    exit 
    	;; 
esac

  

ReservedNum=6 # 保留文件数 
FileDir=${WORKSPACE}/bak/BC-BUS-PlatformServer/ 
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 

 

原文地址:https://www.cnblogs.com/xiao-xue-di/p/12802584.html