bala-16
bala-16

Reputation: 134

How to setup Gitlab Web Hook to deployment

I have created a new project and branches in that project. I need to configure one particular branch to my development server. Whenever pushed to that branch it should be automatically deployed to the server.

I have tried with Web Hook in Gitlab. But it did not work. Please give a ref. step by step link. What configurations I need to do in my local machine as well as the server system. I have found that we need to setup some "Post Receive WebHook in server", can someone please give an idea.

"Here I will explain what is in my hand. The server already setup without git. I have one project inside one branch "develop". I have cloned in my local and pushing to the branch. It is working fine. (I am using window machine git bash). But here after when I push some update I need to push to server. So, I have tried with Web Hook. But it is not working. "

Upvotes: 3

Views: 14825

Answers (2)

Shalako Lee
Shalako Lee

Reputation: 138

I also ran into this issue, there is hardly any documentation for beginners.

Here are the steps I took to get this working with gitlab:

make sure to have an SSH key on the server that you want to auto deploy on, for me this was an Ubuntu server. I had to generate an SSH key for the www-data so the user could connect to the gitlab server.

I had to run this command to generate an ssh key for the www-data user:

sudo -u www-data ssh-keygen -t rsa

After you generate the ssh key you have to add it to the gitlab to your projects deploy keys. (project->settings->deploy keys).

now goto the folder that you want your project to push to (for me it was /var/www/website/) and do the following:

git clone origin-url .

the dot at the end makes it so it only pulls the files, and doesn't include the folder your project is in on gitlab.

then...

chgrp -R www-data .git
chmod -R g+rwxs .git

That gives permission to the git folder to execute php commands and adds the www-data group to the folder.

after that i had to make a script that would run the commands i wanted when the hook was called.

here is the php that i am using, i named the file webhook.php

<?php

if($payload = file_get_contents('php://input')) {
    try {
            $payload = json_decode($payload);
    } catch(Exception $ex) {
            echo $ex;
            exit(0);
    }
    // put the branch you want here
    if($payload->ref != "refs/heads/master") {
            echo "wrong head";
            exit(0);
    }
    //put the branch you want here, as well as the directory your site is in
    $result = `cd /var/www/website && git fetch origin && git merge origin/master`;
   
    echo $result;
} else {
    echo "failed request";
} 
?>

the webhook.php will need execute permissions from www-data as well.

once all that is done you will need to make the webhook in your project (project->settings->webhooks)

add the url to the webhook.php

I put my webhook.php in a subdomain on the server so I could use it to push different sites with the same file.

http://deploy.website.com/webhook.php

I also added used http://requestb.in so that I could make sure of the data that was getting sent to the server.

Not sure if i'm forgetting anything, but I think this was everything i did

Upvotes: 12

Lo&#239;c
Lo&#239;c

Reputation: 11942

There is a configuration option which allows that (turned off by default). On push it will synchronize the server directly.

If your git server isn't on the same machine than your http server you can use that : http://ryanflorence.com/deploying-websites-with-a-tiny-git-hook/

Upvotes: 0

Related Questions