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

crypto gopher, 2018-09-18 14:35

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 4 crypto gopher
h2. Clone Github repository
6 4 crypto gopher
7 4 crypto gopher
Clone repository and make sure it is accessible by webserver:
8 4 crypto gopher
<pre>
9 4 crypto gopher
mkdir /var/lib/redmine/repo
10 4 crypto gopher
chown apache /var/lib/redmine/repo
11 4 crypto gopher
su -u apache git -C /var/lib/redmine/repo clone https://github.com/username/repo_name.git
12 4 crypto gopher
</pre>
13 4 crypto gopher
14 4 crypto gopher
h2. Enable WS for repository management in Redmine
15 4 crypto gopher
16 4 crypto gopher
Go to @https://your.redmine.com/settings?tab=repositories@ and:
17 4 crypto gopher
* select: _Enable WS for repository management_
18 4 crypto gopher
* generate a repository management WS API key and save it for next step
19 4 crypto gopher
20 1 crypto gopher
h2. Prepare CGI script
21 1 crypto gopher
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 1 crypto gopher
#!/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 5 crypto gopher
28 4 crypto gopher
/usr/bin/git -C /var/lib/redmine/repo/repo_name pull -n -q
29 1 crypto gopher
result1=$?
30 1 crypto gopher
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 1 crypto gopher
34 1 crypto gopher
if [[ $result1 && $result2 ]]; then
35 1 crypto gopher
  echo "Status: 200 OK"
36 1 crypto gopher
else
37 1 crypto gopher
  echo "Status: 500 Internal Server Error"
38 1 crypto gopher
fi
39 1 crypto gopher
40 1 crypto gopher
echo "Content-Type: text/plain; charset=utf-8"
41 1 crypto gopher
echo
42 1 crypto gopher
43 1 crypto gopher
if [[ $result1 ]]; then
44 1 crypto gopher
  echo "git pull: ok"
45 1 crypto gopher
else
46 1 crypto gopher
  echo "git pull: failed"
47 1 crypto gopher
fi
48 1 crypto gopher
49 1 crypto gopher
if [[ $result2 ]]; then
50 1 crypto gopher
  echo "fetch changesets: ok"
51 1 crypto gopher
else
52 1 crypto gopher
  echo "fetch changesets: failed"
53 1 crypto gopher
fi
54 1 crypto gopher
</pre>
55 1 crypto gopher
56 1 crypto gopher
Let's say you save this script under: _/var/www/localhost/cgi-bin/update-repo.cgi_
57 4 crypto gopher
58 4 crypto gopher
You can test if script executes properly:
59 4 crypto gopher
<pre>
60 7 crypto gopher
echo "some input" | sudo -u apache /var/www/localhost/cgi-bin/update-repo.cgi
61 4 crypto gopher
</pre>
62 1 crypto gopher
63 2 crypto gopher
h2. Configure Apache to run script whenever particular URL is requested
64 2 crypto gopher
65 2 crypto gopher
Inside @VirtualHost@ of your choice just add:
66 2 crypto gopher
<pre>
67 2 crypto gopher
  ...
68 2 crypto gopher
  # Github webhook for repository pull/update
69 2 crypto gopher
  ScriptAlias /update-repo.cgi /var/www/localhost/cgi-bin/update-repo.cgi
70 2 crypto gopher
  <Directory /var/www/localhost/cgi-bin/>
71 2 crypto gopher
    Options ExecCGI
72 2 crypto gopher
    AllowOverride None
73 2 crypto gopher
    Require all granted
74 2 crypto gopher
  </Directory>
75 2 crypto gopher
  ...
76 2 crypto gopher
</pre>
77 2 crypto gopher
78 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:
79 2 crypto gopher
<pre>
80 2 crypto gopher
ProxyPass /update-repo.cgi !
81 2 crypto gopher
</pre>
82 2 crypto gopher
83 3 crypto gopher
h2. Configure Github Webhook
84 3 crypto gopher
85 3 crypto gopher
Go to your Github repository page, choose _Settings -> Webhooks -> Add webhook_. Then set:
86 3 crypto gopher
* Payload URL: @https://your.virtualhost.com/update-repo.cgi@
87 3 crypto gopher
* Which events would you like to trigger this webhook?: Just the push event.
88 3 crypto gopher
* Active: yes
89 3 crypto gopher
90 3 crypto gopher
Update webhook and you're done.