This guest post is by Vidhyadharan Deivamani, a Senior Engineering Specialist at Software AG.
Introduction
Working on a successfully committed revision always saves a great deal of your time and effort. In this article, we will see how to update build status of a specific commit in Bitbucket Server to let other users pick the correct revision. Also, we will see how to view the build status and pick the right build.
Overview
Developers can easily check the build status using a build badge. However, the build badge displays the status of a particular branch and it's not always feasible to check the status of each branch or all branches at once.
Now imagine in an Enterprise development workstream where multiple developers work on multiple branches. In such an environment, there are high chances that the build would break when working on HOT features.
Thus to save time and effort, developers must ensure that they work on builds that are successfully committed. This situation can be sorted using the Bitbucket build status API, which displays the status of builds. So, with the usage of Bitbucket build status API you can always ensure that the developer picks a build with GREEN status which indicates passing.
In this article we will discuss:
- What is a a continuous build Pipeline with Bitbucket server notifier?
- How to configure the Jenkins plugin – Bitbucket Server Notifier
- How to generate
notifybitbucket
pipeline script - How to add this new pipeline function
- How to view commit build status
What is a continuous build pipeline with Bitbucket server notifier?
In Jenkins, a pipeline is a group of events or jobs which are interconnected with one another in a sequence.
In a Jenkins pipeline, every job or event has some sort of dependency on at least one or more events. The picture below represents a build or compilation flow and continuous delivery pipeline in Jenkins.
In general, when a developer commits into a repository, a webhook trigger is initialized by Bitbucket to trigger the Jenkins pipeline job. The pipeline passes through different sequential stages such as, checkout, mark the commit as IN-PROGRESS, Compile/Build/pack. Finally, the commit of build is marked as FAILED/SUCCESS.
These stages are interlinked. Every stage has its events, which work in a sequence called a continuous delivery pipeline.
In the repository developer can see the status of continuous build, whether it be success, failure,
or in-progress.
The picture seen below displays three different branches with unique build status.
You can click each of the build statuses to view the corresponding Jenkins job.
How to configure Jenkins plugin – Bitbucket Server Notifier?
To add the build status in Jenkins pipeline, you must configure Bitbucket service notifier Jenkins plugin.
In Jenkins, go to plugin manager and install the Bitbucket Server Notifier plugin. The picture shown below displays the Installed Bitbucket Server Notifier plugin in Jenkins.
How to generate notifybitbucket
pipeline script?
After installing the Jenkins bitbucket server notifier plugin, generate the pipe scripts using pipeline syntax. The below steps explain how to generate notifier pipeline script:
- Go to the http://jenkins:8080/pipeline-syntax/
- From the Sample step drop-down list, select "notifyBitbucket Notify Stash instance".
- Click Advanced.
- Fill the following details:
- Stash URL: Your on-prem bitbucket URL.
- Credentials: Click Add and select Create app password from the drop-down list. This password is your bitbucket server app password/or your account password.
- Commit-SHA-1: The commit ID that will be replaced dynamically.
- Select the following checkboxes:
- Ignore unverified SSL certificates
- Keep repeated builds in Stash
- Consider UNSTABLE builds as SUCCESS notification"
- Click Generate Pipeline Script. The pipeline script is generated like shown in the above screenshot.
- Copy the generated notifyBitbuket script.
How to add this new pipeline function
The generated notifyBitbuket
script is abstract . You must create a parameterized function, so that the notifybitbucket
will be called to update the build status in Jenkins pipeline during each stage.
- Create a new function in your Jenkins file.
def notifyBitbucket(String state) {
if('SUCCESS' == state || 'FAILED' == state) {
// Set result of currentBuild !Important!
currentBuild.result = state
}
notifyBitbucket commitSha1: this.COMMIT_HASH, considerUnstableAsSuccess: true, credentialsId: 'BitbucketAppPassword', disableInprogressNotification: false, ignoreUnverifiedSSLPeer: true, includeBuildNumberInKey: false, prependParentProjectKey: false, projectKey: '', stashServerBaseUrl: 'http://repository.url/'
}
In the above notifyBitbucket function, the
commitSha1
is assigned from the git scm variable.
Note: The commitSha1 is assigned by custom global variable at checkout stage.
After adding the function, the Jenkins file will look like seen below.
COMMIT_HASH
will be loaded from the GIT SCM variables, GIT_COMMIT.
#!/usr/bin/env groovy
def COMMIT_HASH
node {
stage('checkout') {
// Before start the build set as INPROGRESS
scmVars = checkout scm
this.COMMIT_HASH = scmVars.GIT_COMMIT
this.notifyBitbucket('INPROGRESS')
}
stage('install dependency') {
// Skipped for this DEMO
}
stage('packaging') {
try {
sh "npm run build"
currentBuild.result = "SUCCESS"
} catch (e) {
// If there was an exception thrown, the build failed
currentBuild.result = "FAILED"
throw e
} finally {
// Success or failure, always send notifications
this.notifyBitbucket(currentBuild.result)
}
}
}
def notifyBitbucket(String state) {
if('SUCCESS' == state || 'FAILED' == state) {
// Set result of currentBuild !Important!
currentBuild.result = state
}
notifyBitbucket commitSha1: this.COMMIT_HASH, considerUnstableAsSuccess: true, credentialsId: ' BitbucketAppPassword', disableInprogressNotification: false, ignoreUnverifiedSSLPeer: true, includeBuildNumberInKey: false, prependParentProjectKey: false, projectKey: '', stashServerBaseUrl: 'http://repository.url/'
}
The example seen above displays the three stages of Jenkinsfile namely checkout, install dependency, and packaging. The currentBuild.result variable will be set for each stage to indicate its status.
Call the notifyBitbucket
on initial stage, in our case checkout stage, and call at the end of the stage packaging.
How to view commit build status
Follow the below steps to view commit status:
- In the bitbucket server, log on to your repository and navigate to All Branches Graph.
You will see all commits with their respective build status.
From the All Branch Graph, copy your successful commit, and execute the below command to work on a particular successful commit.
$ git checkout <sha1 or revision or commit>
Where, <sha1 or revision or commit> is 192ccbf60ee – the success build commit.
In this article, we have seen how to update build status in a Jenkins pipeline by using Bitbucket server notifier plugin. With that, we have found a way to insure that we always work on a successful build revision.
Author bio: Vidhyadharan, who is fondly called as Vidhya, is a Senior Engineering Specialist at Software AG. He has over 10+ years of full stack development, core engineering development with Java and Front-end environment in conjunction with complex architectures. This techno-savvy person likes to share his knowledge through his contributions to open source – Netbeans, Jhipster, and vscode.
To read more about his technical background, head to his LinkedIn page or for quick bites, follow him on Twitter.
Love sharing your technical expertise? Learn more about the Bitbucket writing program.