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

Tom Rochette, 2016-05-01 01:48
Add more detail about "your service key"

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 15 Etienne Massip
Since of version 0.9.0, you can use an HTTP GET submission 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 16 Mohit Sindhwani
*Notice:* 
13 18 Etienne Massip
* Instead of calling an url, you can use @ruby /path_to_redmine/redmine/script/rails runner "Repository.fetch_changesets" -e production > /dev/null 2>&1 &@.
14 16 Mohit Sindhwani
* Note also that project identifier can either be the number of the project, e.g. @id=1@ or the identifier name that you gave the project, e.g., @id=mobileapp@
15 13 C. X.
16 1 Etienne Massip
See #2925 for original feature request.
17 1 Etienne Massip
18 1 Etienne Massip
h2. Step 1 : configure Redmine to accept the request
19 1 Etienne Massip
20 29 Tom Rochette
Web service for repositories must by activated in the Administration menu (Administration - Settings - Repositories - Enable WS for repository management) and the generated API key (referred to "your service key" in the following documentation) will have to be used by the caller in Step 2.
21 29 Tom Rochette
22 1 Etienne Massip
23 1 Etienne Massip
h2. Step 2 : setup a post-commit script on the SCM server
24 1 Etienne Massip
25 1 Etienne Massip
You have to setup a post-commit script which will call the previous URL.
26 1 Etienne Massip
27 1 Etienne Massip
h3. Subversion
28 1 Etienne Massip
29 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 :
30 8 Etienne Massip
<pre><code class="sh">
31 1 Etienne Massip
#!/bin/sh
32 1 Etienne Massip
33 6 Jürgen Hörmann
curl "http://<redmine url>/sys/fetch_changesets?key=<your service key>"
34 8 Etienne Massip
</code></pre>
35 12 razumuhin map
or if you want to use wget 
36 12 razumuhin map
<pre><code class="sh">
37 19 Etienne Massip
wget "http://<redmine url>/sys/fetch_changesets?key=<your service key>"
38 12 razumuhin map
</code></pre>
39 12 razumuhin map
Note: Don`t forget wget in your computer path.
40 11 razumuhin map
41 1 Etienne Massip
Or, on a Windows system (2 files) :
42 1 Etienne Massip
43 8 Etienne Massip
* @post-commit.cmd@ :
44 8 Etienne Massip
<pre><code class="cmd">
45 2 Etienne Massip
cscript "%~dp0refresh_redmine.vbs" //Nologo >> "%~dp0refresh_redmine.log" 2>&1
46 8 Etienne Massip
</code></pre>
47 1 Etienne Massip
48 8 Etienne Massip
* @refresh_redmine.vbs@ :
49 8 Etienne Massip
<pre><code class="vbs">
50 1 Etienne Massip
private const REDMINE_SERVICE_KEY = "<your service key>"
51 1 Etienne Massip
52 15 Etienne Massip
Call HTTPGet("http://<redmine url>/sys/fetch_changesets?key=" & REDMINE_SERVICE_KEY)
53 1 Etienne Massip
54 15 Etienne Massip
Private Function HTTPGet(sUrl)
55 15 Etienne Massip
  Dim oHTTP
56 1 Etienne Massip
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
57 15 Etienne Massip
  oHTTP.open "GET", sUrl, False
58 15 Etienne Massip
  oHTTP.send
59 15 Etienne Massip
  HTTPGet = oHTTP.responseText
60 1 Etienne Massip
End Function
61 8 Etienne Massip
</code></pre>
62 17 Ruslan Khasanov
or for https
63 17 Ruslan Khasanov
<pre><code class="vbs">
64 17 Ruslan Khasanov
private const REDMINE_SERVICE_KEY = "<your service key>"
65 17 Ruslan Khasanov
66 17 Ruslan Khasanov
Call HTTPGet("https://<redmine url>/sys/fetch_changesets?id=<your project id>&key=" & REDMINE_SERVICE_KEY)
67 17 Ruslan Khasanov
68 17 Ruslan Khasanov
Private Function HTTPGet(sUrl)
69 17 Ruslan Khasanov
  Dim oHTTP
70 17 Ruslan Khasanov
  set oHTTP = CreateObject("MSXML2.ServerXMLHTTP")
71 17 Ruslan Khasanov
  oHTTP.open "GET", sUrl, False
72 17 Ruslan Khasanov
  oHTTP.setOption 2, 13056
73 17 Ruslan Khasanov
  oHTTP.send
74 17 Ruslan Khasanov
  HTTPGet = oHTTP.responseText
75 17 Ruslan Khasanov
End Function
76 17 Ruslan Khasanov
</code></pre>
77 9 András Veres-Szentkirályi
78 26 Vincent Lizzi
79 27 Vincent Lizzi
Or, on a Windows system:
80 1 Etienne Massip
81 27 Vincent Lizzi
If Ruby is on your system path, simply create a @post-commit.bat@ script file in the hooks sub-directory of the repository:
82 27 Vincent Lizzi
83 27 Vincent Lizzi
<pre><code class="bat">
84 27 Vincent Lizzi
ruby -e "require 'open-uri'; open('http://<redmine url>/sys/fetch_changesets?key=<your service key>').read;"
85 27 Vincent Lizzi
</code></pre>
86 27 Vincent Lizzi
87 27 Vincent Lizzi
If Perl is installed, simply create a @post-commit.bat@ script file in the hooks sub-directory of the repository:
88 26 Vincent Lizzi
89 26 Vincent Lizzi
<pre><code class="bat">
90 26 Vincent Lizzi
perl -mLWP::Simple -e "$url = 'http://<redmine url>/sys/fetch_changesets?key=<your service key>'; LWP::Simple::get($url);"
91 26 Vincent Lizzi
</code></pre>
92 26 Vincent Lizzi
93 26 Vincent Lizzi
94 26 Vincent Lizzi
95 9 András Veres-Szentkirályi
h3. Git
96 9 András Veres-Szentkirályi
97 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:
98 9 András Veres-Szentkirályi
99 9 András Veres-Szentkirályi
<pre><code class="sh">
100 9 András Veres-Szentkirályi
#!/bin/sh
101 9 András Veres-Szentkirályi
102 9 András Veres-Szentkirályi
curl "http://<redmine url>/sys/fetch_changesets?key=<your service key>"
103 9 András Veres-Szentkirályi
</code></pre>
104 9 András Veres-Szentkirályi
105 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.
106 9 András Veres-Szentkirályi
107 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.
108 14 Hadrien KOHL
109 25 Mindaugas Kairys
In case a redmine project is configured to work with a mirror repository that resides on the same host as the origin git repository, you can also use a @post-receive@ hook to update that redmine mirror repository, so the whole script then could be:
110 21 Mindaugas Kairys
111 21 Mindaugas Kairys
<pre><code class="sh">
112 21 Mindaugas Kairys
#!/bin/sh
113 21 Mindaugas Kairys
114 25 Mindaugas Kairys
cd /<path_to_origin_git_repo>/<your_git_prj.git>
115 22 Mindaugas Kairys
git push /<redmine_path>/<git_repo>/<your_git_prj.git> master
116 21 Mindaugas Kairys
curl "http://<redmine url>/sys/fetch_changesets?key=<your service key>"
117 1 Etienne Massip
</code></pre>
118 23 Mindaugas Kairys
119 24 Mindaugas Kairys
* this will also eliminate the need of a mirror synchronization cron script and will help to keep redmine's project repository in sync just after every git push event
120 21 Mindaugas Kairys
121 14 Hadrien KOHL
h3. Only update relevant project
122 14 Hadrien KOHL
123 20 Adrien Crivelli
/sys/fetch_changesets accepts id parameter, so you can limit the refresh to the correct project : 
124 14 Hadrien KOHL
125 14 Hadrien KOHL
<pre><code class="sh">
126 14 Hadrien KOHL
#!/bin/bash
127 14 Hadrien KOHL
128 14 Hadrien KOHL
_apikey=XXXXXXXXXXXXXXXXXXXX
129 14 Hadrien KOHL
130 14 Hadrien KOHL
_projectid=${PWD##*/}
131 14 Hadrien KOHL
_projectid=${_projectid%*.git}
132 14 Hadrien KOHL
133 14 Hadrien KOHL
curl "http://<redmine url>/sys/fetch_changesets?key=$_apikey&id=$_projectid"&
134 14 Hadrien KOHL
</code></pre>
135 28 Lukas Pirl
136 28 Lukas Pirl
h3. Only update relevant repository
137 28 Lukas Pirl
138 28 Lukas Pirl
If you can install commit hooks on your repository server, you can use "this":http://www.redmine.org/plugins/redmine_scm_hookhelpers plugin to fetch commits for a certain repository only.