Home > Blockchain >  How to bring list of jenkins nodes to Active choice parameter and present only the current logged in
How to bring list of jenkins nodes to Active choice parameter and present only the current logged in

Time:11-08

I have Jenkins job with active choice parameter with the name Computer. Which groovy script I should write to bring me all the Nodes names, and present only the one for the current logged in user? All nodes are with the label Desktop. I tried the below but it doesn't work:

import hudson.slaves.DumbSlave

def users = []
for (DumbSlave it : Hudson.instance.getSlaves()) {
  if(it.getLabelString().contains("Desktop")) {
    def currAgent = it?.computer?.name
    if(currAgent != null) {
      users.add(currAgent)
     }
  }
}

return users

Update: The suggestion answer present list of nodes, but Now I want to present only for the current logged in user. something like (it doesn't work for me):

import jenkins.model.*

def users = []
Jenkins.instance.nodes.each { node ->
    if(node.labelString.contains("Desktop")) {
        def currAgent = node?.computer?.name
        if(currAgent != null) {
            if(it.computer.systemProperties.containsKey('user.name')) {
                def user = it.computer.systemProperties['user.name'].toLowerCase()
                if(user != null) {
                    if(users.containsKey(user)) {
                        users.add(currAgent)
                    } 
                    else {
                        users[user] = [currAgent]
                    }
                }
            }
        }
    }
}

return users

CodePudding user response:

Try this:

import jenkins.model.*

def users = []
Jenkins.instance.nodes.each { node ->
    if(node.labelString.contains("Desktop")) {
        def currAgent = node?.computer?.name
        if(currAgent != null) {
            users.add(currAgent)
        }
    }
 }

 return users

Or make it Groovyer:

import jenkins.model.*

return Jenkins.instance.nodes.findAll { node ->
   node.labelString.contains("Desktop")}.collect { node ->
     node?.computer?.name } 
  • Related