Automatically Building Jenkins Jobs with GitLab Webhooks

1

If you have ever played around with Jenkins, I’m sure you have realized there are a tremendous amount of plugins out there.  However, I haven’t successfully found a list of decent tutorials for doing the most basic things within Jenkins. Today we explore how we can automatically cause a Jenkins job to build when code is pushed to our git repository hosted in GitLab. Begin by logging into your Jenkins instance and navigating to Manage Plugins under Manage Jenkins.  Select the Available tab and install the Gitlab Hook Plugin.

22

If you haven’t created one yet, create a new project by returning to the main page and selecting New Item from the navigation panel on the left.  The most common project and easiest type to use isFreestyle Project so start there.  Scroll down to source control management and add the url of your git repository along with any needed credentials for accessing the environment (Note: We highly recommend making a jenkins user in your gitlab environment with read-only access to your repository for security reasons).

33

Next add the steps you would like to occur during your build phase.  Scroll down to the build section and select Add build step.  This step will entirely depend on your environment and what you want your job to actually do.  For our example, we are using a freestyle project to build and unit test a java project using maven (Note: This required us to install maven and the maven jenkins plugin).  After you complete setting up your build steps, save your project.

44

For the last step, we’re going to add a custom webhook to our git repository in GitLab.  Navigate to your gitlab project instance, select the cog icon in the right corner and choose webhooks.  Assuming your Jenkins instance isn’t hosted on ssl, uncheck the Enable SSL Verification item listed at the bottom of the displayed page.  In the url field, type http://<jenkinsDomain>/jenkins/gitlab/build_now/<projectName> and be sure to replace each of the fields in brackets.  Hit the Add Webhook button at the bottom of the page to complete the process.  You can now successfully test that everything is setup right by hitting the Test button next to the newly created webhook.  Returning to your jenkins instance will show that your project is currently building.  That’s It!  Interested in learning more about Jenkins or need more help with your CI/CD pipeline?  Drop us a line today.

55

Filled under: Uncategorized