Deploying a Web Project from Gitlab

I wanted to automatically deploy a web project when any updates are pushed to the gitlab repository. To do so I added a ‘Web Hook’. The Web Hook calls a URL in response to ‘push’ events. I created a PHP page to be called by the Web Hook:

gitlab deploy hooks

So in response to push events gitlab_hook.php is called:

shell_exec('cd /path/to/website; /bin/bash gitlab_update.sh ');

The PHP file calls a Shell script gitlab_update.sh :

#!/bin/bash
cd /var/www/vhosts/rave.eeecs.qub.ac.uk/rave; git pull origin master > /var/log/rave-git-clone.out 2>&1

All this is dependent on setting the permissions appropriately for the user the web server (e.g. apache, www-data) is running as. It is also necessary to ssh-keygen an RSA key for the ‘apache’ user. The public key can then be added to the ‘Deploy Keys’ section in Gitlab project.

Test the gitlab_update.sh script successfully works as the ‘apache’ user before testing from Gitlab:

sudo -u apache ./gitlab_update.sh

This was just a first stab at deploying automatically from Gitlab. A few changes could make it more secure.

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>