Jenkins pipeline with Gitlab

Victor Yeo
3 min readMar 3, 2023

I am using Jenkins version 2.375.3.

Jenkins version

I encounter a few obstacles setting up Jenkins pipeline with Gitlab. In this article, i will describe the problems and solutions for each problem.

To use Jenkins with Gitlab:

  1. In Gitlab, click on user icon on top right corner, click Edit Profile->Access Tokens, then we create a new personal access token.
Personal access token in Gitlab

2. In Jenkins, we install Gitlab plugins

3. In Jenkins, create a credential of “Gitlab API token”

4. Configure the Jenkins system to use the Gitlab API token

5. However, when click the test connection button, i keep getting the error below:

After debugging, i discover there is a bug in the Jenkins version that i am using (by looking at Jenkins system log).

The bug is explained in the link below.

To solve the bug, i need to manually install Jersey 2 API Plugin in Jenkins. The link below shows theJersey 2 API homepage.

Download the Jersey 2 API Plugin, then deploy it in Jenkins.

After that, the test connection is successful, as shown below.

6. Next, we setup the pipeline for running tasks from Jenkinsfile on Gitlab repo. The screenshot below shows the pipeline setup information.

Jenkins pipeline setup

7. The credentials in the screenshot above is a ssh username with private key credentials.

When i run the pipeline, i keep getting this “Access denied” error, many times.

After some research, i found out the ssh credentials should be setup in the context of Jenkins user (and not ec2-user).

To do that, run:

$ sudo su -s /bin/bash jenkins
$ ssh-keygen

Then, go to Gitlab, add the content of public key in /var/lib/jenkins/.ssh/id_rsa.pub to SSH Key in Gitlab.

setup Gitlab SSH keys

Then, in Jenkins , create credentials of “ssh username with private key”

and copy the private key of /var/lib/jenkins/.ssh/id_rsa to the private key content, as shown below.

setup Jenkins ssh private key

8. In Jenkinsfile, add the url that points to the Gitlab repo, and add credentialsId that must match the credential ID.

With that, Gitlab pipeline should run.

--

--