Jenkins – Get the build user name in job config

I would like to get the username who triggered the Jenkins job in the Post build task. At first i thought the Build User Vars Plugin could work but than i realize those variables are only available in the Build process but not the post build action.

Luckily, i found a blog post written by Donald Simpson which help me to solve the problem.

 

So for each Jenkins build, there is an xml which summarizes the details about the build which could be access thru the following url.

 

In order words, just go to a specific build number in the history list and add api/xml at the end.

To get the username, we can make use of the curl command is to access this xml file.

curl ${BUILD_URL}api/xml --silent | xml_grep --text_only userName

 

So here is a Post build task script to print the username to the console output.

THE_USER="$(curl ${BUILD_URL}api/xml --silent | xml_grep --text_only userName)"

echo "-------------"
echo $THE_USER
echo "-------------"

 

Done =)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s