0

I have the following Groovy script, called by a Jenkins pipeline job:

import jenkins.model.Jenkins

def foo(){
    Jenkins.instance.getNode('bla').getComputer.disconnect()
}

Checking the getComputer() API, I wasn't able to find a way to also get the IP of the node. Is it possible from within the jenkinsfile / groovy script?

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • This post answers exactly your question: https://stackoverflow.com/questions/14930329/finding-ip-of-a-jenkins-node/39752509#39752509 Most of the other answers will print the IP of the master instead, or will only work on a linux agent – Mzzl Dec 18 '19 at 13:33
  • @Mzzl I can't use this solution. Using the ListPossibleNames throws a "Script1.groovy: 719: unable to resolve class ListPossibleNames " - some kind of import error that I can't resolve – CIsForCookies Dec 18 '19 at 13:54
  • What if you remove the import and use the full `new hudson.model.Computer.ListPossibleNames()` ? – Mzzl Dec 18 '19 at 14:01
  • getChannel() returns null so this crahses... Not sure what should be the value instead "my slave" – CIsForCookies Dec 18 '19 at 14:38

1 Answers1

0

This what worked for me eventually. The trick is to run it from master (so the desired node won't be recognized as "local host"

 def find_ip(node_name){
       for (slave in Jenkins.instance.slaves) {
           host = slave.computer.hostName
           addr = InetAddress.getAllByName(host)
           if (! slave.name.trim().equals(node_name.trim())) { continue }
           return host
       }
 }
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124