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%"workspaceuild-SAS.xml "%WORKSPACE%"uild.xml /y) else (copy "%JENKINS_HOME%"workspaceuild-test.xml "%WORKSPACE%"uild.xml /y)
if not exist "%WORKSPACE%"WebRootWEB-INFclasses mkdir "%WORKSPACE%"WebRootWEB-INFclasses
::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=WebRootWEB-INFclasses!
echo !str!|find "src">nul && set str=!str:src=WebRootWEB-INFclasses!
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:"	est" "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%"WebRootWEB-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 fileswinrar
cd "%WORKSPACE%"	empWebRoot
dir  /a-d /s /b>"%WORKSPACE%"list.xls
winrar a -r -m3 -ed WebRoot.zip .*

xcopy  "%JENKINS_HOME%"jobs\%JOB_NAME%uilds\%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
晚生不才,请多指教!
原文地址:https://www.cnblogs.com/lkc-test/p/10065669.html