Reputation: 1398
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 (full answer with logged-on user, based on the below answer):
import jenkins.model.*
import hudson.model.User
def users = []
Jenkins.instance.nodes.each { node ->
if(node.labelString.contains("Desktop")) {
def currAgent = node?.computer
def currAgentName = node?.computer?.name
if(currAgentName != null) {
def user = currAgent.systemProperties['user.name']?.toLowerCase()
if (!users.contains(user) && user==User.current().toString()){
users.add(currAgentName)
}
}
}
}
return users
Upvotes: 3
Views: 1069
Reputation: 2966
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") &&
node.computer.systemProperties["user.name"] == User.current().toString()
}.collect { node ->
node?.computer?.name }
Upvotes: 5