When working on continuous integration and deployment processes for DevOps solution development, a code repository must have an integration with build automation and CI/CD tool. Keeping that under consideration, in this article, we are going to mention steps to effectively integrate Atlassian product Bitbucket with Jenkins.
This integration ensures that whenever a code is altered in the Bitbucket Repo, it will automatically reflect in the Jenkins by creating a new build process.
Now, let’s get started with the steps to process Bitbucket and Jenkins integration.
Step 1: Creating A Repo with Bitbucket
At first, create a Bitbucket account (if you don’t have any) using a given link.
Now, log in to the Bitbucket account and click on the Create repository button as shown in the following image.
Click on the (+) sign present on the left side of the Bitbucket home page to create a new project/ repository. Give it a name, whichever suits your project. Here, we are going to name it a DevOps demo and include a README.md file.
Once all details are filled in, click on the Create repository.
Now, your new Bitbucket Repo is ready.
Step 2: Installing a Bitbucket Plugin in Jenkins
Log into your Jenkins server and follow the path Dashboard > Manage Jenkins.
Click on Manage Jenkins and then on the Manage Plugins.
Manage Plugin section will navigate you to the search field, where you can find the Bitbucket plugin.
Select the Bitbucket plugin and click to Install without restart.
Once the installation process is done, you can go to the installed section, where you can find your Bitbucket plugin installed.
Now, add the credentials of your Bitbucket account to Jenkins if you’re using a private Bitbucket Repo, which is highly recommended.
Step 3: Creating a New Job in Jenkins and Connecting it to A Private Bitbucket Repo
Go to the Jenkins dashboard and click on the New Item present on the top for creating a new Jenkins Job.
Now give a name to the Jenkins Job. Here, we are naming it as devops-job.
Once the naming is done, click on the FreeStyle Project, and then on the OK button to finish the Jenkins job creation.
Once the job is created, go to its Settings, and then Source Code Management.
Add the URL of your Bitbucket Repo in the Repository URL section
Click on the Add button with the dropdown menu, which is present just on the right side of the credential box, in which you’ll have to click on the Jenkins to add/link the Bitbucket username and password.
At last, click on the Add repository button.
Go to the Build Trigger select the Build when a change is pushed to Bitbucket checkbox. Once this is done, you can see the Save and Apply pop-up, where click on the Save button.
Now, your Bitbucket and Jenkins accounts are linked.
Step 4: Pushing Code to Jenkins After New Code Commits Done Using Bitbucket Webhooks
Open your Bitbucket account and go to the Repository Setting page of the recently created Bitbucket Repo.
In the Repository Settings, you’ll find a Webhooks section, click on it and then on the Add Webhook to add a new webhook. Give this new Webhook a name and add the URL of your Jenkins account in the URL field as below.
URL Syntax: IP-address/bitbucket-hook/
Once it is added, select the fields as shown in the below image.
And finally, click on the Save button and complete the integration of Bitbucket with Jenkins. Now, you’re all set to automate the code changes reflection from Bitbucket to Jenkins.
To ensure the integration of Bitbucket and Jenkins, you can use the following steps.
Step 5: Testing Bitbucket and Jenkins Integration
Go to the Jenkins > devops-job and click on the Build Now option.
Go to the Build History and then Console Output to check the output.
If the output doesn’t show any error, the build will reflect the red colour, which shows a successful integration of Jenkins with Bitbucket. Thus, whenever a new code commits from Bitbucket, the Jenkins job will create a new build automatically.
Jenkins and Bitbucket integration success can also be checked from the Bitbucket.
Go to your Bitbucket Repo, which you’ve recently created, and add a new file by clicking on the Add file button as shown in the image to check whether the changes in the file done through Bitbucket reflect in the form of build creation in Jenkins or not.
(For this testing, a terminal can also be used once Bitbucket Repo cloning is done using Git commands.)
Here, we are creating a simple PHP file. Once the file is created, click on the Commit button.
Now, go to the Jenkins and check for the build history, whether the new build has triggered or not.
Also, check for the Workspace files.
If you find a new build and workspace file, consider that your Bitbucket and Jenkins are successfully integrated.
Used the same process but got stuck somewhere in your DevOps project? Let us know your doubts and queries related to your Jenkins project.