先看下参数构建页面效果
说明:projectname是要部署的项目,SSH_Server是项目运行所在的服务器,GitBranch为分支,from_commitid为远程git仓库项目代码提交id的起始版本,to_commitid为截止版本,如下图,如果要等到红框提交版本的增量部署,那么起始版本为椭圆形的版本,截止版本为红框版本。
下面开始配置
1、设置参数:
2、从git拉取代码:
3、设置一下构建环境,每次构建前先清空工作空间:
4、构建前准备,包括复制build.xml的ant脚本,(下面附上脚本)
property="echo.path.compile"
refid="project.classpath">
includeantruntime="false" nowarn="false"
source="1.6" target="1.6" deprecation="true" debug="true"
encoding="UTF-8" classpathref="project.classpath"
>
webxml="${webRoot.dir}/WEB-INF/web.xml">
View Code
5、ant编译:
6、编译后获取增量class文件(附上代码):
#!/bin/bash
cd $WORKSPACE
projectname=ABC
stringName=compile_list.txt
stringName1=filelist.txt
src=WebRoot\/WEB-INF\/classes
scsspath=/scss/$projectname/webapps/$projectname/
#python loop.py -otf $stringName -ntf $stringName1
cp $stringName $stringName1
# 把清单中的属于配置文件的地址以及测试目录去掉,如xml和properties文件,test目录下的文件
sed -i '/./{/\.xml/d;/\.properties/d;/\.propertites/d;/\/test/d}' $stringName
# 把com目录下如果有xml文件,则加入到增量清单中
grep ^.*\/com\/.*\.xml.*$ $stringName1>>$stringName
# 把增量清单文件地址改为本地服务器文件地址
TheStart=$WORKSPACE/
sed -i "s#^#$TheStartg" $stringName
sed -i "/./{s/.java/.class/;s#src#$src#}" $stringName
#sed -i 's/.java/.class/g' $stringName
#sed -i 's#src#$src#g' $stringName
# 若果不是文件,为目录的行则删除掉
sed -i -e '/\./!d' $stringName
localdir=$WORKSPACE
tempdir=$localdir/temp
WebRoot=$tempdir/WebRoot/
cd $localdir
# 如果存在旧的temp目录,则删除掉
if [ -d "$tempdir" ];then
rm -rf $tempdir
echo 删除旧的temp目录
fi
mkdir $tempdir
#复制增量清单为一个临时文件tempfile.txt备用,后面用这个临时文件生成增量目录清单
#cp $stringName tempfile.txt
cp $stringName origindir.txt
#sed -i 's#$localdir#$tempdir#g' tempfile.txt
#遍历目录清单并按清单新建目录
echo 按目录清单新建目录并把增量文件复制到temp目录下
while read line
do
#把temp目录加在路径里
eval newfilepath=${line/$localdir/$tempdir}
#获取temp的文件路径,不包含文件名
eval newfiledir=`dirname $newfilepath`
if [ ! -d "$newfiledir" ];then
#新建temp目录下的文件路径
eval mkdir -p $newfiledir
echo 新建目录$newfiledir完成
fi
#以圆点分割去掉原文件路径中文件扩展名,并获取左边部分
eval originfilepath=${line%.*}
if [ -f $originfilepath.class ];then
eval cp $originfilepath*.class $newfiledir/
elif [ -f $originfilepath.xml ];then
eval cp $originfilepath.xml $newfiledir/
else
eval cp $originfilepath*.* $newfiledir/
fi
done
#生成增量清单
if [ -f $projectname\_$GitBranch.xls ];then
yes|rm $projectname\_$GitBranch.xls
fi
for files in `find $tempdir -name "*.*"`
do
echo $files >>$projectname\_$GitBranch.xls
done
echo 生成增量清单$projectname\_$GitBranch.xls
#把清单中每行的文件地址由本地地址变更为项目服务器地址
sed -i "s#$WebRoot#$scsspath#g" $projectname\_$GitBranch.xls
if [ -d "$WebRoot" ];then
cd $WebRoot
else
echo 增量文件复制到temp目录没有成功或版本$from_commitid到版本$to_commitid间根本就没有增量文件!!!
exit 1
fi
echo 进入$WebRoot目录打tar包
tar -cvf WebRoot.tar ./
cd $WORKSPACE
cp $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/log $WORKSPACE/
error=`cat log|grep ": Error"`
if [ ! -n "$error" ];then
echo 构建完成。
else
echo 构建出错!!!
exit 1
fi
View Code
7、构建后通过SSH把增量文件上传到服务器,并解压,重启服务
cd /scss/$projectname/webapps
compiledir=$projectname
if [ "$projectname" == "sds" ];then
compiledir=sds
fi
if [ "$projectname" == "ATFE" ];then
compiledir=TFE
fi
if [ $firsttime == Y ];then
tar zcvf $compiledir.tar.gz $compiledir
fi
cp /scss/$projectname/webapps/temp/WebRoot/WebRoot.tar WebRoot.tar
rm -rf temp
tar -xvf WebRoot.tar -C $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
配置完成,就可以构建了。
还有需要把下面这些文件放到Linux的ant/lib目录下:
这些安装了eclipse的话都会有的,去eclipse下面找
标签:Git,grep,Tomcat,projectname,echo,WebRoot,Jenkins,stringName,目录
来源: https://www.cnblogs.com/lkc-test/p/11891563.html