After a build is finished I use the mail-ext-plugin (Jenkins Email Extension Plugin) to send an email to certain users. I would like to include the user who started (requested) the build in that mail. - I couldn't find anything working with the default Jenkins vars. - I Couldn't find anything in the mail-ext vars. - I tried Build User Vars Plugin to get $BUILD_USER
but I can't get it to work.
How to get to $BUILD_USER, BUILD_REQUESTER, USERNAME or something like that
For now I have
$DEFAULT_CONTENT
--
{$BUILD_USER}
resulting in
zzz test GreenHat - Build # 27 - Still Failing:
Check console output....
--
{$BUILD_USER}
also tried without {}
SIMPLE SOLUTIONS (WITHOUT ANY PLUGINS / NO PLUGINS) !!
METHOD 1: Via Shell
BUILD_TRIGGER_BY=$(curl --silent ${BUILD_URL}/api/xml | tr '<' '\n' | egrep '^userId>|^userName>' | sed 's/.*>//g' | sed -e '1s/$/ \//g' | tr '\n' ' ')
echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
METHOD 2: Via Groovy
node('master') {
BUILD_TRIGGER_BY = sh ( script: "curl --silent ${BUILD_URL}/api/xml | tr '<' '\n' | egrep '^userId>|^userName>' | sed 's/.*>//g' | sed -e '1s/\$/ \\/ /g'", returnStdout: true ).trim()
echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
}
METHOD 3: Via Groovy
BUILD_TRIGGER_BY = "${currentBuild.getBuildCauses()[0].shortDescription} / ${currentBuild.getBuildCauses()[0].userId}"
echo "BUILD_TRIGGER_BY: ${BUILD_TRIGGER_BY}"
OUTPUT:
Started by user Admin / [email protected]
Note: Output will be both User ID and User Name
I realise I'm replying to a 4-year-old question here, but Google brought me here so others may find this information useful...
There have been many changes to the Email-ext Jenkins plugin over the last few years, which affect this question - for example, in v2.38 (May 24, 2014):
Implemented new extension point for recipient providers (RecipientProvider) this changes the way that recipient types are added
There are no longer checkboxes for "Requestor" "Recipients" etc, each is provided by an implementation of a RecipientProvider
Using the latest version of the plugin, if you wish to send an email to the user who triggered a build, you can simply select "Requestor" from the "Send To" trigger options:
The "Recipient List" is a hard-coded list which you can also define for the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With