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

Version 14 (Hadrien KOHL, 2011-09-28 11:50) → Version 15/29 (Etienne Massip, 2012-03-19 09:23)

h1. HowTo setup automatic refresh of repositories in Redmine on commit

{{>toc}}

Since of version 0.9.0, you can use an HTTP submission, either GET submission or POST, to automatically refresh Redmine after you committed your modification in your repository.

Examples:

* _/sys/fetch_changesets?key=<your service key>_ fetches changesets for all active projects
* _/sys/fetch_changesets?id=<project identifier>&key=<your service key>_ fetches changesets from the repository of a specific project

*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 &@.

See #2925 for original feature request.

h2. Step 1 : configure Redmine to accept the request

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.

h2. Step 2 : setup a post-commit script on the SCM server

You have to setup a post-commit script which will call the previous URL.

h3. Subversion

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 :
<pre><code class="sh">
#!/bin/sh

curl "http://<redmine url>/sys/fetch_changesets?key=<your service key>"
</code></pre>
or if you want to use wget
<pre><code class="sh">
wget "http://your/redmine/path/sys/fetch_changesets?key=<API KEY>"
</code></pre>
Note: Don`t forget wget in your computer path.

Or, on a Windows system (2 files) :

* @post-commit.cmd@ :
<pre><code class="cmd">
cscript "%~dp0refresh_redmine.vbs" //Nologo >> "%~dp0refresh_redmine.log" 2>&1
</code></pre>

* @refresh_redmine.vbs@ :
<pre><code class="vbs">
private const REDMINE_SERVICE_KEY = "<your service key>"

Call HTTPGet("http://<redmine url>/sys/fetch_changesets?key=" HTTPPost(&quot;http://&lt;redmine url&gt;/sys/fetch_changesets&quot;, &quot;key=&quot; & REDMINE_SERVICE_KEY)

Private Function HTTPGet(sUrl) HTTPPost(sUrl, sRequest)
Dim oHTTP
set oHTTP = CreateObject("Microsoft.XMLHTTP")
oHTTP.open "GET", sUrl, False &quot;POST&quot;, sUrl,false
oHTTP.setRequestHeader &quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;
oHTTP.setRequestHeader &quot;Content-Length&quot;, Len(sRequest)
oHTTP.send sRequest
HTTPGet HTTPPost = oHTTP.responseText
End Function
</code></pre>

h3. Git

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:

<pre><code class="sh">
#!/bin/sh

curl "http://<redmine url>/sys/fetch_changesets?key=<your service key>"
</code></pre>

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.

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.

h3. Only update relevant project

/sys/fetch_changesets accepts id parameter, so you can limit the refrech to the correct project :

<pre><code class="sh">
#!/bin/bash

_apikey=XXXXXXXXXXXXXXXXXXXX

_projectid=${PWD##*/}
_projectid=${_projectid%*.git}

curl "http://<redmine url>/sys/fetch_changesets?key=$_apikey&id=$_projectid"&
</code></pre>