手动构建时的通用 webhook 触发器变量

generic webhook trigger variables on manual build

我有一个使用通用 webhook 触发器的 jenkins 管道,运行在触发器内容中提到的分支上构建:

    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'
        )
    }

在作业中,我将变量 ref 定义为分支源:

正确触发时效果很好。

但我想 运行 同样的工作也可以通过 jenkins 中的“立即构建”按钮完成。很明显我必须以另一种方式定义参数,但是如何?

我已经尝试过的:

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

我总是收到错误提示,变量设置不正确:

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

有什么提示吗?非常感谢!

来自 Generic Webhook Trigger 文档:

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!

因此只需将同名参数添加到您的作业中并设置默认值即可:

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

现在可以在手动执行作业、回放作业、钩子触发作业时使用ref参数