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 {
   [$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") {
  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] {

{"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
+ echo 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?


I've spent a bit of time playing "follow the bouncing ball" through the Generic Webhook Trigger Plugin source code, 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 grabs the trigger for your Jenkins job, which creates a cause object that will then take the list of generic variables you supplied and pass them to the variable resolver which will actually then use the post content parameter resolver 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 inside the node context, or use the environment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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