Jenkins & GitLab - Interactions between platforms

In short
  • A tool to do everything or several specialized tools: both strategies are possible.
  • Configuring the interactions between Jenkins and GitLab.
  • Triggering pipelines on push and retrieving code from Git.
  • Document and diagram for maintainability.

Introduction

There are two main ways to build your CI/CD pipeline.

A solution to rule them all!

The first is to use a single product to handle all stages of the pipeline. We are increasingly seeing "platforms" that can handle both CI and CD (for those who don't know the difference, an explanation is available here).

An all-in-one tool

Click the image to enlarge.

For example, GitLab, which I propose installing under Kubernetes in this article, falls into this category.

Jenkins, for which you can find a tutorial here, can also be used for everything. Although originally designed for CD, its extensibility through numerous plugins allows it to cover CI as well.

The cloud also has its complete suites, such as Azure DevOps, which allows you to cover the entire spectrum of CI/CD.

The advantage of using only one tool for everything is the level of integration offered and the simplicity of maintaining the pipelines. Teams specialize in the chosen software, and we can build on everyone's work.

The disadvantages are the dependence induced by the solution and the difficulty sometimes encountered when configuring complex things to perform simple actions. Some tasks may ultimately be complex because it is not always easy to do everything in a basic and efficient way when trying to integrate everything into a single product.

Most of these solutions sometimes have to make compromises to handle a part of the pipeline compared to specialized tools.

Divide and conquer

That's why the second approach involves linking different tools together, each being optimized for one or more tasks in the chain. This allows us to use extremely efficient applications in a specific area, such as GitOps with ArgoCD, which you can find a presentation of in this article.

The combination of tools

Click the image to enlarge.

The advantage of this method is that it's more modular, adaptable, and customizable, while also potentially being more effective in certain parts of the chain. The disadvantage remains the complexity it can introduce. It becomes necessary to train yourself on multiple tools and, above all, to maintain a connection between them. Upgrading one application can degrade the chain and have a negative impact on everything else.

From my side, my experience and especially the colleagues I work with have led me towards this second method. Indeed, the disadvantages mentioned earlier regarding segmenting the chain through various tools can be mitigated when the chosen tools natively have ways to communicate with each other. In this case, and in theory, maintaining good communication between them is relatively simple if we strive to evolve them in parallel while respecting compatibility matrices.

This tutorial aims to do just that. After seeing the deployment of Jenkins and then GitLab under Kubernetes, we will be able to make them communicate together. Rather than using one or the other to design an entire chain, we will capitalize on their respective strengths to trigger a Jenkins pipeline based on a Git event.

This will allow us to establish the first building blocks of my ultimate goal: to illustrate a more complete and concrete use case around CI/CD tooling.

Reading this article requires at least a basic understanding of how Jenkins and GitLab work, and ideally you should have taken a look at my article about Jenkins, and then the one about GitLab. Because I will be relying on the installations that result from those to continue with today's example.

Configuration

Creating a service account and associated token

To begin, we create a dedicated service account for communication between Jenkins and GitLab. While it's possible to use a local GitLab account, as in the associated tutorial, we have implemented Active Directory integration via LDAP, so it is more efficient to work with an Active Directory account. For increased security, I recommend using a dedicated service account rather than a generic one. For example: svc-jenkins-togitlab (feel free to use your preferred naming convention).

Creating the service account

Click the image to enlarge.

The important thing is to be able to quickly identify the purpose of the account (the description field in Active Directory is very useful... but it's too often overlooked...).

Next, we log in to GitLab using this account. From its profile, we create an access token. This token will be specific to the service account, and the generated token will be used within Jenkins.

Connecting to GitLab with the service account

Click the image to enlarge.

Connecting to GitLab with the service account

Click the image to enlarge.

Creating the personal token for the service account

Click the image to enlarge.

Creating the personal token for the service account

Click the image to enlarge.

Creating the personal token for the service account

Click the image to enlarge.

Once this step is complete, you should normally no longer need to use this account directly.

Installing the GitLab plugin in Jenkins

Now, let's switch to Jenkins. You need to log in with administrator privileges.

The first thing to do is install the GitLab plugin.

Go to the Manage Jenkins menu, then Plugins to retrieve it.

Administering Jenkins

Click the image to enlarge.

Installing the GitLab plugin

Click the image to enlarge.

Installing this plugin will allow you to take advantage of the token generated earlier.

To do this, go to the Credentials section in Jenkins.

Credentials section

Click the image to enlarge.

Next, choose the Global domain (a credential valid for the entire Jenkins platform), and then create a new Credentials of type GitLab API Token.

Credential Global

Click the image to enlarge.

Add Credentials

Click the image to enlarge.

Leave the scope as Global, enter the token retrieved from GitLab, and remember to choose an ID and description that are easy to understand.

Add Credentials GitLab Token

Click the image to enlarge.

To verify proper functionality, stay in the Jenkins administration section but switch to the System option.

Jenkins system configuration menu

Click the image to enlarge.

Thanks to the installation of the GitLab plugin, a dedicated section is available.

Simply create a new connection.

As a name, I use the GitLab URL (gitlab.coolcopr.priv), but you can choose any value you prefer.

Next, enter the full URL (including https) of the GitLab server. Be aware that if your server is protected by a custom certificate issued by an internal CA, Jenkins must be aware of this CA. Feel free to follow my tutorial on installing Jenkins to learn more about this.

Then, choose the credential. The one you created earlier should appear.

Simply select it, then click test to validate the configuration, and then save everything.

Configuring the GitLab plugin in Jenkins

Click the image to enlarge.

At this stage, Jenkins can interact with GitLab via the API key of the dedicated AD service account, all thanks to the GitLab plugin deployed in Jenkins.

Creating a Jenkins credential for web access to GitLab

But that's not all. We will try to implement two types of interactions between Jenkins and GitLab.

The first involves, as mentioned at the beginning of the article, triggering a Jenkins pipeline in response to a push to a GitLab repository.

The second is to host the pipeline definition, i.e., the jenkinsfile, directly in Git. Instead of explicitly writing the pipeline code in the GUI, it will be written in a file, which itself is hosted in GitLab. Thus, each time the pipeline is launched, the code for that pipeline will first be retrieved from Git before being executed.

This allows you to centralize your pipeline definitions and enable their team-based tracking and versioning.

To achieve this second interaction, we need to go back to managing Jenkins credentials.

Still in the Global section, we create a credential of type Username and password.

Creating a user and password credential

Click the image to enlarge.

We use the AD service account (without specifying the domain) along with its password.

Now all conditions are met to start with the final example illustrated in the diagram below.

Target Schema

Click the image to enlarge.

Creating the repository for the jenkinsfiles

First, we create the repository dedicated to hosting the jenkinsfile. Be careful, this is not the repository associated with our future application for which we want a CI/CD process; it is simply the repository that will contain all the code for the pipelines that we might build.

I'm going to reuse the code from my previous article on Jenkins.

pipeline {
  agent any

  stages {
      stage('Test Kubernetes Access') {
          steps {
              sh 'kubectl get pods -A'
          }
      }
  }
}

It was originally written explicitly in the Jenkins interface. We will copy it into a file named demo.jenkinsfile.

This is a basic example that simply lists the pods of the Kubernetes cluster where Jenkins is running.

Next, we create the repository that will contain this file. I log in with my personal AD account on GitLab, and then I start by creating a group (in the GitLab sense) named CICD. It's simpler for me to organize everything related to CI/CD (as opposed to applications) in a dedicated group.

Creating the gitlab CICD group

Click the image to enlarge.

I will also grant this group access to the service account svc-jenkins-togitlab that I created at the beginning of this article. This will allow it to access all repositories within the group.

Group access rights

Click the image to enlarge.

Within this same group, we declare a Jenkinsfile repository, which I quickly clone onto my workstation.

Creating the JenkinsFile repository

Click the image to enlarge.

Be careful: since I am using TortoiseGIT with GitLab running under K8S, I need to modify my SSH URL to account for the specific port that I have chosen to expose GitLab Shell. This is all explained in my dedicated article on installing GitLab under Kubernetes.

Cloning the repository

Click the image to enlarge.

All that remains is to place the file demo.jenkinsfile in the folder and perform a "push" to make it available in the repository.

Creating the repository for the demo application

Since we are already on GitLab, let's now move on to the repository for the specific demo application (the one that we want to integrate into a CI/CD pipeline and for which we want to trigger a Jenkins pipeline after each modification).

We create a folder apps within which we create the repository demo-check-code (the name is not insignificant for future articles).

Creating the App group

Click the image to enlarge.

Creating the demo app repository

Click the image to enlarge.

We grant access to the service account svc-jenkins-togitlab

I verify that the repository is working correctly by cloning it onto my workstation.

Creating a Jenkins pipeline

Now, let's continue with Jenkins.

We create a new item of type Pipeline.

Creating the pipeline

Click the image to enlarge.

I decide to call it security-code-check.

In the pipeline configuration, we will provide a brief description.

Next, we switch to the GitLab connection section to use the connection that we created in the prerequisites (the one that uses the token).

Creating the pipeline with GitLab connection configuration

Click the image to enlarge.

Configuring the webhook

We continue scrolling down to check the box: Build when a change is pushed to GitLab. GitLab webhook URL:.

Be sure to note the URL provided.

We want the pipeline to trigger on an event of type push.

Configuring the Jenkins webhook

Click the image to enlarge.

A little further down, we expand the Advanced menu.

We do not filter by any branch.

Then, we click on generate to obtain a secret token; again, it is important to remember this, as with the URL, we will need it for the next steps.

Configuring the Jenkins webhook

Click the image to enlarge.

This part of the configuration covers the case of the first interaction between Jenkins and GitLab, which is triggering the pipeline on a push to the repository hosted on GitLab.

Configuring code retrieval for the pipeline

We must continue the configuration to handle the second interaction.

In the pipeline section, we select Pipeline script from SCM.

This is how you tell Jenkins to retrieve the code for the pipeline from a repository.

In this case, we choose a GIT-type repository.

We specify the URL of the Git repository associated with the first GitLab repository that we created to store the jenkinsfile files.

Note: We use an HTTPS link and not SSH.

Just below, we choose the credentials to connect to this repository.

In this case, we use a login and password credential that was previously created with the credentials of the AD service account dedicated to communication between Jenkins and GitLab.

Just below, we indicate that we are using the main branch.

Configuring the pipeline to retrieve code from Git

Click the image to enlarge.

As the code base browser, we choose GitLab.

We repeat the HTTPS URL of the repository containing the jenkinsfiles.

Regarding the version, it is necessary to align with the GitLab version. At the time of writing this article, I am using release 17.1.

Finally, at the Script Path level, we specify the name of the jenkinsfile to use, which is the file pushed into the dedicated repository: demo.jenkinsfile.

Configuring the pipeline to retrieve code from Git

Click the image to enlarge.

If you decide to host all your jenkinsfiles in a single repository, you can still reproduce this configuration by simply changing the name of the file to be called.

If you are using subfolders within the repository, you must specify the full path.

Otherwise, you can also decide to use dedicated repositories. In that case, you will need to adjust your settings to match your repositories.

We can complete the configuration by clicking on save.

Configuring the webhook in GitLab

Now we need to go back to GitLab.

Before configuring the webhook, we first need to make a global configuration change.

To do this, you must use an account with administrative privileges.

We navigate to the admin, settings, and then Network section.

We expand the Outbound requests section to check the box Allow Request to the local Network.

You can optionally filter the IPs and domains allowed for webhooks.

Global GitLab configuration

Click the image to enlarge.

Don't forget to save.

We can then continue where we left off on GitLab, using our standard account for the "apps" repository, which I personally named demo-check-code.

In the settings of this repository, we choose the webhook option.

Creating the GitLab webhook

Click the image to enlarge.

We create a new webhook.

Creating the webhook

Click the image to enlarge.

This webhook uses the URL returned previously by Jenkins during the creation of the pipeline.

Configuring the GitLab webhook

Click the image to enlarge.

Similarly, the secret must have the value of the token that we generated previously at the pipeline level.

This indicates that you want to trigger the webhook on a push activity for all branches of the repository.

Configuring the GitLab webhook

Click the image to enlarge.

If you followed my tutorial on installing GitLab, you should be able to check the box Enable SSL verification. If you are using a custom CA to secure your Jenkins server's URL and that certificate is not recognized by GitLab, you must uncheck the corresponding box.

Configuring the GitLab webhook

Click the image to enlarge.

All configuration is now complete.

Testing the webhook

Click the image to enlarge.

Testing the webhook

Click the image to enlarge.

Testing and validation

To test everything, simply push a file into the demo app repository.

Testing the webhook by pushing a file

Click the image to enlarge.

Testing the webhook by pushing a file

Click the image to enlarge.

If everything goes as planned, the pipeline should trigger automatically, displaying the name of the person who performed the push to the repository.

Test result

Click the image to enlarge.

Test result

Click the image to enlarge.

Test result

Click the image to enlarge.

Conclusion

This dual integration between Jenkins and GitLab allows you to both trigger a pipeline on a push event in the repository, but also dynamically retrieve the pipeline code with each execution.

We have not used anything exotic, relying only on the native integration features of both products. Of course, it is important to maintain consistency between the version of GitLab, the GitLab plugin for Jenkins, and Jenkins itself. Do not forget the authentication information that links the solutions together, considering password changes and token expirations.

To simplify maintenance, it is always a good idea to keep documentation up-to-date and have an interaction diagram. This is not a configuration you need to redo every day. You can quickly forget how it works. When something stops working, you need to be able to remember the logic that was implemented.

The building blocks are now in place to begin more advanced automation around a specific use case. You can see an illustration of the possibilities offered via this article.