Autodeployment using GitLab Webhooks

Gitlab webhooks

GitLab Webhooks

GitLab provides various Webhooks to perform automated tasks after a certain event. For e.g. Push events Gitlab webhooks will work exactly similar to post-receive hook in GIT. Only thing is it needs a webpage (mostly on your server) which gets executed after a certain commit is made to the GitLab repository. This webpage can perform tasks like updating the local mirror repository and making the corresponding change in the files in your public_html folder. Read more about webhooks in GitLab

Assuming you have a GitLab repository at: git@gitlab.com:username/some_project.git

Steps to follow on your production/live server

  1. SSH to your production/live server
  2. Generate an SSH key for auto deployment to be used by the GitLab server

    Name of the key: gitlab_rsa_deploy, keep the passphrase blank
  3. Add the contents of the gitlab_rsa_deploy.pub key to Deyloy Keys section of your Project Settings and Enable it
  4. Create a config file in .ssh folder and add below contents to the file. Add your user key added to GitLab user key settings.
  5. Create a folder name gitlab preferably outside your public_html folder
  6. Create a mirror repository by cloning your gitlab project
  7. Set Work Directory
  8. Perform git fetch once on the mirror repository so that the SSH keys get added to the server
  9. Create the webhook file on your server as webhook.php. Add below contents to the file and save this file inside your public_html folder.
  10. Add the URL of the webhook.php file to the list of webhooks on GitLab for Push events as shown below
    GitLab Webhook
  11. Now just commit some change either through GitLab or through your local repository and check if autodeployment is working

Set remote GIT on VPS / Dedicated server

It is possible to set Remote GIT on your own server provided your hosting provider allows to install GIT on the server.

If your site/application is hosted on VPS or a dedicated server then it much easier to do so.

Prerequisite to set remote GIT

GIT should be installed on there server

If you are on VPS or dedicated server then it is likely that GIT is already installed on the server.

To check if GIT is installed or not SSH to your server and try below command.

If GIT is installed then it will show the version of GIT.

If not it will give an error message.

If GIT is not installed then you first need to install GIT on your server. For this you may need root access to your server. If you do not have root access ask your hosting provider to install GIT for you.

Once git is installed follow below steps on the server to set remote GIT

Create a Directory

SSH to your server (using Putty on Windows or using terminal on MAC)

Initialise Repository

Hook (Push to deploy)

post-receive hook in GIT is used to perform action after receiving or committing a file to GIT. This hook can be used to deploy our changes automatically to the live server using below commands.

Add below content to the post-receive hook/file

Press ctrl+d to save the file

File permissions

Through these simple steps your GIT is ready to accept commits. On the client side or on the localhost you can set up Remote GIT to push your changes to the server. This is easily possible if you are using Netbeans or similar software.