安裝在Windows上的Jenkins實現任務發佈的回滾

參考:https://www.jianshu.com/p/af5fecaa8357windows

惋惜個人Jenkins是安裝在windows上面的,沒法直接使用bash腳本,只能用Windows的批處理指令dos指令bash

1)參數化構建過程:.net

2)配置備份腳本:code

要實現回滾,核心是要實現jar包的備份。在bash腳本中,這部分的邏輯是這麼寫的:blog

case $Status  in
  Deploy)
    echo "Status:$Status"
    path="${WORKSPACE}/bak/${BUILD_NUMBER}"      #建立每次要備份的目錄
    if [ -d $path ];
    then
        echo "The files is already  exists "
    else
        mkdir -p  $path
    fi
    \cp -f ${WORKSPACE}/target/*.war $path        #將打包好的war包備份到相應目錄,覆蓋已存在的目標
    echo "Completing!"
    ;;
  Rollback)
      echo "Status:$Status"
      echo "Version:$Version"
      cd ${WORKSPACE}/bak/$Version            #進入備份目錄
      \cp -f *.war ${WORKSPACE}/target/       #將備份拷貝到程序打包目錄中,並覆蓋以前的war包
      ;;
  *)
  exit
      ;;
esac

用Windows的批處理指令:get

set bakpath=D:\jenkinsbak\hangzhou
echo %bakpath%
if "%Status%" == "Deploy" goto deploy
if "%Status%" == "Rollback" goto rollback
:deploy
echo "Status:%Status%"
set path=%bakpath%\%BUILD_NUMBER% 
echo "the path is %path%"
if exist %path% (
echo "%path% exists"
) else (
cd %bakpath%
mkdir %BUILD_NUMBER%
echo "%path% created"
)
copy %WORKSPACE%\target\*.jar %path%
echo "Completing!"
exit
:rollback
echo "Status:%Status%"
echo "Version:%Version%"
cd %bakpath%\%Version%
copy *.jar %WORKSPACE%\target\
exit

dos指令要比bash麻煩得多,刪除多餘備份那部分我就懶得寫了,手工刪除吧。jenkins

PS:dos指令查詢某個文件夾下面的文件數量:it

dir /b . | find /v /c "::"
相關文章
相關標籤/搜索