<p>I've spent a bit of time playing &quot;follow the bouncing ball&quot; through the <a href="https://github.com/jenkinsci/generic-webhook-trigger-plugin/" rel="nofollow noreferrer">Generic Webhook Trigger Plugin source code</a>, and it would seem that it is because those variables being displayed do not originate from your pipeline at all. Or at least, the values don't.</p>
<p>What happens is that when your webhook is triggered, the <a href="https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/GenericWebHookRequestReceiver.java" rel="nofollow noreferrer">webhook request receiver</a> grabs the <a href="https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/GenericTrigger.java" rel="nofollow noreferrer">trigger</a> for your Jenkins job, which creates a <a href="https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/GenericWebhookEnvironmentContributor.java" rel="nofollow noreferrer">cause object</a> that will then take the list of generic variables you supplied and pass them to the <a href="https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/resolvers/VariablesResolver.java" rel="nofollow noreferrer">variable resolver</a> which will actually then use the <a href="https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/resolvers/PostContentParameterResolver.java" rel="nofollow noreferrer">post content parameter resolver</a> to grab the value from the post content for any of the generic variables where you supplied either a JSON or XML path as the value. Where the value does not match an input from the request headers, request parameters, or POST content, the variable is silently discarded.</p>
<p>Since genericVariables are only used by the Generic Webhook Trigger, and not by the Jenkins Pipeline itself, if you want a static variable to remain in scope once you enter the stage you will need to define it in the proper context - i.e, using the Groovy <a href="http://groovy-lang.org/semantics.html#_variable_definition" rel="nofollow noreferrer">def statement</a> inside the node context, or use <a href="https://www.jenkins.io/doc/book/pipeline/syntax/#environment" rel="nofollow noreferrer">the environment</a></p>

CC BY-SA 4.0
2021-05-14T04:40:13.857Z
2021-05-14T04:40:13.857Z
Kyanar
<p>I'm new to DevOps, and am cobbling together a beginner understanding from lots of reading, YouTube videos, and online examples.</p>
<p>I've had initial success adapting <a href="https://www.youtube.com/watch?v=8mrJNkofxq4" rel="nofollow noreferrer">this helpful tutorial</a>. I.e. in my bitbucket project, I've introduced a webhook that posts to Jenkins' generic pipeline url. The script referenced in the video is <a href="https://github.com/tomasbjerre/bitbucket-server-jenkins-release-tags/blob/master/Jenkinsfile" rel="nofollow noreferrer">here</a>. My (simplified) adaptation is:</p>
<pre><code>node {
 properties([
  pipelineTriggers([
   [$class: 'GenericTrigger',
    genericVariables: [
     [ key: 'committer_name', value: '$.actor.name' ],
     [ key: 'committer_email', value: '$.actor.emailAddress' ],
     [ key: 'ref', value: '$.changes[0].refId'],
     [ key: 'tag', value: '$.changes[0].ref.id', regexpFilter: 'refs/tags/'],
     [ key: 'commit', value: '$.changes[0].toHash' ],
     [ key: 'repo_slug', value: '$.repository.slug' ],
     [ key: 'project_key', value: '$.repository.project.key' ],
     [ key: 'clone_url', value: '$.repository.links.clone[0].href' ],
     [ key: 'var1', value: 'value1' ],
     [ key: 'var2', value: 'value2' ],
     [ key: 'var3', value: 'value3' ],
     [ key: 'var4', value: 'value4' ]
    ],
     
    causeString: '$committer_name pushed tag $tag to $clone_url referencing $commit',
    
    token: 'my_token',
    
    printContributedVariables: true,
    printPostContent: true,
    
    regexpFilterText: '$ref',
    regexpFilterExpression: '^refs/tags/.*'
   ]
  ])
 ])

 stage(&quot;Prepare&quot;) {
  deleteDir()
  sh '''
  echo $committer_name
  echo $committer_email
  echo $var1
  echo $var2
  '''
 }
}
</code></pre>
<p>Following is console output from one run of this job:</p>
<pre><code>user pushed tag wip35 to https://bitbucket.someplace.com/scm/san/user_generic_webhook_sender.git referencing 24e71eaa1a9f92fd850135798a4177e810a926ab
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on builder-231 in /home/builder/workspace/on_this_project_generic_webhook_build_that_project
[Pipeline] {
GenericWebhookEnvironmentContributor
 Received:

{&quot;eventKey&quot;:&quot;repo:refs_changed&quot;,&quot;date&quot;:&quot;2021-05-13T16:02:29-0700&quot;,&quot;actor&quot;:{&quot;name&quot;:&quot;user&quot;,&quot;emailAddress&quot;:&quot;user@someplace.com&quot;,&quot;id&quot;:4464,&quot;displayName&quot;:&quot;user&quot;,&quot;active&quot;:true,&quot;slug&quot;:&quot;user&quot;,&quot;type&quot;:&quot;NORMAL&quot;,&quot;links&quot;:{&quot;self&quot;:[{&quot;href&quot;:&quot;https://bitbucket.someplace.com/users/user&quot;}]}},&quot;repository&quot;:{&quot;slug&quot;:&quot;user_generic_webhook_sender&quot;,&quot;id&quot;:4285,&quot;name&quot;:&quot;user_generic_webhook_sender&quot;,&quot;description&quot;:&quot;Sends generic webbooks to  Jenkins&quot;,&quot;scmId&quot;:&quot;git&quot;,&quot;state&quot;:&quot;AVAILABLE&quot;,&quot;statusMessage&quot;:&quot;Available&quot;,&quot;forkable&quot;:true,&quot;project&quot;:{&quot;key&quot;:&quot;SAN&quot;,&quot;id&quot;:2280,&quot;name&quot;:&quot;sandbox_projects&quot;,&quot;description&quot;:&quot;sandbox_projects_prj&quot;,&quot;public&quot;:true,&quot;type&quot;:&quot;NORMAL&quot;,&quot;links&quot;:{&quot;self&quot;:[{&quot;href&quot;:&quot;https://bitbucket.someplace.com/projects/SAN&quot;}]}},&quot;public&quot;:false,&quot;links&quot;:{&quot;clone&quot;:[{&quot;href&quot;:&quot;https://bitbucket.someplace.com/scm/san/user_generic_webhook_sender.git&quot;,&quot;name&quot;:&quot;http&quot;},{&quot;href&quot;:&quot;ssh://git@bitbucket.someplace.com:7999/san/user_generic_webhook_sender.git&quot;,&quot;name&quot;:&quot;ssh&quot;}],&quot;self&quot;:[{&quot;href&quot;:&quot;https://bitbucket.someplace.com/projects/SAN/repos/user_generic_webhook_sender/browse&quot;}]}},&quot;changes&quot;:[{&quot;ref&quot;:{&quot;id&quot;:&quot;refs/tags/wip35&quot;,&quot;displayId&quot;:&quot;wip35&quot;,&quot;type&quot;:&quot;TAG&quot;},&quot;refId&quot;:&quot;refs/tags/wip35&quot;,&quot;fromHash&quot;:&quot;0000000000000000000000000000000000000000&quot;,&quot;toHash&quot;:&quot;24e71eaa1a9f92fd850135798a4177e810a926ab&quot;,&quot;type&quot;:&quot;ADD&quot;}]}


Contributing variables:

    clone_url = https://bitbucket.someplace.com/scm/san/user_generic_webhook_sender.git
    commit = 24e71eaa1a9f92fd850135798a4177e810a926ab
    committer_email = user@someplace.com
    committer_name = user
    project_key = SAN
    ref = refs/tags/wip35
    repo_slug = user_generic_webhook_sender
    tag = wip35
    var1 = 
    var2 = 


[Pipeline] properties
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] deleteDir
[Pipeline] sh
+ echo user
user
+ echo user@someplace.com
user@someplace.com
+ echo

+ echo

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
</code></pre>
<p><strong>Question: Why are <code>$var1</code> and <code>$var2</code> empty during the stage execution, whereas <code>$committer_name</code> and <code>$committer_email</code> have the values that were assigned in the <code>genericVariables</code> section of the Pipeline script?</strong></p>

<jenkins><jenkins-pipeline><pipeline>
Why are variables with fixed values not visible in Jenkins Pipeline stage?
StoneThrow
# Why are variables with fixed values not visible in Jenkins Pipeline stage?

(Asked by: username_0 on 2021-05-13)

I'm new to DevOps, and am cobbling together a beginner understanding from lots of reading, YouTube videos, and online examples.
I've had initial success adapting this helpful tutorial. I.e. in my bitbucket project, I've introduced a webhook that posts to Jenkins' generic pipeline url. The script referenced in the video is here. My (simplified) adaptation is:
node {
 properties([
  pipelineTriggers([
   [$class: 'GenericTrigger',
    genericVariables: [
     [ key: 'committer_name', value: '$.actor.name' ],
     [ key: 'committer_email', value: '$.actor.emailAddress' ],
     [ key: 'ref', value: '$.changes[0].refId'],
     [ key: 'tag', value: '$.changes[0].ref.id', regexpFilter: 'refs/tags/'],
     [ key: 'commit', value: '$.changes[0].toHash' ],
     [ key: 'repo_slug', value: '$.repository.slug' ],
     [ key: 'project_key', value: '$.repository.project.key' ],
     [ key: 'clone_url', value: '$.repository.links.clone[0].href' ],
     [ key: 'var1', value: 'value1' ],
     [ key: 'var2', value: 'value2' ],
     [ key: 'var3', value: 'value3' ],
     [ key: 'var4', value: 'value4' ]
    ],
     
    causeString: '$committer_name pushed tag $tag to $clone_url referencing $commit',
    
    token: 'my_token',
    
    printContributedVariables: true,
    printPostContent: true,
    
    regexpFilterText: '$ref',
    regexpFilterExpression: '^refs/tags/.*'
   ]
  ])
 ])

 stage("Prepare") {
  deleteDir()
  sh '''
  echo $committer_name
  echo $committer_email
  echo $var1
  echo $var2
  '''
 }
}

Following is console output from one run of this job:
user pushed tag wip35 to https://bitbucket.someplace.com/scm/san/user_generic_webhook_sender.git referencing 24e71eaa1a9f92fd850135798a4177e810a926ab
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on builder-231 in /home/builder/workspace/on_this_project_generic_webhook_build_that_project
[Pipeline] {
GenericWebhookEnvironmentContributor
 Received:

{"eventKey":"repo:refs_changed","date":"2021-05-13T16:02:29-0700","actor":{"name":"user","emailAddress":"user@someplace.com","id":4464,"displayName":"user","active":true,"slug":"user","type":"NORMAL","links":{"self":[{"href":"https://bitbucket.someplace.com/users/user"}]}},"repository":{"slug":"user_generic_webhook_sender","id":4285,"name":"user_generic_webhook_sender","description":"Sends generic webbooks to  Jenkins","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"SAN","id":2280,"name":"sandbox_projects","description":"sandbox_projects_prj","public":true,"type":"NORMAL","links":{"self":[{"href":"https://bitbucket.someplace.com/projects/SAN"}]}},"public":false,"links":{"clone":[{"href":"https://bitbucket.someplace.com/scm/san/user_generic_webhook_sender.git","name":"http"},{"href":"ssh://git@bitbucket.someplace.com:7999/san/user_generic_webhook_sender.git","name":"ssh"}],"self":[{"href":"https://bitbucket.someplace.com/projects/SAN/repos/user_generic_webhook_sender/browse"}]}},"changes":[{"ref":{"id":"refs/tags/wip35","displayId":"wip35","type":"TAG"},"refId":"refs/tags/wip35","fromHash":"0000000000000000000000000000000000000000","toHash":"24e71eaa1a9f92fd850135798a4177e810a926ab","type":"ADD"}]}


Contributing variables:

    clone_url = https://bitbucket.someplace.com/scm/san/user_generic_webhook_sender.git
    commit = 24e71eaa1a9f92fd850135798a4177e810a926ab
    committer_email = user@someplace.com
    committer_name = user
    project_key = SAN
    ref = refs/tags/wip35
    repo_slug = user_generic_webhook_sender
    tag = wip35
    var1 = 
    var2 = 


[Pipeline] properties
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] deleteDir
[Pipeline] sh
+ echo user
user
+ echo user@someplace.com
user@someplace.com
+ echo

+ echo

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Question: Why are $var1 and $var2 empty during the stage execution, whereas $committer_name and $committer_email have the values that were assigned in the genericVariables section of the Pipeline script?

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


---

## 1 answers

### Answer by username_1 on 2021-05-14. Score: 1 | Accepted answer

I've spent a bit of time playing "follow the bouncing ball" through the [Generic Webhook Trigger Plugin source code](https://github.com/jenkinsci/generic-webhook-trigger-plugin/), and it would seem that it is because those variables being displayed do not originate from your pipeline at all. Or at least, the values don't.

What happens is that when your webhook is triggered, the [webhook request receiver](https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/GenericWebHookRequestReceiver.java) grabs the [trigger](https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/GenericTrigger.java) for your Jenkins job, which creates a [cause object](https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/GenericWebhookEnvironmentContributor.java) that will then take the list of generic variables you supplied and pass them to the [variable resolver](https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/resolvers/VariablesResolver.java) which will actually then use the [post content parameter resolver](https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gwt/resolvers/PostContentParameterResolver.java) to grab the value from the post content for any of the generic variables where you supplied either a JSON or XML path as the value. Where the value does not match an input from the request headers, request parameters, or POST content, the variable is silently discarded.

Since genericVariables are only used by the Generic Webhook Trigger, and not by the Jenkins Pipeline itself, if you want a static variable to remain in scope once you enter the stage you will need to define it in the proper context - i.e, using the Groovy [def statement](http://groovy-lang.org/semantics.html#_variable_definition) inside the node context, or use [the environment](https://www.jenkins.io/doc/book/pipeline/syntax/#environment)


---

