post-receive in Windows---git hooks trigger Jenkins to build artifcat

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了post-receive in Windows---git hooks trigger Jenkins to build artifcat相关的知识,希望对你有一定的参考价值。

  如何在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 编译项目。

以上是关于post-receive in Windows---git hooks trigger Jenkins to build artifcat的主要内容,如果未能解决你的问题,请参考以下文章

python post-receive.py

sh 部署com post-receive

git服务器搭建post-receive 钩子部署服务端代码

编写一个 git post-receive 钩子来处理特定的分支

如何将Github Post-Receive WebHook限制为仅限主分支

linux 创建git 仓库