Integrating GitLab Merge Request Webhooks with Jenkins Using the Generic Webhook Plugin
This guide demonstrates how to configure GitLab and Jenkins to trigger a pipeline automatically after a merge request is merged, using Jenkins' Generic Webhook plugin, including job setup, webhook URL generation, GitLab project configuration, data analysis, and pipeline script examples.
In this tutorial we show how to integrate GitLab merge request events with Jenkins pipelines using the Generic Webhook plugin, enabling automatic pipeline execution after code merges.
Jenkins job configuration : enable the Generic Webhook trigger, turn on debug information, set a unique token (typically the job name), and obtain the trigger URL such as http://192.168.1.200:8080/generic-webhook-trigger/invoke?token=devops-merge-trigger .
GitLab project setup : create a project, then add a webhook for the “Merge request” event pointing to the Jenkins trigger URL.
Data analysis : after creating a merge request, inspect the payload. Example JSON fields for approved and merged states are shown, along with source and target branch extraction.
# approved state
$.event_type = merge_request
$.object_attributes.state = opened
$.object_attributes.action = approved
# merge state
$.event_type = merge_request
$.object_attributes.state = merged
$.object_attributes.action = merge
# source and target branches
$.object_attributes.source_branch
$.object_attributes.target_branchPipeline script : in the Jenkinsfile we define a GenericTrigger with variables mapping to the webhook payload (event_type, state, action, source_branch, target_branch), enable printing of contributed variables and post content, and add a simple stage to verify the trigger.
currentBuild.description = "Trigger: ${source_branch} > ${target_branch}\n${event_type} \n ${state} \n ${action}"
pipeline {
agent any
triggers {
GenericTrigger(
causeString: 'Generic Cause',
genericVariables: [
[defaultValue: '', key: 'event_type', regexpFilter: '', value: '$.event_type'],
[defaultValue: 'NULL', key: 'state', regexpFilter: '', value: '$.object_attributes.state'],
[defaultValue: 'NULL', key: 'action', regexpFilter: '', value: '$.object_attributes.action'],
[defaultValue: 'NULL', key: 'source_branch', regexpFilter: '', value: '$.object_attributes.source_branch'],
[defaultValue: 'NULL', key: 'target_branch', regexpFilter: '', value: '$.object_attributes.target_branch']
],
printContributedVariables: true,
printPostContent: true,
regexpFilterExpression: '^merge_request\\smerged\\smerge$',
regexpFilterText: '$event_type $state $action',
token: 'devops-merge-trigger',
tokenCredentialId: ''
)
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}After saving the job and merging a request in GitLab, the webhook fires, the pipeline runs, and the build description displays the source and target branches along with the event details, confirming a successful integration.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.