Search code examples
jenkinsslack

jenkins: Find the triggering user of a job for the build-script


We're using Jenkins and Slack. I'm trying to setup a slack notification about a build, and I would like the notifucation to include more information - so I resort to the custom message.

I found about $GIT_BRANCH and $BUILD_NUMBER and $JOB_NAME and other interesting guys, and put them to use.

But I lack a way to mention the cause that triggered the build - be it a polling on the scm, or a user that pressed build now. The later is of extreme importance - especially in jobs that deploy to a target environment...

Can anybody recommend me a way to detect the triggering cause and add it to the custom message?

BTW - I'd want this information not only for slack notification. I'd like to add this information to a signature file that we add to every artifact (as well as other things)


Solution

  • Use the Build User Vars Plugin:

    https://wiki.jenkins-ci.org/display/JENKINS/Build+User+Vars+Plugin

    You will have the variables:

    • BUILD_USER – full name of user started build,
    • BUILD_USER_FIRST_NAME – first name of user started build,
    • BUILD_USER_LAST_NAME – last name of user started build,
    • BUILD_USER_ID – id of user started build.

    *If you want to get it from other job you can use the below URL:

    http://[jenkins-server]/job/[job-name]/[build-number]/api/xml?xpath=(//action/cause/shortDescription)

    It will directly return the line:

    < shortDescription>Started by user foobar< /shortDescription>