Jenkins动态声明式管道参数 [英] Jenkins dynamic declarative pipeline parameters

查看:94
本文介绍了Jenkins动态声明式管道参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jenkins声明性管道中的参数可以动态吗?

Can the parameters in a Jenkins declarative pipeline be dynamic?

我希望选择选项值在运行时由函数填充.以下代码确实生成了选项列表,但它们似乎过时了-可能是在我第一次运行此代码时生成的.如果AMI列表更改,则选择将保持不变.我希望每次选择build with parameters时都可以运行.

I want a the choice option values be populated at runtime by a function. The following code does generate a list of options, but they seem to be stale - probably generated on the first time I ran this code. If the list of AMIs changes, the choices remain the same. I want this to run every time I select build with parameters.

def findAMIs() {
    // Find relevant AMIs based on their name
    def sout = new StringBuffer(), serr = new StringBuffer()
    def proc = '/usr/bin/aws --region eu-west-1 ec2 describe-images \
               ' --owners OWNER --filter Name=name,Values=PATTERN \
               ' --query Images[*].{AMI:Name} --output  text'.execute()
    proc.consumeProcessOutput(sout, serr)
    proc.waitForOrKill(10000)
    return sout.tokenize() 
}

def AMIs = findAMIs().join('\n')

pipeline {
    // a declarative pipeline
    agent any

    parameters {
        choice(name: 'Release',
               choices: AMIs)
    }
    ...
 }

编辑 我最终使用了带有扩展选择参数的jenkins-job-builder.它目前不支持groovyScript参数,因此我对其进行了修改 https: //review.openstack.org/#q,I0c6ac0b49c24b8d3afbc06b003847de2e043c2b8,n,z

EDIT I ended up using jenkins-job-builder, with extended choice parameters. It does not support the groovyScript parameter at the moment, so I modified it https://review.openstack.org/#q,I0c6ac0b49c24b8d3afbc06b003847de2e043c2b8,n,z

编辑 上面的链接已失效,因此这是指向openstack的另一个链接: https://review.opendev.org/#/c/477003/ 但是,要点是,我已经在jenkins-job-builder中添加了一个名为"groovyScriptFile"的新参数,该参数已合并.

EDIT The above link went dead, so here is another link to openstack: https://review.opendev.org/#/c/477003/ But the gist of the matter is I have added a new parameter to jenkins-job-builder called 'groovyScriptFile', which was merged.

推荐答案

关于用户输入的内容:

def findAMIs() {
    return UUID.randomUUID().toString().split('-').join('\n')
}

node{
    def userInput = input(
        id: 'userInput', message: 'input parameters', parameters: [
            [
                $class: 'ChoiceParameterDefinition',
                name: 'ami',
                choices: findAMIs(),
                description: 'AMI',
            ],
        ]
    )

    echo ("Selected AMI :: "+userInput)
}

这篇关于Jenkins动态声明式管道参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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