When implementing a CI/CD pipeline with Jenkins, an important decision is which factor should trigger the build for a specific job. The most common practice is to use a Git plugin with Jenkins and when a developer commits and pushes the code, the push action on the repository will trigger the job build. But there is an alternative, where some companies have a requirement that when a new Git Tag is created, that action alone should trigger the job build, without committing any code. We will now see the steps needed to configure Jenkins and a Bitbucket project in our example.
Configure Bitbucket and create a Webhook to Jenkins
We assume that we have a Jenkins server with a configured build pipeline job. This job executes the build.groovy script from a Bitbucket project. The first step for configuring Bitbucket is to install the Webhook to Jenkins for Bitbucket plugin, which is available on the Atlassian Markeplace from the Bitbucket administration panel. This plugin allows our SCM to send notifications to Jenkins about code commits and merges. The second step is to go to the specific project page settings in Bitbucket and create a Webhook to Jenkins with the following actions:
- fill in the Jenkins URL
- select the “Git Plugin” as the “Jenkins endpoint”
- select the “Repo Clone URL” to point to our project
We can check the configuration by clicking on Trigger Jenkins. We should see a successful message and then save the configuration.
Configure the Jenkins project
There are two changes that need to be applied in the Jenkins server. We should enable the Poll SCM build trigger in our pipeline job setting and enter a Schedule. This way, Jenkins will be able to check our project in Bitbucket for any changes. Then in the SCM section on the pipeline job configuration panel, we can enter the following settings:
- Refspec: +refs/tags/*:refs/remotes/origin/tags/*
- Branch: specifier **/tags/**
That’s all, the configuration is done and now it is time to check if this is working.
Test the configuration
By navigating to our project in Bitbucket, we can easily create a new Git Tag in the commit section, thus simulating a situation where developers create new Tags in their workflow. In Jenkins we should see that a new build job has been triggered, even though no code has been committed and pushed to the repository.
In this post we saw how to configure Bitbucket by installing a plugin and creating a Webhook from an existing project to our Jenkins server. Then we configured the pipeline job in Jenkins to be triggered when a new Git Tag is created. Deciding on the factors which will trigger a build job is not a generic process and should depend on the specific CI/CD implementation and the objectives we are trying to achieve.