Skip to main content
  1. Posts/

Add or update Bitbucket build status with REST API

·182 words·1 min· ·
Xianpeng Shen
Author
Xianpeng Shen
Table of Contents

Backgorud
#

  1. When you want to add build status to your Bitbucket the specific commit of a branch when you start a build from the branch

  2. When the build status is wrong, you want to update it manually. for example, update build status from FAILED to SUCCESSFUL

You can call Bitbucket REST API to do these.

Code snippet
#

Below is the code snippet to update Bitbucket build status with REST API in the shell script.

The code on GitHub Gist: https://gist.github.com/shenxianpeng/bd5eddc5fb39e54110afb8e2e7a6c4fb

Click Read More to view the code here.

#!/bin/sh

username=your-bitbucket-user
password=your-bitbucket-password

commit_id='57587d7d4892bc4ef2c4375028c19b27921e2485'
# build_result has 3 status: SUCCESSFUL, FAILED, INPROGRESS
build_result='SUCCESSFUL'
description='Manully update bitbucket status'

build_name='test #1'
build_url=http://localhost:8080/job/test/

bitbucket_rest_api='https://myorg.bitbucket.com/rest/build-status/latest/commits'

gen_post_data()
{
cat <<EOF
{
  "state": "$build_result",
  "key": "$commit_id",
  "name": "$build_name",
  "url": "$build_url",
  "description": "$description"
}
EOF
}

echo "$(gen_post_data)"

curl -u $username:$password \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST $bitbucket_rest_api/$commit_id --data "$(gen_post_data)"

if [ $? -ne 0 ]
then
  echo "$0: Update bitbucket build status failed."
  exit 1
else
  echo "$0: Update bitbucket build status success."
  exit 0
fi

And the screenshot of the final update result

Bitbucket build status

Related

These settings in Bitbucket/GitHub recommends enable
·282 words·2 mins
Provides a list of recommended settings for Bitbucket and GitHub repositories, including enabling force push rejection, branch protection, tag management, merge checks, and commit message standards.
Obtaining Bitbucket Repository Events in Real Time via the generic-webhook-trigger Plugin
·409 words·2 mins
This article describes how to use Jenkins’ generic-webhook-trigger plugin to obtain real-time event information from a Bitbucket repository, such as the Pull Request ID.
Bitbucket Webhooks Configuration
·398 words·2 mins
How to configure Bitbucket webhooks to trigger Jenkins builds for multi-branch pipelines, ensuring that Jenkins can automatically respond to events like pull requests and branch updates.
How to Set Up NFS Sharing and Mount on Different Platforms—Windows/Linux/Unix
·430 words·3 mins
This article introduces the steps and commands for setting up NFS sharing and mounting it on different platforms (Windows/Linux/Unix).
About Code Coverage
·839 words·4 mins
This article briefly introduces the concept, importance, common metrics, working principle, and mainstream tools of code coverage, emphasizing that code coverage metrics should not be over-relied upon.
Code coverage testing of C/C++ projects using Gcov and LCOV
·1000 words·5 mins
This article shares how to use Gcov and LCOV to metrics code coverage for C/C++ projects. It explains the steps to compile, run tests, and generate coverage reports, including example commands and expected outputs.