Jenkins dynamic declarative pipeline parameters

前端 未结 3 2136
悲哀的现实
悲哀的现实 2020-12-01 11:13

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 gen

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 12:04

    For anyone needing a declarative pipeline syntax option, I found a good solution in another question, which helped me.

    This is my suggestion based on it. You should be able to generate a more dynamic list with the code that creates the ${WORKSPACE}/list file

    pipeline {
        agent any
        stages {
            stage("Release scope") {
                steps {
                    script {
                        // Prepare a list and write to file
                        sh "echo \"patch\nminor\nmajor\" > ${WORKSPACE}/list"
    
                        // Load the list into a variable
                        env.LIST = readFile (file: "${WORKSPACE}/list")
    
                        // Show the select input
                        env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
                                parameters: [choice(name: 'RELEASE_SCOPE', choices: env.LIST, description: 'What is the release scope?')]
                    }
                    echo "Release scope selected: ${env.RELEASE_SCOPE}"
                }
            }
        }
    }
    

    I hope this helps

提交回复
热议问题