Home > OS >  How to get the BUILD_USER in Jenkins when a human rebuilds a job triggered by timer?
How to get the BUILD_USER in Jenkins when a human rebuilds a job triggered by timer?

Time:11-09

EDIT: I use this plugin to schedule runs btw: https://plugins.jenkins.io/parameterized-scheduler/

I have set slack notification which would automatically be sent when the timer(scheduler) starts the execution.

In the slack channel, it would post something like this when the auto-build starts:

(/ci/master-15): Started by timer with parameters: {URL=https://shop.com, USERNAME=yyy, PASSWORD=xxs, SLACK-NOTIFY-CHANNEL=#JENKINS-REPORTS}

And when the run is complete, a message similar to this would be posted to the channel:

(/ci/master-15): The job is now complete. Here are the execution summary

Groovy:

BUILD_TRIGGERED_BY = currentBuild.getBuildCauses().shortDescription[0]
SEND_SLACK_NOTIF(BUILD_TRIGGER_BY)

Now, If a human rebuilds one of these timer jobs after they're unsuccessful, I would expect it to say "Started by [email protected]" but instead

In the pipeline status it shows:

Started by timer with parameters: {URL=https://shop.com, USERNAME=yyy, PASSWORD=xxs, SLACK-NOTIFY-CHANNEL=#JENKINS-REPORTS}

Started by user [email protected]

Rebuilds build #14



In the slack message it shows:

Started by timer with parameters: {URL=https://shop.com, USERNAME=yyy, PASSWORD=xxs, SLACK-NOTIFY-CHANNEL=#JENKINS-REPORTS}

How can I make it send the username instead of timer?

CodePudding user response:

The behavior you are seeing is a result of the plugins you are using. I assume the rebuild plugin picks both triggers when rebuilding the Job. Since you have consistent behavior when rebuilding, you can improve your Groovy code to something like the one below.

BUILD_TRIGGERED_BY = currentBuild.getBuildCauses().shortDescription.size() > 1 ?  currentBuild.getBuildCauses().shortDescription[1] : currentBuild.getBuildCauses().shortDescription[0]
  • Related