zoukankan      html  css  js  c++  java
  • 实现Jenkins+svn+bat批处理构建svn版本差异增量的自动化打zip包

      最近进到的公司,因为我之前做过自动化测试,所以要求我写一个从svn上面的各个目录拉取需要的代码,集合在一起打zip包。想了想,觉得用批处理+Jenkins持续集成工具来操作应该是可行的,于是就开始着手去做了,还真的是有些事情你去想了然后大胆去做了就会能实现。下面是我的实现过程:

    1、首先你得安装Jenkins,安装Jenkins就不多说了,网上一搜一大把;

    2、安装好了就构建自由风格的任务,进入配置页面

    2.1、选择参数化构建过程,根据自己需要选择相应的参数类型

    2.2、把svn相应的目录作为参数写入配置中

    3、把需要的参数写好后在构建的步骤选择Execute Windows batch command,如果选项中没有的自己去系统管理--》管理插件--》可安装插件中搜索Execute Windows batch command安装

    4、这个步骤也是最重要的部分就是写批处理代码,写入刚刚选择的构建步骤中,下面是我的代码:

    代码部分:

    ::disk是我上面设置的参数,是指打的包将要存放在哪个磁盘,Jenkins构建使用的目录
    set Jenkins=%disk%:Jenkins
    ::svnupdate是构建使用目录,因为我本机有全量自动化打包的目录,为了区分就建了这个目录 
    set svnupdate=%Jenkins%svnupdate
    ::temp是每次构建使用的临时目录
    set tempdir=%svnupdate%temp
    ::sources目录是作为构建后打包的备份目录
    set sources=%Jenkins%sources 
    ::版本起始-1是因为下面使用svn命令行比较差异文件时是不包括开始版本的,-1之后的参数放到svn比较差异文件的命令里面,参数就不用往前挪版本,开发给什么版本到什么版本直接填入参数就好
    set /a from_version_up=%from_version%-1
    if not exist %Jenkins% mkdir %Jenkins%
    if not exist %sources% mkdir %sources%
    if not exist %svnupdate% mkdir %svnupdate%
    if exist %tempdir% rd /s /Q %tempdir%
    if exist %svnupdate% del /s /q %svnupdate%
    if %type%==APP set folder=%city%/HTML/sources/%APPbranch%
    
    if %type%==DT (if %DLL%==old (set folder=%DLL%)else (if %DLL%==aa (set folder=%DLL%/%city%/test)else ( if %DLL%==差异驱动和配置文件 (set folder=%DLL%/%差异驱动和配置文件%)else ( set folder=%DLL%/test))))
    if %DLL%==差异驱动和配置文件 set "folder=%folder: =%"
    if %type%==NJW (if %NJW%==EXPJS (set folder=%NJW%/trunk/n) else (set folder=%NJW%/nw))
    set url=http://IP/svn/%type%/%folder%
    rem 把差异清单导入到txt文件中
    set stringName=%svnupdate%file_list.txt
    set stringName1=%svnupdate%filelist.txt
    svn log -r %to_version%:%from_version% %url% >%svnupdate%updatelog.txt
    svn diff -r %from_version_up%:%to_version% --summarize %url% > %stringName% --username svn账号 --password svn密码
    ::如果svn有中文目录,运行python的转码文件来识别,下面给出loop.py文件
    python C:loop.py %stringName% %stringName1% 
    rem 导出差异文件
    for /f "delims=" %%i in (%stringName1%) do (
    
    if '%%i'=='' goto end
    call :export %%i
    
    )
    for /f "delims=" %%i in (%svnupdate%updatelog.txt) do (
    for /f "tokens=1,2,3,4 delims=|" %%a in ("%%i") do (set a1=%%a && echo %%a|find "r">nul && goto next
    )
    )
    :next
    for /f "tokens=1,2 delims=r" %%a in ("%a1%") do (set a1=%%a)
    set "a1=%a1: =%"
    set path=%path%;c:program fileswinrar
    %disk%:
    cd %svnupdate%
    if %type%==APP mkdir %svnupdate%app
    ::设置打包的名称带上时间
    set update=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%
    set update=%update: =0%
    
    if %type%==APP set updatezip=app_%city%_%from_version%-%a1%_%update%.zip
    if %type%==DLL (if %DLL%==aa (set updatezip=%DLL%_%city%_%from_version%-%a1%_%update%.zip) else (if %DLL%==差异驱动和配置文件 (set updatezip=%DLL%_%差异驱动和配置文件%_%from_version%-%a1%_%update%.zip) else (set updatezip=%DLL%_%from_version%-%a1%_%update%.zip)))
    if %type%==NJW set updatezip=%NJW%_%from_version%-%a1%_%update%.zip
    mkdir %update%
    if %type%==APP (xcopy /s/e/y %tempdir% %svnupdate%app) else (xcopy /s/e/y %tempdir% %svnupdate%\%update%)
    if %type% NEQ APP cd %svnupdate%%update%
    if %type%==APP (winrar a -r -m3 -ed %updatezip% .app*) else (winrar a -r -m3 -ed %updatezip% * && xcopy /s/e/y %updatezip% %svnupdate% && cd %svnupdate%)
    
    if %type%==APP (rd /s /Q app)
    rd /s /Q %update%
    
    xcopy /s/y %svnupdate%\%updatezip% %sources%
    ::把备份的zip按时间顺序列入到txt文件中
    dir %sources%*.zip /O:-D >%sources%file.txt
    %disk%:
    cd %sources%
    ::删除按时间排序后多余20个的zip文件
    for /F "skip=25 tokens=4" %%a in (%sources%file.txt) do @if exist %%a del %%a
    :end
    exit 0
    :export
    set fullpath=%2
    
     
    
    if %fullpath%==%url% goto :EOF
    if %fullpath%==%url%/DLL goto :EOF
    if %fullpath%==%url%/dll goto :EOF
    
    set filename=%~nx2
    call set "filepath=%%fullpath:%filename%=%%"
    call set "filepath=%%filepath:%url%=%%"
    call set "filepath=%%filepath:/=\%%"
    if not exist %tempdir% mkdir %tempdir%
    set tempfilepath=%tempdir%%filepath%
    echo "%tempfilepath%"|find "DLL">nul&& call set tempfilepath=%%tempfilepath:DLL=dll%%
    
    if not exist "%tempfilepath%" mkdir "%tempfilepath%" 
    svn export -r %to_version% %fullpath% %tempfilepath%%filename% --force
    goto :EOF

    (要求装有python,下面代码是python2.7写的)下面是loop.py文件代码:

    #coding=utf-8
    import sys
    import urllib
    import codecs
    a = sys.argv[1] #批处理输入参数1
    b = sys.argv[2] #批处理输入参数2
    
    f=open(a,"r")
    line=f.readline()
    fx = codecs.open(b, 'wb+', 'gb2312')
    while line:
    url = urllib.unquote(line)+"
    "
    content = unicode(url,"utf-8")
    
    fx.write(content)
    line = f.readline()
    fx.close()
    f.close() 

     这样一个增量自动化打包就完成了,代码肯定有改进的地方,有大神看到请指出

    晚生不才,请多指教!
  • 相关阅读:
    echarts图形的位置布局如何调整
    IDEA在写springboot项目的时候遇见的问题:Caused by: java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResol
    【学习总结】linux命令:ps -ef | grep xxxserver | grep -v grep|wc -l
    linux中管道符“|”的作用 find的作用
    linux系统中的管道“|”命令的作用
    JDBC管理事务
    SQL注入的问题&使用PreparedStatement对象防止SQL注入
    JDBC对象详解DriverManager&Connection&Statement
    MySQL安装&使用DataGrip连接数据库
    红黑树(R-B树,对称二叉B树)
  • 原文地址:https://www.cnblogs.com/lkc-test/p/8675992.html
Copyright © 2011-2022 走看看