Project

General

Profile

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