如何在Windows上编写post-receive,当git用户push文件到git server时。检查commit message,如果包含RUN_BUILD就调用Jenkins build API编译项目。
环境设置:
1. 安装Jenkins ---流行的代码管理工具
选择Remote trigger scripts 在Jenkins Job build 配置项。
2. 安装scm-manager ---同时支持svn,git...
3. 安装git-client---需要用到git command
4. 安装wget command---访问URL in command
把post-receive.bat放在git hooks目录下。
1 @ echo off 2 set CI_JOB_NAME=CI-Test 3 set JENKINS_BUILD_API=http://localhost:8080/job/%CI_JOB_NAME%/build?token=BUILD //set yourself token value in jenkins config page. 4 ::read post-receive stdin parmameters 5 set /p OLDREV_NEWREV_REFNAME= 6 set OLDREV=%OLDREV_NEWREV_REFNAME:~0,40% 7 set NEWREV=%OLDREV_NEWREV_REFNAME:~41,40% 8 set REFNAME=%OLDREV_NEWREV_REFNAME:~82,999% 9 10 11 set MESSAGE_FILE_TEMP=%CI_JOB_NAME%_COMMIT_MSG_TEMP.txt 12 ::Get commit message and retrive RUN_BUILD snippt 13 git log --pretty^=format:%%s %OLDREV%..%NEWREV% >%MESSAGE_FILE_TEMP% 14 15 16 findstr /c:RUN_BUILD %MESSAGE_FILE_TEMP% 17 if %ERRORLEVEL% == 0 ( 18 "C:\Program Files (x86)\GnuWin32\bin\wget.exe" ^ 19 --header="Content-Type:text/plain;charset=UTF-8" ^ 20 --output-document="-" ^ 21 --timeout=2 ^ 22 --auth-no-challenge ^ 23 --user=Jenkins User ^ 24 --password=Jenkins password^ 25 %JENKINS_BUILD_API% >NUL 2>$1) 26 del %MESSAGE_FILE_TEMP%
当push code到git server时就会触发Jenkins 编译项目。
时间: 2024-10-24 19:58:41