How to know which user triggered a build during execution time
プラットフォームについて: Server および Data Center のみ。この記事は、Server および Data Center プラットフォームのアトラシアン製品にのみ適用されます。
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.
*Fisheye および Crucible は除く
本記事で説明している手順は、現時点でのものとなります。そのため、一部のお客様で特定の状況下で動作したという報告がありますが、正式にサポートされているわけではなく、お客様の特定のシナリオで動作することを保証するものではありません。
本番環境での実施の前に一通り非本番環境で検証し、成功しなかった場合にはサポートされている代替案にフォール バックしてください。
要約
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.
環境
All Bamboo versions.
ソリューション
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.
echo "Build triggered by ${bamboo.ManualBuildTriggerReason.userName}"
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 usingxmllint
. 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.
AUTHOR=$(git log -1 --pretty=format:'%an')
echo "Committed by $AUTHOR"
[...]
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.,