How to Integrate GitLab with Jenkins for CI/CD
To integrate
GitLab with Jenkins, install the GitLab plugin in Jenkins, create a Jenkins job linked to your GitLab repository, and configure a webhook in GitLab to trigger Jenkins builds on code changes. This setup automates your CI/CD pipeline by connecting GitLab commits to Jenkins jobs.Syntax
The integration involves these key parts:
- GitLab Plugin: Jenkins plugin to connect with GitLab.
- Jenkins Job: A pipeline or freestyle job configured to pull code from GitLab.
- GitLab Webhook: URL in GitLab that triggers Jenkins on push or merge events.
Each part works together to automate builds when code changes.
yaml
gitlab: webhook_url: http://<jenkins-server>/project/<job-name> jenkins: plugins: - gitlab job: scm: git repository_url: https://gitlab.com/your-repo.git triggers: - gitlab webhook
Example
This example shows how to create a Jenkins pipeline job triggered by GitLab webhook:
groovy
pipeline {
agent any
triggers {
GenericTrigger(
genericVariables: [
[key: 'ref', value: '$.ref']
],
causeString: 'Triggered by GitLab push',
token: 'your-webhook-token',
printContributedVariables: true,
printPostContent: true,
regexpFilterText: '$.ref',
regexpFilterExpression: 'refs/heads/main'
)
}
stages {
stage('Checkout') {
steps {
git url: 'https://gitlab.com/your-repo.git', branch: 'main'
}
}
stage('Build') {
steps {
echo 'Building the project...'
}
}
}
}Output
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/your-job
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] git
Cloning repository https://gitlab.com/your-repo.git
[Pipeline] }
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Building the project...
[Pipeline] }
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Common Pitfalls
- Missing or incorrect webhook URL: Ensure the webhook URL in GitLab matches Jenkins job URL exactly.
- Webhook token mismatch: The token set in Jenkins GenericTrigger must match the one in GitLab webhook settings.
- Insufficient Jenkins permissions: Jenkins user must have access to the GitLab repository.
- Firewall or network issues: GitLab must be able to reach Jenkins server URL.
text
Wrong webhook URL example: https://jenkins-server/wrong-path Correct webhook URL example: https://jenkins-server/project/your-job Wrong token in Jenkins: GenericTrigger(token: 'wrong-token') Correct token in Jenkins: GenericTrigger(token: 'your-webhook-token')
Quick Reference
Summary tips for GitLab and Jenkins integration:
- Install and configure GitLab plugin in Jenkins.
- Create Jenkins job with Git SCM pointing to GitLab repo.
- Set up GitLab webhook to Jenkins job URL with correct token.
- Test webhook delivery from GitLab to Jenkins.
- Check Jenkins logs for webhook trigger confirmation.
Key Takeaways
Install the GitLab plugin in Jenkins to enable integration features.
Configure a Jenkins job to pull code from your GitLab repository.
Set up a GitLab webhook pointing to Jenkins job URL to trigger builds automatically.
Ensure webhook tokens and URLs match exactly to avoid trigger failures.
Test the webhook connection and monitor Jenkins logs for successful triggers.