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

crypto gopher, 2018-09-17 15:00

1 1 crypto gopher
h1. HowTo keep Redmine in sync with Github without dedicated plugin (Apache CGI + Github Webhook)
2 1 crypto gopher
3 1 crypto gopher
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 1 crypto gopher
5 1 crypto gopher
h2. Prepare CGI script
6 1 crypto gopher
7 1 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:
8 1 crypto gopher
<pre>
9 1 crypto gopher
#!/bin/sh
10 1 crypto gopher
/usr/bin/git -C /var/lib/redmine/tv/repo/token_voting pull -n -q
11 1 crypto gopher
result1=$?
12 1 crypto gopher
13 1 crypto gopher
/usr/bin/curl --max-time 60 -s 'https://tv.michalczyk.pro/sys/fetch_changesets?id=token-voting&key=2GkhmLmtTjKoXYf6PS9y' >/dev/null
14 1 crypto gopher
result2=$?
15 1 crypto gopher
16 1 crypto gopher
if [[ $result1 && $result2 ]]; then
17 1 crypto gopher
  echo "Status: 200 OK"
18 1 crypto gopher
else
19 1 crypto gopher
  echo "Status: 500 Internal Server Error"
20 1 crypto gopher
fi
21 1 crypto gopher
22 1 crypto gopher
echo "Content-Type: text/plain; charset=utf-8"
23 1 crypto gopher
echo
24 1 crypto gopher
25 1 crypto gopher
if [[ $result1 ]]; then
26 1 crypto gopher
  echo "git pull: ok"
27 1 crypto gopher
else
28 1 crypto gopher
  echo "git pull: failed"
29 1 crypto gopher
fi
30 1 crypto gopher
31 1 crypto gopher
if [[ $result2 ]]; then
32 1 crypto gopher
  echo "fetch changesets: ok"
33 1 crypto gopher
else
34 1 crypto gopher
  echo "fetch changesets: failed"
35 1 crypto gopher
fi
36 1 crypto gopher
</pre>
37 1 crypto gopher
38 1 crypto gopher
Let's say you save this script under: _/var/www/localhost/cgi-bin/update-repo.cgi_
39 1 crypto gopher
40 2 crypto gopher
h2. Configure Apache to run script whenever particular URL is requested
41 2 crypto gopher
42 2 crypto gopher
Inside @VirtualHost@ of your choice just add:
43 2 crypto gopher
<pre>
44 2 crypto gopher
  ...
45 2 crypto gopher
  # Github webhook for repository pull/update
46 2 crypto gopher
  ScriptAlias /update-repo.cgi /var/www/localhost/cgi-bin/update-repo.cgi
47 2 crypto gopher
  <Directory /var/www/localhost/cgi-bin/>
48 2 crypto gopher
    Options ExecCGI
49 2 crypto gopher
    AllowOverride None
50 2 crypto gopher
    Require all granted
51 2 crypto gopher
  </Directory>
52 2 crypto gopher
  ...
53 2 crypto gopher
</pre>
54 2 crypto gopher
55 2 crypto gopher
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:
56 2 crypto gopher
<pre>
57 2 crypto gopher
ProxyPass /update-repo.cgi !
58 2 crypto gopher
</pre>
59 2 crypto gopher
60 3 crypto gopher
h2. Configure Github Webhook
61 3 crypto gopher
62 3 crypto gopher
Go to your Github repository page, choose _Settings -> Webhooks -> Add webhook_. Then set:
63 3 crypto gopher
* Payload URL: @https://your.virtualhost.com/update-repo.cgi@
64 3 crypto gopher
* Which events would you like to trigger this webhook?: Just the push event.
65 3 crypto gopher
* Active: yes
66 3 crypto gopher
67 3 crypto gopher
Update webhook and you're done.