Automated Deployment -svn hook trigger a build

purpose

  Prior is in the form of polling, updated every two minutes svn, immediacy is not high, and now want direct trigger a build when submitting the code

Program

  The svn server hook, API requests Jenkins achieved when a code delivery construct

Implementation

  1, jenkins preparations

    User Management jenkins added Token, token needs to be preserved, the latter can not be viewed

    

    Build settings need to trigger the task, building check triggered remotely trigger construct, set up a custom token Token

    

  2、svn hook

    Here or use python to request jenkins, create a python file PostTouchJenkinsBuild.py

# ! / Usr / bin / Python 
# - * - Coding: UTF-. 8 - * - 

Import argparse
 Import OS
 Import Requests 

# define parameters 
Parser = argparse.ArgumentParser ()
 # depot_path 
parser.add_argument ( ' -s ' , ' - -repos ' , = STR type, default = None)
 # version 
parser.add_argument ( ' -v ' , ' --rev ' , = STR type, default = None)
 # branch 
parser.add_argument ( ' -b ', '--branch', type=str, default=None)
# Jenkins Job Token
parser.add_argument('-t', '--job_token', type=str, default=None)
# Jenkins Job名
parser.add_argument('-n', '--job_name', type=str, default=None)
args = parser.parse_args()
# 读取修改目录
svndirschanged = os.popen(
    '"C:\\Program Files (x86)\\VisualSVN Server\\bin\\svnlook.exe" dirs-changed -r {} {}' . .Format (args.rev, args.repos)) the readlines ()
 # determines whether or not the branch that contains modifications 
for changed in svndirschanged:
     IF changed.find ( " {} / " .format (args.branch))> = 0:
         # request jenkins jenkins address is constructed jenkinsurl deployed 
        Response = requests.get ( ' HTTP: // the root:? 113ce72325xxxxxxxx @ jenkinsurl / Job / {} / {} = buildWithParameters token ' .format (args.job_name, args.job_token )) 
     BREAK

    jenkins root for the user Id, 113ce72325xxxxxxxx is added above api token, other relevant parameters with the project, send it to come when called

    After submitting the trigger code, using post-commit, add the batch file post-commit.bat (windows server) in the corresponding hooks file svn repository project folder, you need to pay attention to coding task name in Chinese

The SET the REPOS = %. 1 
the SET REV = % 2 
"D: \ the Python \ Python38-32 \ python.exe" "D: \ the Python \ the Project \ SvnHookPython \ PostTouchJenkinsBuild.py" -s the REPOS%%% REV% -v -b trunk -t 11193a3f81441xxxxxxxxxxxxxxx -n LastOne- test area

    svn hook will pass two parameters when triggered, warehouses path and version number when submitting, after we get passed to obtain a modified python directory to determine changes on branches, only the corresponding branch is modified only build

    -s path to the repository

    -v version number

    -t task set custom token Token

    -b branch name

    -n jenkins task name

Guess you like

Origin www.cnblogs.com/sinianchangliu/p/11963399.html