多分支管道中的詹金斯主动选择参数 [英] Jenkins Active Choice Parameter in Multibranch pipeline

查看:150
本文介绍了多分支管道中的詹金斯主动选择参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Jenkins中使用主动选择"参数,用于以下目的:
我的项目开发人员希望为不同的分支机构运行Jenkins构建,基本上会大喊"Multibranch"!事实是,如果未标记回购中的最新提交,则开发人员希望在作业页面(您单击BUILD)中得到通知.

I would like to use the Active Choice Parameter in Jenkins for the following purpose:
My project developers want to run a Jenkins build for different branches, which basically shouts "Multibranch"!! The thing is, that the developers want to get a notification in the Job's page (where you hit BUILD) if the latest commit in the repo is not tagged.

如果我让开发人员通过Git Parameter之类的参数选择他要构建的分支,然后使用Active Choice Reference Parameter引用所选值,那么我可以轻松地做到这一点.

I can easily do this if I let the developer to choose the branch he wants to build via a parameter, such as Git Parameter, and then reference the chosen value with Active Choice Reference Parameter.

问题是,如果我想使用多分支管道,是否有一种方法可以在主动选择参数"脚本中知道选择了哪个分支?我实际上怀疑是否存在这种可能性,但我想在考虑其他解决方案之前先问一下.

The question is, if I want to use Multibranch Pipeline, is there a way to know in the Active Choice Parameter script which branch was chosen?? I actually doubt there is such possibility but I thought I'll ask here before I think of another solution..

推荐答案

Active Choices参数确实很酷,但同时又很奇怪.而且也缺少文档,就像真的,真的很糟糕.

The Active Choices param is really cool, but it's so strange at the same time. And also the documentation is lacking, like really, really bad.

我认为我有一个解决方法,但是根据您使用哪种类型的Active Choice参数,这可能对您不起作用.附言:此解决方案确实不完善.

I think I have a workaround for you, but depending on what kind of Active Choice parameter you use, this may or may not work for you. P.S.: this solution is really not elegant.

[
    $class: 'ChoiceParameter',
    choiceType: 'PT_SINGLE_SELECT',
    description: '',
    filterable: true,
    name: 'TestParam',
    description: 'A description.',
    script: [
        $class: 'GroovyScript',
        fallbackScript: [
            classpath: [],
            sandbox: false,
            script: '''
                return ['error']
            '''
        ], 
        script: [
            classpath: [],
            sandbox: false,
            script: '''
                def branchNameRex = ".+?/job/.+?/job/([^/]+)/.*"
                def threadName = Thread.currentThread().toString()
                def branchName = (threadName =~ branchNameRex)[0][1]
                return [branchName]
            '''
        ]
    ]
]

因此,如果您有幸需要将 ChoiceParameter PT_SINGLE_SELECT 一起使用,并且还处于多分支管道作业"中,则应该可以使用.

So if you are lucky enough to need to use ChoiceParameter with PT_SINGLE_SELECT and also be in a Multibranch Pipeline Job, this should work.

推理. Thread.currentThread().toString()返回当前作业的完整路径,幸运的是包含分支名称.使用该正则表达式,我只会得到分支名称,例如 feature/something main .

The reasoning. Thread.currentThread().toString() returns the full path of the current job which luckily includes the branch name. With that regex, I only get the branch name, like feature/something or main.

对我来说很不幸,我尝试使用 DynamicReferenceParameter ET_FORMATTED_HTML ,并且 Thread.currentThread().toString()返回了其他内容.

Sadly for me, I tried it with DynamicReferenceParameter and ET_FORMATTED_HTML and the Thread.currentThread().toString() returns something else.

这也可能取决于插件的版本,我不确定,但是我已经这样做了,并且在特定情况下也可以工作.

This may also depend on the version of the plugin, I don't know for sure, but I have done it like this and it worked in that specific case.

好吧,经过大量的挖掘,并且我自己也玩了很多,我想我有一个解决方案.您需要两个参数才能使这项工作完成.

Okay, after a lot of digging, and I played with this myself a lot, I think I have a solution. You need two parameters to make this work.

有一个 FORMATTED_HIDDEN_HTML 参数类型不会显示给用户.我们将如下定义该参数

There is a FORMATTED_HIDDEN_HTML parameter type that won't get displayed to the user. We will define this parameter as follows

[
    $class: 'DynamicReferenceParameter',
    choiceType: 'ET_FORMATTED_HIDDEN_HTML',
    name: 'BranchName',
    omitValueField: true,
    script: [
        $class: 'GroovyScript',
        fallbackScript: [
            classpath: [],
            sandbox: true,
            script: '''
                return '<p>error</p>'
            '''
        ], 
        script: [
            classpath: [],
            sandbox: true,
            script: """
                return '<input name="value" value="${env.BRANCH_NAME}" type="text">'
            """
        ]
    ]
]

这里要注意几件重要的事情.

There are a couple of important things to note here.

  1. 返回类型必须为输入类型,否则,您将无法访问其值
  2. 它需要将属性 name 设置为 value .

文档中对此进行了描述.3.注意脚本的三重双引号"" .这是怎么做的,它使用带 $ {env.BRANCH_NAME} 的字符串插值.该值最初不存在.只有在按下Build之后,它才会被填充.

These are described in the docs. 3. Note the script's triple-double quotes """. What this does, it uses string interpolation with ${env.BRANCH_NAME}. This value doesn't exist at first. Only after you press Build it gets populated.

基本上,从第二个版本开始,此参数将与分支名称保持不变.如果您有多分支管道作业,那就太好了.

Basically, from the second build onwards this parameter will remain unchanged with the branch name. This is excellent if you have a multibranch pipeline job.

之后,您可以将其用作其他反应性参数中的参考参数.

After that, you can use it as a reference parameter in the other reactive parameters.

感谢 Michael

Thanks to Michael's answer to my question, I managed to figure this out. I don't know if it suits your needs, but for me it is perfect.

这篇关于多分支管道中的詹金斯主动选择参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆