How to know which user triggered a build during execution time

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

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!

Summary

Depending on your use-case scenario, it might be interesting to know, during execution time, who triggered the build. This article covers ways to do this.

Environment

All Bamboo versions.

Solution

Manually triggered builds

Bamboo features a variable, ${bamboo.ManualBuildTriggerReason.userName}, that stores, upon a manual run of a build, the name of the user who triggered it. This variable can be used in a Script task to echo the username of who manually triggered the build.

Script task example
echo "Build triggered by ${bamboo.ManualBuildTriggerReason.userName}"
Build logs
04-Jul-2022 18:48:44 Build triggered by admin


Retrieving the full name

If the username (e.g., jdoe) is not enough and you'd like to retrieve the full name of the user who triggered the build, you can do so with the workaround below:

  • Make a REST API call to the /search/users endpoint, and then filter out the XML / JSON results. This endpoint can be used to perform a starts-with search of users based on the username and returns their information, including the full name, which is what we're looking for. Implementation example using a Script Task:

    rm -rf data.xml
    
    curl -X GET "http://<BAMBOO_URL>/rest/api/latest/search/users?searchTerm=${bamboo.ManualBuildTriggerReason.userName}" \
    -H "Authorization: Bearer <TOKEN>" \
    -H 'Content-Type: application/xml' >> data.xml
    
    FULL_NAME=$(/usr/bin/xmllint --xpath "string(/searchResults/searchResults/searchEntity/fullName)" data.xml)
    
    echo "build triggered by: $FULL_NAME"

    The solution above might need adjustments according to your environment.

    The libxml2-utils package is used in order to be able to parse the XML data using xmllint. You can also use other solutions, such as getting the API response in JSON and parsing with JQ. 

    A personal token is also used, for authorization purposes.

Automatically triggered builds

However, if the plan was triggered automatically, the value of ${bamboo.ManualBuildTriggerReason.userName} is null. For that case, it's possible to implement a script to get the name of the user who performed the repository commit that triggered the build. The following can be added to a Script task after the Source Code Checkout task.

Script task content
AUTHOR=$(git log -1 --pretty=format:'%an')
echo "Committed by $AUTHOR"
Build logs
[...]
simple	04-Jul-2022 18:52:39	Finished task 'Source Code Checkout' with result: Success
[...]
simple	04-Jul-2022 18:52:39	Starting task 'Script' of type 'com.atlassian.bamboo.plugins.scripttask:task.builder.script'
command	04-Jul-2022 18:52:39	Beginning to execute external process for build 'Test project - test 2 - Default Job #3 (TP-TEST2-JOB1-3)[...]
build	04-Jul-2022 18:52:39	Committed by John Doe
simple	04-Jul-2022 18:52:39	Finished task 'Script' with result: Success

Solution for both automatically and manually triggered builds

In order to implement a way that works for both manual and automatically triggered builds in the same workflow, you can add both Script tasks mentioned above, but with conditions for their execution: if the variable ManualBuildTriggerReason.userName exists, the script for Manually triggered builds should be executed. If it doesn't exist, the script for Automatically triggered builds should be executed. E.g.,




Last modified on Jul 4, 2022

Was this helpful?

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