码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-04-11 18:17:37      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:

  如何在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

 Step-by-step guide:

  1. 安装git Jenkins插件:Dashboard->Mange Jenkins->Manage Plugins

技术分享

 

  2. 配置git scm

技术分享  

3. 配置Jenkins触发模式为远程触发:设置Token,这个Token会在后面的post-receive 里面用到。

技术分享

 4. 把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%

5. 执行git commit push 命令,在Jenkins job查看build job。

 $ git commit -m "RUN_BUILD test git trigger Jenkins"

 $ git push origin master

 

至此当push code到git server时就会触发Jenkins 编译项目。

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

标签:

原文地址:http://www.cnblogs.com/AlanWalkOn/p/5302708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!