Project

General

Profile

HowTo keep Redmine in sync with Github without dedicated plugin (Apache CGI + Github Webhook) » History » Version 6

crypto gopher, 2018-09-18 14:34

1 1 crypto gopher
h1. HowTo keep Redmine in sync with Github without dedicated plugin (Apache CGI + Github Webhook)
2
3
This is a solution in case you don't want to install additional plugins just to keep repository synchronised. It requires you to have Apache webserver with access to repository you are trying to sync. Apache has to support running CGI scripts.
4
5 4 crypto gopher
h2. Clone Github repository
6
7
Clone repository and make sure it is accessible by webserver:
8
<pre>
9
mkdir /var/lib/redmine/repo
10
chown apache /var/lib/redmine/repo
11
su -u apache git -C /var/lib/redmine/repo clone https://github.com/username/repo_name.git
12
</pre>
13
14
h2. Enable WS for repository management in Redmine
15
16
Go to @https://your.redmine.com/settings?tab=repositories@ and:
17
* select: _Enable WS for repository management_
18
* generate a repository management WS API key and save it for next step
19
20 1 crypto gopher
h2. Prepare CGI script
21
22 4 crypto gopher
Any script you run on your server will do. Below is an example of Bash script that pulls git repository and notifies Redmine to fetch changesets (substitute @<project-id>@ and @<repository-api-key>@ with your own):
23 1 crypto gopher
<pre>
24
#!/bin/sh
25 6 crypto gopher
# Empty stdin, Apache issue https://bz.apache.org/bugzilla/show_bug.cgi?id=44782
26 5 crypto gopher
cat <&0 >/dev/null
27
28 4 crypto gopher
/usr/bin/git -C /var/lib/redmine/repo/repo_name pull -n -q
29 1 crypto gopher
result1=$?
30
31 4 crypto gopher
/usr/bin/curl --max-time 60 -s 'https://your.redmine.com/sys/fetch_changesets?id=<project-id>&key=<repository-api-key>' >/dev/null
32 1 crypto gopher
result2=$?
33
34
if [[ $result1 && $result2 ]]; then
35
  echo "Status: 200 OK"
36
else
37
  echo "Status: 500 Internal Server Error"
38
fi
39
40
echo "Content-Type: text/plain; charset=utf-8"
41
echo
42
43
if [[ $result1 ]]; then
44
  echo "git pull: ok"
45
else
46
  echo "git pull: failed"
47
fi
48
49
if [[ $result2 ]]; then
50
  echo "fetch changesets: ok"
51
else
52
  echo "fetch changesets: failed"
53
fi
54
</pre>
55
56
Let's say you save this script under: _/var/www/localhost/cgi-bin/update-repo.cgi_
57 4 crypto gopher
58
You can test if script executes properly:
59
<pre>
60
sudo -u apache /var/www/localhost/cgi-bin/update-repo.cgi
61
</pre>
62 1 crypto gopher
63 2 crypto gopher
h2. Configure Apache to run script whenever particular URL is requested
64
65
Inside @VirtualHost@ of your choice just add:
66
<pre>
67
  ...
68
  # Github webhook for repository pull/update
69
  ScriptAlias /update-repo.cgi /var/www/localhost/cgi-bin/update-repo.cgi
70
  <Directory /var/www/localhost/cgi-bin/>
71
    Options ExecCGI
72
    AllowOverride None
73
    Require all granted
74
  </Directory>
75
  ...
76
</pre>
77
78
In case you use the same @VirtualHost@ to proxy requests to your Redmine @rails server@, you should exclude your special URL from being proxied with:
79
<pre>
80
ProxyPass /update-repo.cgi !
81
</pre>
82
83 3 crypto gopher
h2. Configure Github Webhook
84
85
Go to your Github repository page, choose _Settings -> Webhooks -> Add webhook_. Then set:
86
* Payload URL: @https://your.virtualhost.com/update-repo.cgi@
87
* Which events would you like to trigger this webhook?: Just the push event.
88
* Active: yes
89
90
Update webhook and you're done.