Jenkins declarative pipeline dynamic parameters

Viewed 176

The objective is to load parameter values dynamically from inside a Jenkins declarative pipeline. One of the parameters preloads it's values from a function. When the function is called it produces an array of KMS keys, but it is not able to populate the parameter value field. NB - I can get this to work via a scripted pipeline, but wish to do it via the declarative pipeline.

Here is the code

def my_kms_keys=""

def LocalKmsKeys(){

    kms_keys = sh(script: """aws kms list-aliases --region eu-west-1 |jq -r '.Aliases[] |select (.AliasArn |contains(":alias/aws/") | not) | "\\(.AliasArn | split("/")[1]) --- \\(.TargetKeyId)"'""", returnStdout:true).trim()

    return kms_keys.split("\n");
}

pipeline {

    agent { label 'slave' }

    stages {
        stage('Load KMS Keys'){
            steps {
                script {
                    my_kms_keys = LocalKmsKeys()
                    println "Debug my_kms_keys : " + my_kms_keys
                }
            }
        }
    }

    parameters {
        string(name: 'Product', description: 'Product name', defaultValue: "product-1")
        choice(name: 'KmsKeys', description: 'Select Kms key for S3 bucket', choices: my_kms_keys)
        }
}

Any ideas on how to solve this would be much appreciated.

0 Answers
Related