Home > Back-end >  Jenkins groovy Prompt with choices parameters based
Jenkins groovy Prompt with choices parameters based

Time:08-24

I have a nodenames that is going to be changing in a variable with each node on each line for example:

node1
node2
node3 

The node will be assigned to variable, but because the node name will be dynamic am looking for way that the prompt will ask the user to choose from the chocie node 1 or node 2 or node 3 in the params

For example

def choosenode(nodename) {
    def nodechosen = input(
            id: 'userInput',
            message: 'Choose:',
            parameters: [[
                                 $class     : 'ChoiceParameterDefinition',
                                 choices    : nodename.join("\n"),
                                 description: 'choosing node during prompt',
                                 name       : 'nodename'
                         ]]
    )
    return nodechosen
}

CodePudding user response:

Do you want to do something like below?

pipeline {
    agent any
    
    stages {
        stage('Stage') {
            steps {
                script {
                    def nodechosen = input message: 'Choose', ok: 'Next',
                                    parameters: [choice(name: 'Node', choices: gerNodeNames(), description: 'Select something')]
                                    
                    node(nodechosen) {
                        echo "Running in Selected node"
                    }
                   
                }
                
            }
        }
    }
}

def gerNodeNames() {
    // Dummy code to create a file with node names
    sh "echo 'node1' >> nodeList.txt"
    sh "echo 'node2' >> nodeList.txt"
    sh "echo 'node3' >> nodeList.txt"
    
    // Reading the file
    def data = readFile(file: 'nodeList.txt')
    
    return data
}
  • Related