0
0
JenkinsHow-ToBeginner · 4 min read

How to Integrate Bitbucket with Jenkins for CI/CD

To integrate Bitbucket with Jenkins, install the Bitbucket plugin in Jenkins, create a Jenkins job linked to your Bitbucket repository, and configure a webhook in Bitbucket to trigger Jenkins builds on code changes. This setup automates your build process whenever you push code to Bitbucket.
📐

Syntax

The integration involves three main parts:

  • Jenkins Bitbucket Plugin: Installs in Jenkins to enable Bitbucket communication.
  • Jenkins Job Configuration: Defines the repository URL and build triggers.
  • Bitbucket Webhook: Sends HTTP POST requests to Jenkins when code changes occur.
groovy
pipeline {
    agent any
    triggers {
        bitbucketPush()
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building project from Bitbucket repository'
            }
        }
    }
}
💻

Example

This example shows a Jenkins Pipeline job configured to trigger on Bitbucket pushes using the Bitbucket webhook.

groovy
pipeline {
    agent any
    triggers {
        bitbucketPush()
    }
    stages {
        stage('Checkout') {
            steps {
                git url: 'https://bitbucket.org/yourusername/yourrepo.git', branch: 'main'
            }
        }
        stage('Build') {
            steps {
                echo 'Running build steps...'
            }
        }
    }
}
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://bitbucket.org/yourusername/yourrepo.git [Pipeline] } [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Running build steps... [Pipeline] } [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline
⚠️

Common Pitfalls

  • Missing Bitbucket Plugin: Jenkins won't recognize Bitbucket triggers without the plugin.
  • Incorrect Webhook URL: The webhook URL must point to http://your-jenkins-url/bitbucket-hook/.
  • Insufficient Permissions: Jenkins needs access rights to the Bitbucket repository.
  • Firewall or Network Issues: Bitbucket must reach Jenkins server over the network.
text
Wrong webhook URL example:
https://bitbucket.org/yourusername/yourrepo/settings/hooks
Webhook URL set to: http://wrong-url/bitbucket-hook/

Correct webhook URL example:
http://your-jenkins-url/bitbucket-hook/
📊

Quick Reference

Steps to integrate Bitbucket with Jenkins:

  • Install Bitbucket Plugin in Jenkins.
  • Create a Jenkins job with your Bitbucket repo URL.
  • Configure the job to trigger on Bitbucket push events.
  • Set up a webhook in Bitbucket pointing to http://your-jenkins-url/bitbucket-hook/.
  • Test by pushing code to Bitbucket and watch Jenkins build start automatically.

Key Takeaways

Install the Bitbucket plugin in Jenkins to enable integration features.
Configure Jenkins jobs with your Bitbucket repository URL and enable Bitbucket push triggers.
Set up a webhook in Bitbucket pointing to Jenkins to trigger builds on code changes.
Ensure Jenkins has network access and permissions to your Bitbucket repository.
Test integration by pushing code and verifying Jenkins starts the build automatically.