Operations 4 min read

Integrating Jenkins with GitLab API: Configuration, Groovy Wrapper, and Common Endpoints

This article explains how to integrate Jenkins with GitLab by using the HTTP Request plugin to send API calls, configure pipeline steps, and provides sample Groovy code and a summary of common GitLab API endpoints for operations such as project, branch, tag, and pipeline management.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Integrating Jenkins with GitLab API: Configuration, Groovy Wrapper, and Common Endpoints

In continuous integration, the project typically uses GitLab for source control, and Jenkins pipelines need to interact with GitLab for tasks such as reporting build status, creating tags, and retrieving configuration files.

To enable this, install the “HTTP Request” plugin in Jenkins, then generate a Jenkinsfile via the pipeline syntax. The main fields to fill are the API endpoint URL, request method, data format, and authentication (user/password or token). Credentials for the GitLab user must be created in Jenkins beforehand.

Example of a raw HTTP request step:

httpRequest acceptType: 'APPLICATION_JSON_UTF8', authentication: '24982560-17fc-4589-819b-bc5bea89da77', contentType: 'APPLICATION_JSON_UTF8', responseHandle: 'NONE', url: 'http://gitlab.demo.com/api/v4/xxxx'

A reusable Groovy wrapper can be defined in a shared library:

package org.demo

def HttpReq(reqUrl, reqMode, reqBody) {
    response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',
                           authentication: '24982560-17fc-4589-819b-bc5bea89da77',
                           contentType: 'APPLICATION_JSON_UTF8',
                           url: 'http://gitlab.demo.com/api/v4/xxxx',
                           requestBody: "{\"aa\":\"bb\",\"cc\":\"dd\"}"
    response = readJSON text: "${response.content}"
    return response
}

The article also lists frequently used GitLab API categories, including group management, project management, branch operations, tag creation, pipeline control, repository file handling, commit retrieval, and merge request creation, with links to the official GitLab CE API documentation.

In summary, while many GitLab interactions can be handled by existing Jenkins plugins, custom API calls are sometimes necessary, and the provided Groovy function demonstrates a simple way to encapsulate those requests.

ci/cddevopsGitLabGroovyJenkinsHTTP request
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.