Jenkins GitLab integration with webhook

In this article I will show how to configure Jenkins server to work with GitLab with webhook. It means when developer will push some code in the repository where webhook already configured it will trig execution of Jenkins job. If you want to configure Jenkins with GitLab please use this link.

Jenkins server IP address: 192.168.106.100
GitLab server IP address: 192.168.106.230

Network topology:
jkis-gitlab-topo

In Linux or Windows desktop create SSH keypair which will be used to connect from desktop and from Jenkins. Content of the private ‘gitlab’ key will be used to connect to the GitLab server. Content of the ‘gitlab.pub’ public key will be imported to out repository which will be created in the GitLab. Add key to ssh list.

$ ssh-keygen -b 2048 -t rsa -f gitlab -q -N ""
$ ls -la gitlab*
-rw-r--r-- 1 jamal.shahverdiyev 1049089 1679 Feb  7 15:59 gitlab
-rw-r--r-- 1 jamal.shahverdiyev 1049089  413 Feb  7 15:59 gitlab.pub
$ eval $(ssh-agent.exe -s)
Agent pid 860
$ ssh-add.exe gitlab
Identity added: gitlab (gitlab)
$ ssh-add.exe -l
2048 SHA256:39wtTMQC6+910gOV/0ilTUiuglmRyqiYqaPnytBEcnU gitlab (RSA)

In the GitLab create new repository and import ‘gitlub.pub’ public key to this repository. Reset GitLab ‘root’ username password:

> vagrant ssh GitLab
[vagrant@localhost ~]$ sudo su -
[root@localhost ~]# echo 'R00tpass123' | openssl passwd -1 -stdin
$1$VCr0gWBG$rggDtfhsWOtjHgORD0Pbg1
[root@localhost ~]# gitlab-rails console production
Loading production environment (Rails 4.2.10)
irb(main):001:0> user = User.where(id: 1).first
=> #
irb(main):002:0> user.password = 'R00tpass123'
=> "R00tpass123"
irb(main):003:0> user.password_confirmation = 'R00tpass123'
=> "R00tpass123"
irb(main):004:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 045c47d8-c866-4e23-9dc2-d336cf5608b3) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
=> true
irb(main):005:0> exit

Create new project:
gitlab-new-project

Enter name of the project ‘jenkins-webhook‘ -> Visibility level ‘Private‘ -> Then press ‘Create project’ button.
project-gitlab-private.png

In the top of page press ‘add an SSH key’ button:
add-gitlab-ssh-key

Copy and paste the content of the ‘gitlab.pub‘ file which we created before and add ‘Add key‘ button.
gitlab-add-cleaned-key

Go to the left side and press ‘Access Tokens
select-access-token

Enter name of token ‘JenkinsSRV‘, set expiration date, select access level and press ‘Create personal access token
create-personal-access-token

Then copy the content of the ‘Your New Personal Access Token‘ to some secure place because you cannot copy this again.
personal-access-tokens

First of all, update all plugins ang Jenkins itself. Then install ‘GitLab Plugin‘ from availables. Main menu Jenkins -> Manage Jenkins -> Manage Plugins -> Available tab and search ‘GitLab Plugin‘ -> Select Chechbox -> Download now and install after restart
jenkins-install-gitlab-plugin

Add new credentials for SSH and Token. Firstly we will add SSH private key credential for git. Go to the main menu -> Credentials -> global
add-2-new-credentials

Press Add Credentials -> Select SSH Username with private key -> Enter username ‘git‘ -> Select Enter directly and paste content of the ‘gitlab‘ private key (we created this key before) here -> Write some description and press ‘OK‘ button.
gitlab-ssh-private-key

Add one more credential for ‘GitLab API Token‘. In the Kind select ‘GitLab API Token‘ -> In the API Token paste token which we created before in the GitLab server -> enter some ID with description and press OK button
gitlab-api-token-credential

Our credentials:
gitlab-jenkins-credentials-result

Configure connection between Jenkins and GitLab. Go to the Main menu -> Manage Jenkins -> Configure System
gitlab-global-config

In the GitLab section uncheck ‘Enable authentication for /project end-point‘, enter connection name, enter GitLab url, select credentials for token, press Test Connection button (Result must be: Success) and press Apply/Save
gitlab-connector-configure

Create new ‘Freestyle project‘. Go to the Main menu -> New Item and enter name ‘Jenkins-GitLab-WebHook-Trig‘ and press ‘OK‘ button.
create-new-gitlab-proje

Under ‘General‘ tab we must see connection ‘GitLab Connector
general-tab-in-proje.png

Under ‘Source Code Management‘ select ‘Git‘ -> Enter our project repo url, select git credential (SSH key created before), Enter Name origin, Enter refspec +refs/heads/*:refs/remotes/origin/* +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/* and in the Branch Specifier enter origin/${gitlabSourceBranch}
scm-in-proje

In the Build Triggers section select ‘Build when a change is pushed to GitLab. GitLab CI Service URL: http://192.168.106.100:8080/project/Jenkins-GitLab-WebHook-Trig‘ and configure events as in screenshot:
build-triggers-in-proje.png

In the ‘Post-build Actions‘ -> press ‘Add post-build action‘ and select ‘Publish build status to GitLab commit(GitLab 8.1 + required)‘ then press ‘Apply
publish-build-in-proje
post-build-action-screen

For test purpose in the Build section new build Add build step Execute shell with some commands and then press ‘Save‘ button
add-build-execut-shell
add-build-execut-shell-commands

Now we need configure GitLab to trigger builds. Go to the GitLab -> Select your project -> Press Settings -> Press Integrations
integrate-gitlab-jenkins

In the opened page enter url for Jenkins project like as the following syntax
http://jenkinsIP:port/project/JenkinsJobName

Select ‘Push events‘ and ‘Merge Requests events‘, uncheck ‘Enable SSL verification‘ and press ‘Add webhook
add-jenkins-url.png
add-jenkins-url1

Now from windows push somethings to see the result:

$ git clone git@192.168.106.230:root/jenkins-webhook.git
$ cd jenkins-webhook/
$ echo 'This is test Readme' > README.md
$ git add README.md
$ git commit -m "ADDED README"
$ git push

Output from Jenkins:
jenkins-trigger-executed.png

In the GitLab side look at the output. Go to your project name -> select Respository -> Commits
see-gitlab-jenkins-response

Look at the build status:
gitlab-latest-result

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s