Answers: <p>The answer is in environment variables. All builds should have the &quot;Environment Variables&quot; link, which shows all the environment variables, which you can easily compare.  In my case, the difference is in the <code>BUILD_CAUSE</code> and <code>review</code> variables:</p>
  <p>Build 1 (started by the scheduler):</p>
  <pre><code>BUILD_CAUSE = SCMTRIGGER
  </code></pre>
  <p>Build 2 (started by &quot;build review&quot;):</p>
  <pre><code>BUILD_CAUSE = MANUALTRIGGER
  review = 24564765
  </code></pre>
  <hr />
  <p>This solution won't work in the general case, because I have some groovy script and/or some plugin which set these environment variables.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-06-28T13:50:39.860Z
Answers: 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?
Answers: Preston Martin
Answers: CC BY-SA 4.0
Answers: 2020-06-28T13:54:23.297Z
Answers: 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.
Answers: Preston Martin
Answers: CC BY-SA 4.0
Answers: 2020-06-28T15:04:13.980Z
Answers: groovy script? webhook? - just buzzwords for me; you are most likely right, I probably have these.
Answers: anatolyg
Answers: CC BY-SA 4.0
Answers: 2020-06-28T18:06:45.300Z
Answers: 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.
Answers: Preston Martin
Answers: CC BY-SA 4.0
Answers: 2020-06-29T05:34:14.980Z
Answers: I added all that I know; not much.
Answers: anatolyg
Answers: CC BY-SA 4.0
Answers: 2024-05-09T19:26:21.563Z
Answers: This doesn't seem to work with Jenkins (scripted) pipelines? I trigger a job from one pipeline, and echo `env.BUILD_CAUSE` on the triggered job, and it gives `null`?
Answers: Chris F
Answers: CC BY-SA 4.0
Answers: 2024-08-27T10:01:14.330Z
Answers: Just look at the answer from @G. C., he's using a plugin to set this environment variable
Answers: Janos Vinceller
Answers: CC BY-SA 4.0
Answers: 2020-06-28T13:38:00.140Z
Answers: 2020-06-29T05:32:26.633Z
Answers: 2020-06-29T05:32:26.633Z
Answers: anatolyg
Answers: <p>I couldn't get the accepted answer to work in a scripted pipeline for some reason. However, the following worked for me. See <a href="https://javadoc.jenkins-ci.org/hudson/model/Cause.html" rel="nofollow noreferrer">Cause API</a>.</p>
  <pre class="lang-java prettyprint-override"><code>import hudson.model.Cause
  
  // Triggered upstream
  currentBuild.rawBuild.getCause(Cause.UpstreamCause) != null
  // Triggered by user
  currentBuild.rawBuild.getCause(Cause.UserIdCause) != null
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-05-10T15:28:32.323Z
Answers: 2024-05-10T19:28:34.660Z
Answers: 2024-05-10T19:28:34.660Z
Answers: Chris F
Answers: <p>I'm not sure if there is a simple way to check if the &quot;scheduler&quot; kicked off the job, but you can check if a user executed the job.</p>
  <p>In your Jenkins job, check for the presence of the <code>BUILD_USER_ID</code> 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).</p>
  <p>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 <code>BUILD_USER_ID</code> from the output.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-06-26T03:41:09.993Z
Answers: 2020-06-28T11:58:22.043Z
Answers: 2020-06-28T11:58:22.043Z
Answers: Preston Martin
Answers: <p>I found a very useful plugin:</p>
  <p><strong>Environment Injector Plugin</strong>
  <a href="https://plugins.jenkins.io/envinject/" rel="nofollow noreferrer">https://plugins.jenkins.io/envinject/</a></p>
  <p>Just configure the job with:</p>
  <p>☑ Inject environment variables to the build process</p>
  <hr />
  <h2>From plugin webpage:</h2>
  <p>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.</p>
  <p>The build cause is exposed as a comma separated list:</p>
  <pre><code>BUILD_CAUSE=USERIDCAUSE, SCMTRIGGER, UPSTREAMTRIGGER, MANUALTRIGGER
  </code></pre>
  <p>In addition, each cause is exposed as a single environment variable too:</p>
  <pre><code>BUILD_CAUSE_USERIDCAUSE=true
  BUILD_CAUSE_SCMTRIGGER=true
  BUILD_CAUSE_UPSTREAMTRIGGER=true
  BUILD_CAUSE_MANUALTRIGGER=true
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-06-24T13:55:02.457Z
Answers: 2021-06-24T14:01:14.837Z
Answers: 2021-06-24T14:01:14.837Z
Answers: G. C.
Body: <p>I have a Jenkins job which does some unit-testing for some code. It can be started in two possible ways:</p>
  <ol>
  <li>By the scheduler - it polls the Perforce server, and starts the job when it detects a code change</li>
  <li>By a user, on demand - it downloads a shelved/stashed code change, and checks whether it breaks the tests</li>
  </ol>
  <p>The second way is called &quot;Build Review&quot;:</p>
  <p><a href="https://i.sstatic.net/ZguRE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZguRE.png" alt="Build Review" /></a></p>
  <hr />
  <p>If the job discovers problems, I want to send emails conditionally, to avoid spam:</p>
  <ol>
  <li>If invoked by the scheduler, send mails to everyone who did check-ins since last successful build (called &quot;developers&quot; in Jenkins jargon)</li>
  <li>If invoked by a specific user, send a mail only to that user</li>
  </ol>
  <p>I am using the <a href="https://plugins.jenkins.io/any-buildstep/" rel="nofollow noreferrer">Any Build Step plugin</a> 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?</p>
  
Tags: <jenkins><email>
Title: How to determine whether the Jenkins build was started by a user or the scheduler?
UserDisplayName: anatolyg
ThreadText: # How to determine whether the Jenkins build was started by a user or the scheduler?
  
  (Asked by: username_0 on 2020-06-25)
  
  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](https://i.sstatic.net/ZguRE.png)](https://i.sstatic.net/ZguRE.png)
  
  ---
  
  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](https://plugins.jenkins.io/any-buildstep/) 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?
  
  **Tags:** jenkins, email
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_0 on 2020-06-28. Score: 0 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_1**: 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?  
  > **username_1**: 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.  
  > **username_0**: groovy script? webhook? - just buzzwords for me; you are most likely right, I probably have these.  
  > **username_1**: 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.  
  > **username_0**: I added all that I know; not much.  
  > **username_2**: This doesn't seem to work with Jenkins (scripted) pipelines? I trigger a job from one pipeline, and echo `env.BUILD_CAUSE` on the triggered job, and it gives `null`?  
  > **username_3**: Just look at the answer from @G. C., he's using a plugin to set this environment variable  
  
  ---
  
  ### Answer by username_2 on 2024-05-10. Score: 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](https://javadoc.jenkins-ci.org/hudson/model/Cause.html).
  
  ```java
  import hudson.model.Cause
  
  // Triggered upstream
  currentBuild.rawBuild.getCause(Cause.UpstreamCause) != null
  // Triggered by user
  currentBuild.rawBuild.getCause(Cause.UserIdCause) != null
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2020-06-26. Score: 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.
  
  
  ---
  
  ### Answer by username_4 on 2021-06-24. Score: 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
  
  ```
  
  
  ---
  
  