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

crypto gopher, 2018-09-17 15:11

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 4 crypto gopher
/usr/bin/git -C /var/lib/redmine/repo/repo_name pull -n -q
26 1 crypto gopher
result1=$?
27 1 crypto gopher
28 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
29 1 crypto gopher
result2=$?
30 1 crypto gopher
31 1 crypto gopher
if [[ $result1 && $result2 ]]; then
32 1 crypto gopher
  echo "Status: 200 OK"
33 1 crypto gopher
else
34 1 crypto gopher
  echo "Status: 500 Internal Server Error"
35 1 crypto gopher
fi
36 1 crypto gopher
37 1 crypto gopher
echo "Content-Type: text/plain; charset=utf-8"
38 1 crypto gopher
echo
39 1 crypto gopher
40 1 crypto gopher
if [[ $result1 ]]; then
41 1 crypto gopher
  echo "git pull: ok"
42 1 crypto gopher
else
43 1 crypto gopher
  echo "git pull: failed"
44 1 crypto gopher
fi
45 1 crypto gopher
46 1 crypto gopher
if [[ $result2 ]]; then
47 1 crypto gopher
  echo "fetch changesets: ok"
48 1 crypto gopher
else
49 1 crypto gopher
  echo "fetch changesets: failed"
50 1 crypto gopher
fi
51 1 crypto gopher
</pre>
52 1 crypto gopher
53 1 crypto gopher
Let's say you save this script under: _/var/www/localhost/cgi-bin/update-repo.cgi_
54 4 crypto gopher
55 4 crypto gopher
You can test if script executes properly:
56 4 crypto gopher
<pre>
57 4 crypto gopher
sudo -u apache /var/www/localhost/cgi-bin/update-repo.cgi
58 4 crypto gopher
</pre>
59 1 crypto gopher
60 2 crypto gopher
h2. Configure Apache to run script whenever particular URL is requested
61 2 crypto gopher
62 2 crypto gopher
Inside @VirtualHost@ of your choice just add:
63 2 crypto gopher
<pre>
64 2 crypto gopher
  ...
65 2 crypto gopher
  # Github webhook for repository pull/update
66 2 crypto gopher
  ScriptAlias /update-repo.cgi /var/www/localhost/cgi-bin/update-repo.cgi
67 2 crypto gopher
  <Directory /var/www/localhost/cgi-bin/>
68 2 crypto gopher
    Options ExecCGI
69 2 crypto gopher
    AllowOverride None
70 2 crypto gopher
    Require all granted
71 2 crypto gopher
  </Directory>
72 2 crypto gopher
  ...
73 2 crypto gopher
</pre>
74 2 crypto gopher
75 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:
76 2 crypto gopher
<pre>
77 2 crypto gopher
ProxyPass /update-repo.cgi !
78 2 crypto gopher
</pre>
79 2 crypto gopher
80 3 crypto gopher
h2. Configure Github Webhook
81 3 crypto gopher
82 3 crypto gopher
Go to your Github repository page, choose _Settings -> Webhooks -> Add webhook_. Then set:
83 3 crypto gopher
* Payload URL: @https://your.virtualhost.com/update-repo.cgi@
84 3 crypto gopher
* Which events would you like to trigger this webhook?: Just the push event.
85 3 crypto gopher
* Active: yes
86 3 crypto gopher
87 3 crypto gopher
Update webhook and you're done.