How to get BUILD_USER in Jenkins when a task runs on a timer?

I wanted to show the user who triggered Jenkins work in an email. This is possible using the Build User Vars Plugin plugin and the env variable BUILD_USER . But this variable is not initialized when the task is started by the scheduler.

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

But I just want to know how we can use this and reach a solution ...

+19
source share
10 answers

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

When the assembly is started manually by the logged in user, this user ID is available in the environment variable BUILD_USER_ID .

However, this environment variable will not be replaced / initialized when the assembly is automatically started by the Jenkins timer / scheduler.

Attached screenshot for details enter image description here

This problem can be solved by embedding the condition in the task using the conditional assembly stage plug-in / execution condition plug-in, where we can add a condition for each task to initialize the variable BUILD_USER_ID only when the assembly is called or started by a timer or scheduler, setting the condition using a regular expression ..

+15
source

The Build User Vars plugin did not work for me, so I quickly and dirty hacked:

 BUILD_CAUSE_JSON=$(curl --silent ${BUILD_URL}/api/json | tr "{}" "\n" | grep "Started by") BUILD_USER_ID=$(echo $BUILD_CAUSE_JSON | tr "," "\n" | grep "userId" | awk -F\" '{print $4}') BUILD_USER_NAME=$(echo $BUILD_CAUSE_JSON | tr "," "\n" | grep "userName" | awk -F\" '{print $4}') 
+13
source

Install the "Build Vars User Vars Plugin" and use as shown below: - [See Https://plugins.jenkins.io/build-user-vars-plugin ]

enter image description here

Be sure to check the " Set jenkins user build variables " section of the Build Environment checkbox to configure the Jenkins job.

enter image description here

+4
source

I use a combination of the Execute Shell and Env Inject modules as follows:

  • Create an Execute Shell build step that uses shell parameter substitution to write the default value and map that value to a file. An example is shown in the screenshot below.
  • Use the Env Inject file to read this file as properties for the installation.

Jenkins Builds Steps for Default Variables

+3
source

The $ BUILD_CAUSE token from the email-ext plugin is what you are looking for.

Can you see the full link to the content token when you click the button? immediately after combo box attachment assembly assembly when setting up email content.

Some tokens are added by plugins, but by default this should be available.

Edit: As the bishop noted in the comments, when using the EnvInject plugin , the $ BUILD_CAUSE token changes to behave differently.

+2
source

I wanted to call assembly initiator information for one of my slack / flock groups, so I used the following method to get the email and assembly initiator name in a declarative style.

I just print here, you can use to store in some environment variable or write to a single file, giving the file path according to your own preferences ..

 pipeline { environment { BRANCH_NAME = "${env.BRANCH_NAME}" } agent any stages{ stage('Build-Initiator-Info'){ sh 'echo $(git show -s --pretty=%ae)' sh 'echo $(git show -s --pretty=%an)' } } 

}

+1
source

It turns out the name of the user who clicked Build Now in the Jenkins pipeline job.

 @NonCPS def getBuildUser() { return currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId() } 
+1
source

Iโ€™ll just clarify Musaffirโ€™s answer. The conditional build step plugin now directly supports the build reason - this also requires the execution condition plugin .

If you want to determine when the assembly was started on a timer, you can choose Run? Reason for build with reason to build from: TimerTrigger

enter image description here

This is a bit simpler and more reliable than using regular expressions. There are other triggers that you can detect, for example, when the assembly was the result of fixing source control, you can choose: SCMTrigger.

0
source

I found a similar, but really working on Jenkins 2.1.x and easy for my understanding method. And it works without any plugins.

 if (currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')['userId']){ // Will be run only if someone user triggers build // Because in other cases this contructions returns null } 

You can use any classes described here in this construct. They will return cards with useful values.

0
source

For the declarative syntax of the pipeline, here is a quick hack based on @Kevin's answer. For a declarative pipeline, you need to wrap them in a node, otherwise you will get an assembly error / failure

 node { def BUILD_FULL = sh ( script: 'curl --silent '+buildURL+' | tr "{}" "\\n" | grep -Po \'"shortDescription":.*?[^\\\\]"\' | cut -d ":" -f2', returnStdout: true ) slackSend channel: '#ci-cd', color: '#000000', message: "The pipeline was ${BUILD_FULL} ${GIT_COMMIT_MSG} " } 

The result will be a weak notification sent to your free channel with a brief description of git

0
source

Source: https://habr.com/ru/post/1245679/


All Articles