Gitlab Continuous Integration on Jenkins

Gitlab and Jenkins can be integrated to perform builds on certain events, such as testing every commit or automatically building and publishing master after a merge. If you're looking how to do that, using Gitlab CE and Jenkins 2.x, read on! Note that this will work for DIY setups on a private network.

Why would someone want to integrate source management tools with build tools? Well, it allows for every change to trigger a build (aka "continuous integration") which vastly improves code quality and the speed of code reviews; it eliminates questions on whether a change is safe, and makes it more difficult for changes to break master.

Here's a screenshot from Gitlab showing the status of several merge requests and branch updates. Gorgeous.

If you don't have Gitlab and Jenkins already, you'll need to decide if you want a temporary solution or a more production setup. For this article I wrote a quick docker-compose to simulate the environment (copy this project, execute make setup run). Otherwise I'd suggest VMs on DigitalOcean -- use this link for a $10 credit.

1. Gitlab Configuration

1.1 Create the build user and access token

It's nice to have the CI builds performed by a build user so that it's not tied to an individual. So create a new user in Gitlab named jenkins-user. Impersonate that user (or login) and create a new access token at Profile Settings -> Access Tokens. SAVE that token for the Jenkins setup later.

Gitlab creating a user access token

1.2 Create the ljdelight/thrifty project

To demonstrate Gitlab-Jenkins CI, import my fork of Thrifty which is identical to Microsoft's with the addition of a Jenkinsfile. To import the project: Gitlab New Project -> git project by URL and use https://github.com/ljdelight/thrifty.git. Add the jenkins-user as a Developer of the project.

2. Jenkins Configuration

2.1 Install Required Plugins

Jenkins features are added mostly through plugins. Install these (Manage Jenkins -> Manage Plugins -> Available tab):

2.2 Create credentials for Gitlab access

Jenkins CI will send build status updates to an authenticated Gitlab API. We need to add the Gitlab user jenkins-user's access token for Jenkins to authenticate. And for private repositories, credentials are required when cloning repositories. Jenkins credentials are on the left pane of the dashboard under Credentials.

  • Create a "Gitlab API Token": Paste in the Gitlab access token and give it the ID gitlab-jenkins-user-api-token.
  • Create a "Username with password" with the username as jenkins-user and the password as the gitlab api token. Set the ID to gitlab-jenkins-user-credentials.

Jenkins adding an API Token

2.3 Configure the Jenkins Gitlab connection

The last part is to navigate to Manage Jenkins -> Configure System and find the Gitlab section. Enable authentication, name the connection, fill out the URL, and select the already-added gitlab token. If you're using http, check the "ignore ssl certificate errors" under Advanced. Test the connection to verify the setup is correct.

Jenkins gitlab configuration

The initial setup is tedious, but now things are secure between Gitlab and Jenkins! Just add some projects and follow my Part 2 to setup CI builds for every merge requests. Then look at Part 3 (coming soon) for building when branches have new commits.

Happy coding :)


Powered by Digital Ocean

Click the image for a $10 referral