How to Configure GitLab Webhook in Jenkins

By: Published: October 13, 2021

GitLab Webhooks Jenkins_FI

Continuous Integration is a DevOps Software Development practice in which the developers merge their code changes into a central repository on a regular basis, after which Automated Builds and Tests are run. Jenkins is an Open-Source Continuous Integration (CI) tool that is extensively used by developers to automate the testing and deployment of their applications. GitLab is the leading integrated product for the entire software development life cycle. Even though GitLab has a CI feature, many people choose to use Jenkins alongside GitLab since it is more extensible, has more capabilities, and has a simpler user interface.

In this article, you will gain information about GitLab Webhook Jenkins Integration. You will also gain a holistic understanding of GitLab, Jenkins, its key features, Webhooks, and the steps involved in setting up GitLab Webhook Jenkins Integration. Read along to find out in-depth information about GitLab Webhook Jenkins Integration.

Table of Contents

Introduction to GitLab

Gitlab Webhook Jenkins Integration -GitLab logo
Image Source

GitLab is a Web-Based Git repository that provides free open and private Repositories, Problem Tracking features, and Wikis. It is a comprehensive DevOps platform that enables professionals to seamlessly carry all their Project Tasks, from Project Planning and Source Code Control to Monitoring and Security. It also enables teams to work together, collaborate and develop better software.

GitLab helps teams in decreasing the life cycle of a project and increase productivity, which in turn creates added value for customers. The application does not require users to manage permissions for each tool. Once permissions are set, all members of the organization have access to all components. Users can also have access to some advanced features offered by GitLab by opting for their paid version which costs around $ 4 per month.

For further information on GitLab, you can follow the Official Documentation.

Introduction to Jenkins

GitLab Webhook Jenkins Integration - Jenkins Logo
Image Source

Jenkins is an Open-Source Automation Tool written in Java that includes plugins for Continuous Integration. It enables you to automate the execution of a series of actions to achieve the Continuous Integration process. Jenkins is used to build and test your software projects continuously, making it easier for developers to incorporate changes to the project and for users to leverage a fresh build. It also enables you to deliver software on a continuous basis by integrating with a wide range of testing and deployment technologies.

Organizations can use Jenkins to automate and speed up the software development process. Jenkins manages and controls software delivery processes throughout the entire Development Lifecycle, including Build, Documentation, Testing, Packaging, Staging, Deployment, Static Code Analysis, and much more.

For further information on Jenkins, you can follow the Official Documentation.

Key Features of Jenkins

Some of the developer-centric features offered by Jenkins are as follows:

  • Easy to Install and Configure: Jenkins is a self-contained Java Program, hence it does not depend on the platform on which it is installed. It is available as both – a standard installer and a .war file. Thanks to its minimal Web User Interface, it is easy to configure once the installation is complete.
  • Open Source: Jenkins is an Open-Source tool and is completely free to use. The strong involvement of the Open-Source community makes it a powerful Continuous Integration tool.
  • Easy Distribution: Jenkins is designed in such a manner that makes it relatively simple to distribute work across multiple machines and platforms for faster Build, Testing, and Deployment.
  • Plugin Ecosystem: The community is the backbone of Jenkins, and members of the community have been instrumental in the development (and testing) of close to 1500+ plugins available in the Update Center.

Introduction to Webhooks

GitLab Webhook Jenkins Integration - Webhook
Image Source

A Webhook, also known as a web callback or HTTP push API, is a method for an application to deliver real-time information to other applications. It is sometimes referred to as “Reverse APIs,” although in reality, it allows users to skip a step involved in the API concept. Most APIs involve a request followed by a response. Whereas, no request is required for a webhook; it just delivers data to the applications when it is available. Webhooks become considerably more efficient as a result, for both provider and consumer.

To leverage a webhook, initially you register a URL with the firm that provides the service. That URL is a location within your application that will accept the data and process it. In some cases, you can inform the supplier when you’d want to receive data. Whenever there’s something new, the webhook will send it to your URL. They’re quite useful and have a low-resource approach to handle event reactions.

The sole disadvantage of webhooks is the difficulty in setting them up at the beginning. For the webhook to function, you must create an API. The webhook will send an HTTP request to your app (usually a POST), and you will then be charged for interpreting it.

Simplify ETL and Data Analysis with Hevo’s No-code Data Pipeline

A fully managed No-code Data Pipeline platform like Hevo Data helps you integrate and load data from 100+ different sources (including 40+ Free Sources ) to a Data Warehouse or Destination of your choice in real-time in an effortless manner. Hevo with its minimal learning curve can be set up in just a few minutes allowing the users to load data without having to compromise performance. In addition to the 100+ data sources, Hevo also allows integrating data from non-native sources using Hevo’s in-built Webhooks Connector.

Get Started with Hevo for Free

Its strong integration with umpteenth sources allows users to bring in data of different kinds in a smooth fashion without having to code a single line. 

Check out some of the cool features of Hevo:

  • Completely Automated: The Hevo platform can be set up in just a few minutes and requires minimal maintenance.
  • Connectors: Hevo supports 100+ Integrations to SaaS platforms, Files, Databases, BI tools, and Native REST API & Webhooks Connectors. It supports various destinations including Google BigQuery, Amazon Redshift, Snowflake Data Warehouses; Amazon S3 Data Lakes; and MySQL, SQL Server, TokuDB, DynamoDB, PostgreSQL Databases to name a few.  
  • Real-Time Data Transfer: Hevo provides real-time data migration, so you can have analysis-ready data always.
  • 100% Complete & Accurate Data Transfer: Hevo’s robust infrastructure ensures reliable data transfer with zero data loss.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ sources (Including 40+ Free Sources ) that can help you scale your data infrastructure as required.
  • 24/7 Live Support: The Hevo team is available round the clock to extend exceptional support to you through chat, email, and support calls.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  • Live Monitoring: Hevo allows you to monitor the data flow so you can check where your data is at a particular point in time.
Sign up here for a 14-Day Free Trial!

Requirements for GitLab Webhook Jenkins Integration

  • Jenkins GitLab Plugin
    Thi plugin allows to build trigger that allows GitLab to trigger Jenkins builds when code is pushed or a merge request is created.
  • Jenkins Git Plugin
    This plugin enables use of Git as a build SCM, including repository browsers for numerous providers.
  • Jenkins GitLab Hook plugin
    This plugin allows GitLab webhooks to be used to trigger SMC polling on GitLab projects.

Use of GitLab Webhook Jenkins Integration

The Jenkins integrations with GitLab (webhook) can be implemented when:

  • You intend to migrate your CI from Jenkins to GitLab CI/CD in the future, but you require a temporary solution.
  • You’ve invested in Jenkins Plugins and have decided to continue using Jenkins to create your apps.

Setting up Gitlab Webhook Jenkins Integration

A) Grant Jenkins Access to GitLab Project

Before setting up GitLab Webhook Jenkins Integration, it is required to Grant a GitLab user access to the GitLab projects.

  • Create a new GitLab user or choose an existing one. Jenkins uses this account to access GitLab projects. It is recommended to create a GitLab user for this purpose only. If you use someone else’s account and that person’s account is deactivated or deleted, the GitLab-Jenkins integration will stop working.
  • Give the user access to the GitLab projects. Consider giving the user the global Administrator position if you’re integrating Jenkins with numerous GitLab projects. Otherwise, add the user to each project and assign the Developer role to them.

B) Configure GitLab API Access

Create a personal access token to authorize Jenkins’ access to GitLab.

  • Step 1: Sign in to GitLab as the same user to be used with Jenkins.
  • Step 2: In the top-right corner, select your avatar.
  • Step 3: Select the “Edit profile” option.
  • Step 4: On the left navigation pane, select the “Access Tokens” option.
  • Step 5: Check the “API Scope” checkbox and create a personal access token.
  • Step 6: Record the personal access token’s value, because it’s required in configuring the Jenkins server.

C) Configure the Jenkins Server

Install and configure the required Jenkins plugins. The plugin must be installed and configured to authorize the connection to GitLab.

  • Step 1: On the Jenkins server, Select the “Manage Jenkinsoption in the left navigation pane. Then click the Manage Plugins” option.
  • Step 2: Install the Jenkins GitLab Plugin, Jenkins Git Plugin and Jenkins GitLab Hook Plugin.
  • Step 3: Now select the Manage Jenkinsoption. Then select theConfigure System” option.
  • Step 4: In the “GitLab” section, check the “Enable authentication for ‘/project’ end-point” checkbox.
  • Step 5: Under the above checkbox, provide the Connection name.
  • Step 6: Enter the GitLab server’s URL in the “GitLab host URL” field.
  • Step 7: Click the “Add” button, then choose “Jenkins Credential Provider“.
  • Step 8: Choose the “GitLab API token” as the token type.
  • Step 9: Enter the GitLab personal access token’s value in the “API Token” field and click the “Add” button.
  • Step 10: Click on the “Test Connection” button.
  • Step 11: On receiving a success message, you can be ensured that the connection is successful.
  • Step 12: Click the “Save” button.

D) Configure the Jenkins project

Before implementing GitLab Webhook Jenkins Integration, you need to set up the Jenkins project where you want to execute your build.

  • Step 1: Go to the Jenkins Dashboard.
  • Step 2: In the left navigation pane, select the “New item” option.
  • Step 3: Assign a project’s name according to your choice.
  • Step 4: Choose between “Freestyle” or “Pipeline” projects. Then click on “OK“. (Because the Jenkins plugin changes the build status on GitLab, it is recommended to select a Freestyle project. You must configure a script in a Pipeline project to update the status on GitLab.)
  • Step 5: Write the description of your project as per your requirements.
  • Step 6: Choose your GitLab connection from the dropdown.
  • Step 7: Scroll down and in the “Source Code Management” section, check the “Git” checkbox.
  • Step 8: Choose the repository branch if you have any.
  • Step 9: In the repository browser section under the “Git” checkbox, choose “GitLab” from the dropdown menu. Paste the link of the repository browser and set your version accordingly.
  • Step 10: Copy the repository link of your project from GitLab and paste it in the Repository URL box in the Repository section under the “Git” checkbox.
  • Step 11: Scroll down to the “Build Triggers” section.
  • Step 12: Check the “Build when a change is pushed to GitLab” checkbox. Copy the webhook URL written alongside the checkbox.
  • Step 13: Check the following checkboxes:
    • Accepted Merge Request Events
    • Closed Merge Request Events
  • Step 14: Specify how to build status is reported to GitLab: If you created a Freestyle project, in the “Post-build Actions” section, choose “Publish build status to GitLab“.
  • Step 15: If you created a Pipeline project, you must use a Jenkins Pipeline script to update the status on GitLab.

Example of a Jenkins Pipeline Script:

pipeline {
    agent any

    stages {
       stage('gitlab') {
          steps {
             echo 'Notify GitLab'
             updateGitlabCommitStatus name: 'build', state: 'pending'
             updateGitlabCommitStatus name: 'build', state: 'success'
          }
       }
    }
 }
  • Click on the “Save” button.

Since there are no builds currently, so you would need to pull the code using webhooks.

E) Configure the GitLab Project

This step can be implemented in 2 ways:

1) Recommended Jenkins Integration

  • Step 1: Create a new GitLab project or select an existing one.
  • Step 2: Now, scroll to the bottom of the left navigation pane and select the “Settings option. Then click on theIntegrations” option.
  • Step 3: Integration Settings” Dashboard appears.
  • Step 4: Scroll down to the list of Project Services. Click on the “Jenkins CI” option.
  • Step 5: Jenkins CI” window appears. Turn on the “Active” toggle.
  • Step 6: Select the events you want GitLab to trigger a Jenkins build for:
    • Push
    • Merge request
    • Tag push
  • Step 7: Enter the Jenkins URL. (Make sure you have a public IP address such that your URL is exposed to the Internet instead of being private.)
GitLab Webhook Jenkins Integration - Jenkins URL
Image Source: Self
GitLab Webhook Jenkins Integration - Jenkins URL
Image Source: Self
  • Step 8: Enter the name of your project in Jenkins in the “Project name” field. (The project name should be URL-friendly, with spaces replaced with underscores. To assure the project name is correct, copy it from your browser’s URL bar while browsing the Jenkins project.)
  • Step 9: Enter the Username and Password if your Jenkins server requires authentication.
  • Step 10: Click on the “Test settings and save changes” button. GitLab tests the connection to Jenkins.

The Jenkins CI is activated now.

2) Webhook Integration

  • Step 1: In the left navigation pane, select the “Settings” option. Then click on the “Webhooks” option.
GitLab Webhook Jenkins Integration - Webhooks option
Image Source: Self
  • Step 2: Now, in the Integration settings window, under the “Integrations” section, select the “Webhook” hyperlink.
  • Step 3: In the “Webhook Settings” window, under the “Webhooks” section, paste the webhook URL (such as https://JENKINS_URL/project/YOUR_JOB) which you have copied in Jenkins server.
GitLab Webhook Jenkins Integration - Webhooks
Image Source: Self
  • Step 4: Now, regarding the secret token, go to Jenkins and generate the secret token.
Secret Token Generation

A) First Method:

  • Step 1: On the top left, click on the “admin” button and then select “Configure” from the drop-down menu.
GitLab Webhook Jenkins Integration - Configure option
Image Source: Self
  • Step 2: In the “API Token” section, click the “Add New Token” button.
  • Step 3: Give a name to the token.
  • Step 4: Now, Click the “Generate” button. Copy the token name immediately since it cannot be recovered in the future.

B) Second Method:

  • Step 1: Go the the “Settings” of your Jenkins project.
  • Step 2: Go to the “Build Triggers” section.
  • Step 3: Under the “Build when a change is pushed to Gitlab” checkbox, click the “advanced” button.
  • Step 4: Click the “Generate” button under the “Secret Token” field.
  • Step 5: Copy the resulting token, and save the job configuration.

Now you can move back to the Jenkins server and continue implementing the following steps:

  • Step 5: Back in GitLab, paste the secret token which you have generated in the Jenkins server.
  • Step 6: Check the triggers as per your requirements.
  • Step 7: Now, click the “Add webhook” button.
  • Step 8: Click the “Test” button, and it should succeed.

For further information on implementing GitLab Webhook Jenkins Integration, visit here.

Conclusion

In this article, you have learned about GitLab Webhook Jenkins Integration. This article also provided information on GitLab, Jenkins, its key features, Webhooks, and steps involved in implementing GitLab Webhook Jenkins Integration.

Hevo Data, a No-code Data Pipeline provides you with a consistent and reliable solution to manage data transfer between a variety of sources and a wide variety of Desired Destinations with a few clicks.

Visit our Website to Explore Hevo

Hevo Data with its strong integration with 100+ data sources (including 40+ Free Sources) allows you to not only export data from your desired data sources & load it to the destination of your choice but also transform & enrich your data to make it analysis-ready. Hevo also allows integrating data from non-native sources using Hevo’s in-built Webhooks Connector. You can then focus on your key business needs and perform insightful analysis using BI tools. 

Want to give Hevo a try? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. You may also have a look at the amazing price, which will assist you in selecting the best plan for your requirements.

Share your experience of understanding GitLab Webhook Jenkins Integration in the comment section below! We would love to hear your thoughts.

Manisha Jena
Research Analyst, Hevo Data

Manisha is a data analyst with experience in diverse data tools like Snowflake, Google BigQuery, SQL, and Looker. She has hadns on experience in using data analytics stack for various problem solving through analysis. Manisha has written more than 100 articles on diverse topics related to data industry. Her quest for creative problem solving through technical content writing and the chance to help data practitioners with their day to day challenges keep her write more.

No-code Data Pipeline for your Data Warehouse