Thursday, 28 December 2017

Custom Gitlab Hooks

Gitlab installation

Update linux machine with yum update.
yum -y update


Once update is completed install postfix for notifications. 
yum install postfix
service postfix start
chkconfig postfix on



Add the GitLab package repository and install the package
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | bash


Install gitlab package. Here specify the External URL you want gitlab to be accessed.
EXTERNAL_URL="http://192.168.100.25" yum -y install gitlab-ee



If you need to change EXTERNAL_URL or IP address of the host where gitlab is installed following are the steps.
1. Modify external_url parameter in file /etc/gitlab/gitlab.rb
2. gitlab-ctl reconfigure

Start gitlab with command below
gitlab-ctl start



Configure gitlab on 8888 port so that apache can be started on port 80.
Open /etc/gitlab/gitlab.rb and modify 
nginx['listen_port'] = nil to nginx['listen_port'] = 8888
external_url 'http://192.168.100.25' to external_url 'http://192.168.100.25:8888'






Once modification completes execute reconfigure command
gitlab-ctl reconfigure



Restart gitlab to get the new setting in effect.
gitlab-ctl restart



Login to gitlab external url with following address
http://192.168.100.25:8888

Once you open page for the first time you need to set the root user password. Type root user password and click on Submit.


Now register new user. Click on Create project to add new gitlab project.



Set Project name. Click on Create Project.


 Clone git repository locally.
git clone http://192.168.100.25:8888/dmaharjan/dilli_com_np.git


Add index page on local repository.
cd dilli_com_np
echo "This is index page" >> index.html



Add file, commit and push to git remote repository.
git add .
git commit -m "Adding index.html"
git push


Install apache. In our case apache server is web hosting for the client.
yum -y install httpd


Change directory to /var/www/html and clone git repo.
git clone http://dmaharjan@192.168.100.25:8888/dmaharjan/dilli_com_np.git



The page is accessible with the URL below:
http://192.168.100.25/dilli_com_np/index.html


Change directory to remote repository in our case /var/opt/gitlab/git-data/repositories/dmaharjan/dilli_com_np.git . Here dmaharjan is repos user and dilli_com_np is repository directory.
cd /var/opt/gitlab/git-data/repositories/dmaharjan/dilli_com_np.git
Create directory custom_hooks


Create hook file named post-receive with the content below
#!/bin/bash

cd /var/www/html/dilli_com_np
sudo /usr/bin/git stash
sudo /usr/bin/git pull



Change ownership of hook file and make it executable.
chown -R git:git post-receive 
chmod 755 post-receive 



Change sudo configuration and add following
git         ALL=NOPASSWD: /usr/bin/git stash, /usr/bin/git pull



Change ownership of /var/www/html/dilli_com_np
chown -R apache:apache /var/www/html/dilli_com_np


Login as user git and verify sudo is working for user git. Make sure Password is not required for sudo and git pull



Now change the content of the local repo, add the file, commit it and push it. The hook should be executed and change should be reflected immediately on the web directory.
Change directory to local repository and add content to the file.
echo "Adding new line 1" >> index.html 
git add .
git commit -m "Testing hook"
git push




View change on the web directory.











Harry

Author & Editor

A technology enthusiast and addictive blogger who likes to hacking tricks and wish to be the best White Hacket Hacker of the World.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.