How to identify which repository triggered a build

Still need help?

The Atlassian Community is here for you.

Ask the community

The steps outlined on this article are provided AS-IS. This means we've had reports of them working for some customers — under certain circumstances — yet are not officially supported, nor can we guarantee they'll work for your specific scenario.

You may follow through and validate them on your own non-prod environments prior to production or fall back to supported alternatives if they don't work out.

We also invite you to reach out to our Community for matters that fall beyond Atlassian's scope of support!

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

On a build plan set up with multiple repositories, depending on the workflow, it might be useful to know which of the repositories triggered each specific build. Although this information is available in the UI (Build Summary > Code Commit section), some scenarios might benefit from having that information during build time.

Solution

Bamboo has variables that store both the current and previous revision of each repository on a given plan. We can use that in a Script Task to determine which repository had its contents changed (and thus triggered the build).

Script Task
#!/bin/bash
echo ===========================================================
i=1
while true
do
   repoRevision='bamboo_planRepository_'$i'_revision'
   prevRepoRevision='bamboo_planRepository_'$i'_previousRevision'
   if [ -z "${!repoRevision}" ]; then
      break
   else
      repoRevision=$repoRevision
      prevRepoRevision=$prevRepoRevision
      if [ ${!repoRevision} != ${!prevRepoRevision} ]
      then
        repoName='bamboo_planRepository_'$i'_name'
        repoBranch='bamboo_planRepository_'$i'_branch'
        echo 'The repository '${!repoName}' ('${!repoBranch}') was updated'
        echo 'Revision: '${!repoRevision}
        echo 'Previous revision: '${!prevRepoRevision}
      fi
      i=$(( $i + 1 ))
    fi
done
echo ===========================================================

You might need to update the script to any specific nuances your environment might have, which can include translating it into a different syntax.

The build logs will then show the following:

Build logs
===========================================================
29-Oct-2021 17:45:05	The repository MY-REPOSITORY (master-branch) was updated
29-Oct-2021 17:45:05	Revision: 4b8fa9bece5a2c95c1abea7ce760321421cec36e
29-Oct-2021 17:45:05	Previous revision: ea10759cc579f9bb9966f1b75500d33e7e18d342
===========================================================

Going further

You can do some cool things with that information. For example, you can inject the ${!repoName} value (or any of the other script variables) into a variable and use it in the conditional checkout tasks. (info) Please see the Inject Bamboo Variables Task documentation. The variable can then be integrated into the workflow on the following tasks & stages of a build plan.

Last modified on Nov 1, 2021

Was this helpful?

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