Operations 6 min read

Jenkins System Integration Overview: Automating GitLab Branch Creation with Pipelines and Shared Libraries

This tutorial explains how to use Jenkins to integrate command‑line tools and webhooks for automating GitLab branch creation, covering API discovery, Postman testing, pipeline scripting, credential handling, shared library implementation, and final system configuration.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Jenkins System Integration Overview: Automating GitLab Branch Creation with Pipelines and Shared Libraries

Jenkins System Integration Overview

Jenkins is commonly used to integrate command‑line tools and scripts, often by executing shell commands or using webhooks to call external APIs.

Course Practice Goal

Create a Jenkins job that automatically creates a GitLab project branch based on user input.

Typical Integration Steps

Obtain the API documentation of the target system.

Select the required API endpoints.

Test the endpoints with Postman or cURL.

Encapsulate the API calls in a Jenkins pipeline or shared library.

Postman Debugging of GitLab API

Reference the official GitLab documentation and locate the branch‑management API.

curl --request POST \
  --header "PRIVATE-TOKEN: t74bnVEaxHAhUfCKkomH"  \
  "http://192.168.1.200/api/v4/projects/2/repository/branches?branch=mynewbranch&ref=master"

The PRIVATE-TOKEN value is obtained from the GitLab user settings.

Writing the Jenkinsfile

First, place the raw cURL command directly into a Jenkins pipeline.

pipeline {
  agent { label "master" }
  stages {
    stage("test") {
      steps {
        echo "Hello "
        script {
          // Create Branch
          sh """
            curl --location \
              --request POST \
              'http://192.168.1.200/api/v4/projects/2/repository/branches?branch=${env.branchName}&ref=${env.baseBranch}' \
              --header 'PRIVATE-TOKEN: t74bnVEaxHAhUfCKkomH'
          """
        }
      }
    }
  }
}

Improvements include retrieving branch, project, and base‑branch names from the Jenkins UI and storing the GitLab token securely using withCredentials.

Encapsulating HTTP Requests

def HttpReq(apiURL, method){
    gitlabURL = "http://192.168.1.200/api/v4"
    withCredentials([string(credentialsId: 'b0e34f3a-4ecb-4298-8e5f-2cbfd2c1f571', variable: 'GITLAB_TOKEN')]) {
        response = sh returnStdout: true, script: """
            curl --location \
                --request ${method} \
                "${gitlabURL}/${apiURL}" \
                --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"
        """
    }
    return response
}

// Create branch
def CreateBranch(projectId, branchName, baseBranch){
    apiURL = "projects/${projectId}/repository/branches?branch=${branchName}&ref=${baseBranch}"
    HttpReq(apiURL, "POST")
}

// Get project ID
def GetProjectId(projectName){
    apiURL = "projects?search=${projectName}"
    result = HttpReq(apiURL, "GET")
    result = readJSON text: result
    return result[0]["id"]
}

Using a Shared Library

Place the above functions in a shared library (e.g., gitlab.groovy) and import it in the Jenkinsfile.

@Library("jenkinslib@main") _

// Import gitlab.groovy
def gitlab = new org.devops.gitlab()

pipeline {
    agent { label "master" }
    stages {
        stage("test") {
            steps {
                echo "Hello "
                script {
                    projectId = gitlab.GetProjectId("${env.projectName}")
                    gitlab.CreateBranch(projectId, "${env.branchName}", "${env.baseBranch}")
                }
            }
        }
    }
}

Jenkins System Settings

Configure Jenkins credentials, global tool settings, and any required plugins to enable the pipeline to run successfully.

Final Result

The pipeline creates a new branch in the specified GitLab project automatically, demonstrating end‑to‑end DevOps automation.

Course Information

Replay link: https://www.idevops.site/detail/l_61128c62e4b0a27d0e3bd3c6/4 Bilibili channel: https://space.bilibili.com/475079413

About the instructor: Ze Yang, a DevOps practitioner focusing on enterprise‑level operations development, sharing practical Linux and DevOps techniques.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdAutomationDevOpsGitLabPipelineGroovyJenkins
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

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.