Jenkins+GIT增量部署Tomcat項目(windows環境的Jenkins)


最近有svn版本管理工具切換到git,自動化部署需要修改為從git拉取代碼,下面是修改的步驟:

首先展示一下構建頁面的效果:

 

看完構建頁面的效果了,下面來看配置:

1、參數設置(包含projectname參數關聯type參數):

包含projectname參數關聯type參數

SSHServer參數關聯projectname參數

設置其他參數

2、源碼拉取:

3、構建環境:

4、構建步驟:

4.1、把需要的ant編譯的build.xml(build.xml的代碼上一篇有給出過,這里就不貼出來了)復制到workspace里面

if "%firsttime%"=="--select--" exit 1
if %projectname%==SAS (copy "%JENKINS_HOME%"\workspace\build-SAS.xml "%WORKSPACE%"\build.xml /y) else (copy "%JENKINS_HOME%"\workspace\build-test.xml "%WORKSPACE%"\build.xml /y)
if not exist "%WORKSPACE%"\WebRoot\WEB-INF\classes mkdir "%WORKSPACE%"\WebRoot\WEB-INF\classes
::if not exist "%WORKSPACE%"\update mkdir "%WORKSPACE%"\update

4.2、把git增量文件作為清單列入文本

#\!/bin/bash
cd "$WORKSPACE"
git checkout $GitBranch
git diff $from_commitid $to_commitid --name-only>compile_list.txt

4.3、遍歷工作目錄按清單文本(代碼)

set stringName="%WORKSPACE%"\compile_list.txt
set stringName1="%WORKSPACE%"\filelist.txt
cd "%WORKSPACE%"
python C:\loop.py compile_list.txt filelist.txt

@echo off
SET TheStart="%WORKSPACE%"\
(FOR /f "delims=" %%i IN (filelist.txt) DO (
ECHO %TheStart%%%i
))>New_list.txt

@echo off
(for /f "delims=" %%i in (New_list.txt) do (
set str=%%i
SetLocal EnableDelayedExpansion
set str=!str:/=\!
echo !str!|find ".java">nul && set str=!str:.java=.class! && set str=!str:src=WebRoot\WEB-INF\classes!
echo !str!|find "\src">nul && set str=!str:src=WebRoot\WEB-INF\classes!
echo !str!
EndLocal
))>newlist.txt && echo running 

rem 把配置文件去掉xml和properties文件
(for /f "delims=" %%a in ('findstr /i /v /c:".properties" "newlist.txt"') do (
SetLocal EnableDelayedExpansion
echo %%a
EndLocal
))>compile_list.txt
(for /f "delims=" %%a in ('findstr /i /v /c:"\test" "compile_list.txt"') do (
SetLocal EnableDelayedExpansion
echo %%a
EndLocal
))>compile_list0.txt
(for /f "delims=" %%a in ('findstr /i /v /c:".xml" "compile_list0.txt"') do (
SetLocal EnableDelayedExpansion
echo %%a
EndLocal
))>compile1.txt
(for /f "delims=" %%a in ('findstr /i /v /c:".propertites" "compile1.txt"') do (
SetLocal EnableDelayedExpansion
echo %%a
EndLocal
))>compile2.txt
@echo on
if "%projectname%" == "ADS" for /f "delims=" %%a in ('findstr /r "wtsd\\ads.*\.xml" "newlist.txt"') do (
SetLocal EnableDelayedExpansion
echo %%a>>compile2.txt
EndLocal
)
move /y compile2.txt compile_list.txt

set tempdir="%WORKSPACE%"\temp
set localdir="%WORKSPACE%"
set classesdir="%WORKSPACE%"\WebRoot\WEB-INF
cd %localdir%
if exist %tempdir% rd /s /Q %tempdir%

for /f "delims=" %%i in (compile_list.txt) do (
     
     if "%%i"=="" goto end
     set fullpath=%%i
     call :export %%i

)
set path=%path%;c:\program files\winrar
cd "%WORKSPACE%"\temp\WebRoot
dir  /a-d /s /b>"%WORKSPACE%"\list.xls
winrar a -r -m3 -ed WebRoot.zip .\*

xcopy  "%JENKINS_HOME%"\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\log "%WORKSPACE%"\ /s/e/y
findstr /c:"svn: E" "%WORKSPACE%"\log >nul 2>nul
if %ERRORLEVEL%==0 exit 1
findstr /c:"不包含文件" "%WORKSPACE%"\log >nul 2>nul
if %ERRORLEVEL%==0 goto end
::findstr /c:"系統找不到" "%WORKSPACE%"\log >nul 2>nul
::if %ERRORLEVEL%==0 exit 1

:end
exit 0
:export

cd %classesdir%
set filename=%~nx1
set fileonlyname=%~n1
call set "filepath=%%fullpath:%filename%=%%"
call set "filepath=%%filepath:%localdir%=%%"
if not exist %tempdir% mkdir %tempdir%
set localfilepath=%localdir%%filepath%
set tempfilepath=%tempdir%%filepath%
if not exist %tempfilepath% mkdir %tempfilepath%
set originfile=%localfilepath%%filename%
set tempfile=%tempfilepath%%filename%
call set "originfile=%originfile:\ =\%"
call set "tempfile=%tempfile:\ =\%"
copy %originfile% %tempfile% /y
@echo off
echo %localfilepath%%filename%|find /i "." >nul && echo 包含文件 || echo 不包含文件
@echo on
cd %localfilepath%
for /f  "tokens=*" %%a in ('dir /s/b/a-d "*.class"') do (
echo %%~fa|find "%fileonlyname%$">nul && xcopy "%%a" %tempfilepath%
)
goto :EOF

5、構建后步驟,把編譯好的增量文件上傳到服務器,並重啟項目:

代碼:

cd /scss/$projectname/webapps
compiledir=$projectname
if [ "$projectname" == "www" ];then
 compiledir=ww
fi
if [ "$projectname" == "err" ];then
 compiledir=we
fi
if [ "$projectname" == "werer" ];then
 compiledir=wer
fi
if [ $firsttime == Y ];then
tar zcvf $compiledir.tar.gz $compiledir
fi
cp /scss/$projectname/webapps/temp/WebRoot/WebRoot.zip WebRoot.zip
rm -rf temp
unzip -o WebRoot.zip -d $compiledir

pid=`ps aux|grep java|grep $projectname|grep -v grep|awk '{print $2}'`
if [ "${pid}" ]; then
    kill -9 $pid
else
   echo "server is not started,can startup"  
fi
sh /scss/$projectname/bin/startup.sh
sleep 10
pid=`ps aux|grep java|grep $projectname|grep -v grep|awk '{print $2}'`
if [ "${pid}" ]; then
    echo "restart ok,server is running and pid=$pid"
else
   echo "restart fail,server is not started"  
    exit 1
fi

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM