How to retrieve the revision used by the previous build in Bamboo

Still need help?

The Atlassian Community is here for you.

Ask the community


Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

This article explains how to retrieve the Revision used by the previous build in Bamboo using the REST API.

Diagnosis

In certain scenarios, it is desired to know the last Revision tag used by a build and then make decisions based on that.
Currently, Bamboo implements two variables that are related to the Repository.

  • ${bamboo.planRepository.previousRevision}
  • ${bamboo.planRepository.revision}

Those two variables will not consider the builds circumstances and will continue reporting only the current and past revisions in the Repository, hence if you had to check for any custom builds based on a specific Revision, that information will not be provided by those variables.

Solution

You can use the following script to capture that information by using the Bamboo REST API. You can use a more flexible language like Python, Ruby or even Java if you'd like.

The script below is provided "as-is" and serves as a guide only. If running from within a Script task in Bamboo, make sure to switch from curl's username/password to a Personal Access Token (PAT) in read-only mode.

The script will run on the Bamboo Agent, either Local, Remote or Elastic, so make sure that it has connectivity back to Bamboo and that the required tools like curl, and jq are available.

Bonus: The script will also print the last successful build of the Plan by querying the API.

#!/bin/bash

planKey=${bamboo.planKey}
buildNumber=${bamboo.buildNumber}
planRepositoryRevision=${bamboo.planRepository.revision}

BAMBOO_BASEURL="https://bamboo.mydomain.net"
BAMBOO_USERPASS="admin:password"
CURL_OPTIONS=(-s -k -X GET -H 'Accept: application/json' -u ${BAMBOO_USERPASS})

function get_vcsrevision() {
  curl "${CURL_OPTIONS[@]}" \
    "${BAMBOO_BASEURL}/rest/api/latest/result/${1}" \
      | jq -Mr '.vcsRevisionKey'
}

function get_last_successful_build() {
  curl "${CURL_OPTIONS[@]}" \
    "${BAMBOO_BASEURL}/rest/api/latest/result/${1}?max-result=100" \
      | jq -Mr 'first(.results.result[] | select (.buildState == "Successful") | .buildNumber)'
}

PREVIOUS_VCSREVISION=$(get_vcsrevision ${planKey}-$((${buildNumber}-1)))
LAST_SUCCESSFUL_BUILD=$(get_last_successful_build ${planKey})

echo "Current VCS revision is ${planRepositoryRevision}"
echo "Previous VCS revision was ${PREVIOUS_VCSREVISION}"
echo "The last successful build was ${LAST_SUCCESSFUL_BUILD}"

Sample output

(...)
24-Nov-2021 00:07:01	Current VCS revision is 33ea6f6d86ea31c4b74c33d637641c3c2f1b8805
24-Nov-2021 00:07:01	Previous VCS revision was edd7f413393cdf59d5ed91762132b3cc7b728683
24-Nov-2021 00:07:01	The last successful build was 62
24-Nov-2021 00:07:01	Finished task 'Success' with result: Success
(...)

Last modified on Nov 23, 2021

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.