HowTo setup automatic refresh of repositories in Redmine on commit » History » Version 14

Hadrien KOHL, 2011-09-28 11:50
Adding relevant update

1 1 Etienne Massip
h1. HowTo setup automatic refresh of repositories in Redmine on commit
2 4 Mischa The Evil
3 4 Mischa The Evil
{{>toc}}
4 1 Etienne Massip
5 3 Etienne Massip
Since of version 0.9.0, you can use an HTTP submission, either GET or POST, to automatically refresh Redmine after you committed your modification in your repository.
6 1 Etienne Massip
7 10 Maxim Strukov
Examples:
8 1 Etienne Massip
9 1 Etienne Massip
* _/sys/fetch_changesets?key=<your service key>_ fetches changesets for all active projects
10 7 Felix Schäfer
* _/sys/fetch_changesets?id=<project identifier>&key=<your service key>_ fetches changesets from the repository of a specific project
11 1 Etienne Massip
12 13 C. X.
*Notice:* Instead of calling an url, you can use @ruby /path_to_redmine/redmine/script/runner "Repository.fetch_changesets" -e production > /dev/null 2>&1 &@.
13 13 C. X.
14 1 Etienne Massip
See #2925 for original feature request.
15 1 Etienne Massip
16 1 Etienne Massip
h2. Step 1 : configure Redmine to accept the request
17 1 Etienne Massip
18 1 Etienne Massip
Web service for repositories must by activated in the Administration menu and the generated key will have to be used by the caller in Step 2.
19 1 Etienne Massip
20 1 Etienne Massip
h2. Step 2 : setup a post-commit script on the SCM server
21 1 Etienne Massip
22 1 Etienne Massip
You have to setup a post-commit script which will call the previous URL.
23 1 Etienne Massip
24 1 Etienne Massip
h3. Subversion
25 1 Etienne Massip
26 8 Etienne Massip
Simply add a @post-commit@ (or @post-commit.cmd@ on a Windows system) script file in the hooks sub-directory which contains the HTTP request call :
27 8 Etienne Massip
<pre><code class="sh">
28 1 Etienne Massip
#!/bin/sh
29 1 Etienne Massip
30 6 Jürgen Hörmann
curl "http://<redmine url>/sys/fetch_changesets?key=<your service key>"
31 8 Etienne Massip
</code></pre>
32 12 razumuhin map
or if you want to use wget 
33 12 razumuhin map
<pre><code class="sh">
34 12 razumuhin map
wget "http://your/redmine/path/sys/fetch_changesets?key=<API KEY>"
35 12 razumuhin map
</code></pre>
36 12 razumuhin map
Note: Don`t forget wget in your computer path.
37 11 razumuhin map
38 1 Etienne Massip
Or, on a Windows system (2 files) :
39 1 Etienne Massip
40 8 Etienne Massip
* @post-commit.cmd@ :
41 8 Etienne Massip
<pre><code class="cmd">
42 2 Etienne Massip
cscript "%~dp0refresh_redmine.vbs" //Nologo >> "%~dp0refresh_redmine.log" 2>&1
43 8 Etienne Massip
</code></pre>
44 1 Etienne Massip
45 8 Etienne Massip
* @refresh_redmine.vbs@ :
46 8 Etienne Massip
<pre><code class="vbs">
47 1 Etienne Massip
private const REDMINE_SERVICE_KEY = "<your service key>"
48 1 Etienne Massip
49 1 Etienne Massip
Call HTTPPost("http://<redmine url>/sys/fetch_changesets", "key=" & REDMINE_SERVICE_KEY)
50 1 Etienne Massip
51 1 Etienne Massip
Private Function HTTPPost(sUrl, sRequest)
52 1 Etienne Massip
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
53 1 Etienne Massip
  oHTTP.open "POST", sUrl,false
54 1 Etienne Massip
  oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
55 1 Etienne Massip
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
56 1 Etienne Massip
  oHTTP.send sRequest
57 1 Etienne Massip
  HTTPPost = oHTTP.responseText
58 1 Etienne Massip
End Function
59 8 Etienne Massip
</code></pre>
60 9 András Veres-Szentkirályi
61 9 András Veres-Szentkirályi
h3. Git
62 9 András Veres-Szentkirályi
63 9 András Veres-Szentkirályi
Simply add a @post-receive@ (even on a Windows system, no extension is required) script file in the hooks sub-directory which contains the HTTP request call:
64 9 András Veres-Szentkirályi
65 9 András Veres-Szentkirályi
<pre><code class="sh">
66 9 András Veres-Szentkirályi
#!/bin/sh
67 9 András Veres-Szentkirályi
68 9 András Veres-Szentkirályi
curl "http://<redmine url>/sys/fetch_changesets?key=<your service key>"
69 9 András Veres-Szentkirályi
</code></pre>
70 9 András Veres-Szentkirályi
71 9 András Veres-Szentkirályi
This setup works in the "usual" case, where Redmine's repository is set to a bare Git repository, so no commits can happen directly in there. If you have a rare setup with a non-bare repository linked to Redmine, you need to add the script as a @post-commit@ hook as well.
72 9 András Veres-Szentkirályi
73 9 András Veres-Szentkirályi
Don't forget to make the file(s) executable on UNIXish systems, more information about Git hooks can be found in the "githooks":http://www.kernel.org/pub/software/scm/git/docs/githooks.html man page.
74 14 Hadrien KOHL
75 14 Hadrien KOHL
h3. Only update relevant project
76 14 Hadrien KOHL
77 14 Hadrien KOHL
/sys/fetch_changesets accepts id parameter, so you can limit the refrech to the correct project : 
78 14 Hadrien KOHL
79 14 Hadrien KOHL
<pre><code class="sh">
80 14 Hadrien KOHL
#!/bin/bash
81 14 Hadrien KOHL
82 14 Hadrien KOHL
_apikey=XXXXXXXXXXXXXXXXXXXX
83 14 Hadrien KOHL
84 14 Hadrien KOHL
_projectid=${PWD##*/}
85 14 Hadrien KOHL
_projectid=${_projectid%*.git}
86 14 Hadrien KOHL
87 14 Hadrien KOHL
curl "http://<redmine url>/sys/fetch_changesets?key=$_apikey&id=$_projectid"&
88 14 Hadrien KOHL
</code></pre>