jenkinsjenkins-pluginsjenkins-pipeline

How to get the BUILD_USER in Jenkins when job triggered by timer?


I wanted to show the user who triggered a Jenkins job in the post job email. This is possible by using the plugin Build User Vars Plugin and the env variable BUILD_USER. But this variable do not get initialized when the job is triggered by a scheduler.

How can we achieve this? I know we have a plugin called - EnvInject Plugin, and that can be used...

But I just want to know how we can use this and achieve the solution...


Solution

  • This can be done using the Jenkins Build User Vars Plugin which exposes a set of environment variables, including the user who started the build. It gives environment variables like BUILD_USER_ID, EMAIL, etc.

    When the build is triggered manually by a logged-in user, that user's userid is available in the BUILD_USER_ID environment variable.

    However, this environment variable won't be replaced / initialized when the build is automatically triggered by a Jenkins timer / scheduler.

    Attached a screenshot for detailsenter image description here

    This can be resolved by injecting a condition to the Job by using Conditional Build Step Plugin / Run Condition Plugin,where in to each job we can add a condition to initialize the variable BUILD_USER_ID only when the build is caused or triggered by the Timer or scheduler, by setting a condition using the regular expression..