Operations 8 min read

Using Jenkins to Interact with GitLab API: Creating Branches via HttpRequest Plugin

This article demonstrates how to extend a Jenkins CI/CD pipeline to communicate with external systems, specifically showing how to create a GitLab branch by researching the GitLab API, installing the HttpRequest plugin, handling credentials securely, and encapsulating HTTP calls in reusable Groovy functions.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Using Jenkins to Interact with GitLab API: Creating Branches via HttpRequest Plugin

After implementing a continuous delivery pipeline with Jenkins, you often need to interact with external systems such as code repositories, quality platforms, or requirement management tools; this guide explains how to achieve such interactions, using GitLab as an example.

First, a brief overview of Docker components is provided: docker-cli (the command‑line tool), docker-api (the interface that receives CLI requests), and docker-daemon (the long‑running service that manages images, containers, networks, and volumes). Understanding this interaction model helps illustrate how Jenkins scripts can control external APIs.

The first practical step is to study the GitLab API. By visiting GitLab API documentation , navigate to the Resources menu and then to Branches to locate the endpoint for creating a branch.

The branch‑creation endpoint provides the URL, required parameters, request template, and response template. These details are essential for constructing a proper HTTP request.

Instead of using raw curl commands, the article recommends the Jenkins HttpRequest plugin. After installing the plugin (search for "requests"), you can encapsulate the API call within a pipeline step.

curl --request POST --header "PRIVATE-TOKEN:
" "https://gitlab.example.com/api/v4/projects/5/repository/branches?branch=newbranch&ref=master"
# --request specifies the HTTP method
# --header adds the token header
# The URL is the API endpoint

In the Jenkinsfile, the httpRequest step is configured with the URL, HTTP method, custom headers, and content type:

httpRequest acceptType: 'APPLICATION_JSON_UTF8',
            contentType: 'APPLICATION_JSON_UTF8',
            customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: '${gitlabtoken}']],
            httpMode: 'POST',
            responseHandle: 'NONE',
            url: 'https://gitlab.example.com/api/v4/projects/5/repository/branches?branch=newbranch&ref=master',
            wrapAsMultipart: false

The token is stored securely in Jenkins credentials (type "Secure Text") and accessed via withCredentials :

withCredentials([string(credentialsId: 'your_token_id', variable: 'gitlabtoken')]) {
    httpRequest ...
}

Note that the token value must be wrapped in double quotes inside the customHeaders map so that the variable is correctly interpolated.

withCredentials([string(credentialsId: 'your_token_id', variable: 'gitlabtoken')]) {
    httpRequest customHeaders: [[maskValue: false,
                                 name: 'PRIVATE-TOKEN',
                                 value: "${gitlabtoken}"]],
                ...
}

To make the solution reusable, a helper method HttpReq is defined to perform generic HTTP calls, and a specific CreateBranch function builds the branch‑creation URL and invokes HttpReq :

// Encapsulate HTTP request
def HttpReq(reqType, reqUrl, reqBody) {
    def gitServer = "http://192.168.1.200:30088/api/v4"
    withCredentials([string(credentialsId: 'gitlab-token', variable: 'gitlabtoken')]) {
        result = httpRequest customHeaders: [[maskValue: true,
                                             name: 'PRIVATE-TOKEN',
                                             value: "${gitlabtoken}"]],
                             httpMode: reqType,
                             contentType: "APPLICATION_JSON",
                             consoleLogResponseBody: true,
                             ignoreSslErrors: true,
                             requestBody: reqBody,
                             url: "${gitServer}/${reqUrl}"
    }
    return result
}

// Create branch
def CreateBranch(projectId, refBranch, newBranch) {
    branchApi = "projects/${projectId}/repository/branches?branch=${newBranch}&ref=${refBranch}"
    response = HttpReq("POST", branchApi, '').content
    branchInfo = readJSON text: "${response}"
}

The final Jenkinsfile combines these utilities, using readJSON to parse the API response.

In summary, the interaction process is divided into three steps: researching the external system's API, encapsulating the HTTP request with the HttpRequest plugin, and parsing the response with readJSON . This approach can be extended to other systems such as Jira for requirement management.

Hope this guide helps you integrate external services into your Jenkins pipelines!

DockerCI/CDDevOpsGitLabpipelineJenkinshttpRequest
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.