<p>use try/catch</p>

<pre><code>def safeBuildUser = "unknown"
wrap([$class: 'BuildUser']) {
  try {
     safeBuildUser = BUILD_USER
  } catch (e) {
     echo "User not in scope, probably triggered from another job"
  }
}
echo "Builduser is: ${safeBuildUser}"
</code></pre>

CC BY-SA 4.0
2018-11-09T13:17:21.640Z
2018-11-09T13:17:21.640Z
Anders Elton
<p>I am using the Build User Vars <a href="https://wiki.jenkins.io/display/JENKINS/Build+User+Vars+Plugin" rel="nofollow noreferrer">Plugin</a> which sets various variables in the pipeline. Here is what I am currently trying:</p>

<pre><code>wrap([$class: 'BuildUser']) {
    def safeBuildUser = binding.hasVariable('BUILD_USER') ? BUILD_USER : "unknown"
    echo "${safeBuildUser}"
}
</code></pre>

<p>There are times when the plugin does not set the variable. For instance, when the build was launched by an SCM branch scan. When that happens, I receive: </p>

<pre><code>groovy.lang.MissingPropertyException: No such property: BUILD_USER for class: groovy.lang.Binding
</code></pre>

<p>Using <code>binding.hasVariable</code> does not appear to work because all of the builds now show "unknown".</p>

<p>In addition to the above, I also tried the following with no luck:</p>

<pre><code>def safeBuildUser = BUILD_USER?: "unknown" 
def safeBuildUser = binding['BUILD_USER']?: "unknown"
def safeBuildUser = binding.variables['BUILD_USER']?: "unknown"
</code></pre>

CC BY-SA 4.0
2018-06-02T04:26:10.827Z
Welcome to DevOps! Just a side note: in general it's recommended to edit your post and add the extra info/details in there rather than in comments.
Dan Cornilescu
CC BY-SA 4.0
2018-07-12T22:08:18.450Z
What is binding ? Shouldn't it be env ?
Michael Doubez
<jenkins-pipeline><jenkins-plugins>
How to test whether a variable exists in the declarative pipeline
Wes W
# How to test whether a variable exists in the declarative pipeline

(Asked by: username_0 on 2018-06-01)

I am using the Build User Vars Plugin which sets various variables in the pipeline. Here is what I am currently trying:
wrap([$class: 'BuildUser']) {
    def safeBuildUser = binding.hasVariable('BUILD_USER') ? BUILD_USER : "unknown"
    echo "${safeBuildUser}"
}

There are times when the plugin does not set the variable. For instance, when the build was launched by an SCM branch scan. When that happens, I receive: 
groovy.lang.MissingPropertyException: No such property: BUILD_USER for class: groovy.lang.Binding

Using binding.hasVariable does not appear to work because all of the builds now show "unknown".
In addition to the above, I also tried the following with no luck:
def safeBuildUser = BUILD_USER?: "unknown" 
def safeBuildUser = binding['BUILD_USER']?: "unknown"
def safeBuildUser = binding.variables['BUILD_USER']?: "unknown"

**Tags:** jenkins-pipeline, jenkins-plugins

> **Comments:**
> **username_1**: Welcome to DevOps! Just a side note: in general it's recommended to edit your post and add the extra info/details in there rather than in comments.  
> **username_2**: What is binding ? Shouldn't it be env ?  

---

## 1 answers

### Answer by username_3 on 2018-11-09. Score: 5

use try/catch
def safeBuildUser = "unknown"
wrap([$class: 'BuildUser']) {
  try {
     safeBuildUser = BUILD_USER
  } catch (e) {
     echo "User not in scope, probably triggered from another job"
  }
}
echo "Builduser is: ${safeBuildUser}"


---

