generic webhook trigger variables on manual build

Viewed 1198

I've a jenkins pipeline which uses generic webhook trigger and runs a build on the branch mentioned in the triggers content:

    triggers {
        GenericTrigger (
            genericVariables: [
                [
                    key: 'ref',
                    regexpFilter: '^refs/heads/',
                    value: '$.changes[0].refId',
                    defaultValue: 'master'
                ],
                [
                    key: 'committer_name',
                    value: '$.actor.displayName'
                ]
            ],
            token: 'default_token',
            printContributedVariables: true,
            printPostContent: true,
            causeString: 'Commit on branch $ref by $committer_name'
        )
    }

In the job, I've defined the variable refas branch source: Screenshot

This works well when triggered properly.

But I'd like to run the same job also by means of the "build now" button in jenkins. It's obvious that I've to define the parameter in another way, but how?

What I already tried:

def ref = 'master'
environment {
   ref = 'master'
}

I always get en error indicating, that the variable is not set properly:

> git rev-parse origin/$ref^{commit} # timeout=10

Any hints? Thanks a lot!

1 Answers

From the Generic Webhook Trigger documentation:

The plugin can be configured with default values.
But if you execute the job manually (or replay a pipeline), this default value will not be used. Because the plugin will not be invoked at all.
You can solve this by checking the "This job is parameterized" and add a parameter with the same name as the one you configured in the plugin.
Now this default value will be used both when you trigger the job manually, replaying pipeline, and when you trigger it with the plugin!

so just add a parameter with the same name to your job and set the default value:

parameters {
    string(name: 'ref', defaultValue: 'master', description: 'Git branch', trim: true)
}

Now you can use the ref parameter when the job is executed manually, when it is replayed and when it is triggered by the hook.

Related