3

I have a Jenkins job which does some unit-testing for some code. It can be started in two possible ways:

  1. By the scheduler - it polls the Perforce server, and starts the job when it detects a code change
  2. By a user, on demand - it downloads a shelved/stashed code change, and checks whether it breaks the tests

The second way is called "Build Review":

Build Review


If the job discovers problems, I want to send emails conditionally, to avoid spam:

  1. If invoked by the scheduler, send mails to everyone who did check-ins since last successful build (called "developers" in Jenkins jargon)
  2. If invoked by a specific user, send a mail only to that user

I am using the Any Build Step plugin to add two conditional instances of Editable email Notification into my job settings. But what are the conditions? How can I tell it to send mail only if invoked in a specific way, (1) or (2) above?

4 Answers 4

2

I'm not sure if there is a simple way to check if the "scheduler" kicked off the job, but you can check if a user executed the job.

In your Jenkins job, check for the presence of the BUILD_USER_ID environment variable. If it is present, retrieve the value and send your email to the specific user(s) (scenario 1). If it is not present, a non-user has executed the job, and you can email the users listed on the last successful check-in (scenario 2).

If you are looking for methods outside of the Jenkins job, you can also use the Jenkins API (http://jenkinsurl/job/[JobName]/[BuildNumber]/api/json?pretty=true) and parse the BUILD_USER_ID from the output.

2

I couldn't get the accepted answer to work in a scripted pipeline for some reason. However, the following worked for me. See Cause API.

import hudson.model.Cause

// Triggered upstream
currentBuild.rawBuild.getCause(Cause.UpstreamCause) != null
// Triggered by user
currentBuild.rawBuild.getCause(Cause.UserIdCause) != null
0

The answer is in environment variables. All builds should have the "Environment Variables" link, which shows all the environment variables, which you can easily compare. In my case, the difference is in the BUILD_CAUSE and review variables:

Build 1 (started by the scheduler):

BUILD_CAUSE = SCMTRIGGER

Build 2 (started by "build review"):

BUILD_CAUSE = MANUALTRIGGER
review = 24564765

This solution won't work in the general case, because I have some groovy script and/or some plugin which set these environment variables.

7
  • I don't think this is available in core Jenkins. Do you have a plugin installed or a groovy script that sets this environment variable?
    – Preston Martin
    Commented Jun 28, 2020 at 13:50
  • Also, I doubt BUILD_CAUSE = SCMTRIGGER equates to it being started by the scheduler. It's most likely caused by a webhook from your SCM repository.
    – Preston Martin
    Commented Jun 28, 2020 at 13:54
  • groovy script? webhook? - just buzzwords for me; you are most likely right, I probably have these.
    – anatolyg
    Commented Jun 28, 2020 at 15:04
  • If you do have those enabled, I would recommend you add that to the question. Either post the Groovy script you are using, or the plugin (with a link) that you use. Reason being, if someone looks to this answer for a solution for their Jenkins system (regardless of the solution for your environment) they are not going to be able to resolve their issues. Otherwise, I would say that your answer is incomplete.
    – Preston Martin
    Commented Jun 28, 2020 at 18:06
  • I added all that I know; not much.
    – anatolyg
    Commented Jun 29, 2020 at 5:34
0

I found a very useful plugin:

Environment Injector Plugin https://plugins.jenkins.io/envinject/

Just configure the job with:

☑ Inject environment variables to the build process


From plugin webpage:

This plugin also exposes the cause of the current build as an environment variable. A build can be triggered by multiple causes at the same time e.g. an SCM Change could have occurred at the same time as a user triggers the build manually.

The build cause is exposed as a comma separated list:

BUILD_CAUSE=USERIDCAUSE, SCMTRIGGER, UPSTREAMTRIGGER, MANUALTRIGGER

In addition, each cause is exposed as a single environment variable too:

BUILD_CAUSE_USERIDCAUSE=true
BUILD_CAUSE_SCMTRIGGER=true
BUILD_CAUSE_UPSTREAMTRIGGER=true
BUILD_CAUSE_MANUALTRIGGER=true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.