Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

groovy - Jenkins dynamic declarative pipeline parameters

Can the parameters in a Jenkins declarative pipeline be dynamic?

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

pipeline {
    // a declarative pipeline
    agent any

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

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

what about user input:

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...