How to get the Jenkins master IP/hostname inside a pipeline stage executing on a slave?

jenkins pipeline get hostname
jenkins hostname variable
jenkins get hostname of slave
jenkins pipeline get ip
jenkins pipeline get node ip address
launch agent via execution of command on the master
jenkins master slave setup
connect agent to jenkins one of these ways

I have a Jenkins declarative pipeline in which I build in one stage and test in another, on different machines. I also have a Selenium hub running on the same machine as the Jenkins master.

pipeline {
  agent none
  stages {
    stage('Build') {
      agent { node { label 'builder' } }
      steps {
        sh 'build-the-app'
        stash(name: 'app', includes: 'outputs')
      }
    }
    stage('Test’) {
      agent { node { label 'tester' } }
      steps {
        unstash 'app'
        sh 'test-the-app'
      }
    }
  }
}

I'd like for the Selenium tests that run on at the Test stage to connect back to the Selenium hub on the Jenkins master machine, and that means that I need to get the IP address or hostname of the Jenkins master machine from the slave.

Is there a way to do this? The Jenkins master URL / hostname isn't in the environment variables and I'm uncertain how else to get the Jenkins master's IP address.

Not sure if there are better ways to do this, I am able to run

def masterIP = InetAddress.localHost.hostAddress
println "Master located at ${masterIP}"

in my Jenkinsfile. The first time I ran this code in my Jenkinsfile, the build failed with

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: 
Scripts not permitted to use method java.net.InetAddress getHostAddress
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:178)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor$6.reject(SandboxInterceptor.java:243)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:363)
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28)

I had to approve the method signature in Jenkins by navigating to Manage Jenkins > In-process Script Approval.

Distributed builds - Jenkins, How to get the Jenkins master IP/hostname inside a pipeline stage executing on get the IP address or hostname of the Jenkins master machine from the slave. Jenkins slave using pipeline ignores system defined limits. How to get the Jenkins master IP/hostname inside a pipeline stage executing on a slave? 0.

Inspired from @kayvee for using BUILD_URL, the following worked from me:

def getJenkinsMaster() {
    return env.BUILD_URL.split('/')[2].split(':')[0]
}

This returns the master's hostname or IP as it would appear in the build URL. If you also require the port number remove the second split().

CheatSheet: Jenkins & Groovy – CheatSheet, This does require that the master have network access (ingress) to the agent In some cases the agent server will not be visible to the master, so the build, or pipeline, can be allocated to run in specific agents (based on their label). Note that the SSH Slaves plugin does this automatically, so agents  A windows slave node connected to Jenkins server through "Java web start". The system information of the node doesn't have it's IP address. I had to run through all the slaves node we had, and find which machine (ip address) corresponds to the slave node in Jenkins. Is there a way to find the IP address of a slave node from Jenkins itself?

Try this below shell command

def host= sh(returnStdout: true, script: 'echo ${BUILD_URL/https:\\/\\/} | cut -d "/" -f1').trim()
println("Hostname : ${host}")

Jenkins Master-Slave setup and configuration with , Ask for user input, stage('stage2'){ input "OK to go?" } Actively fail current pipeline job, error("Build failed because of this and that..") Check whether Run jenkins jobs in a sequential way, jenkinsfile-sequentially.groovy jnlp, jenkins.​setSlaveAgentPort(-1) Get hostname by ip, get-ip-by-hostname.groovy. I have a Jenkins pipeline and I am trying to get the NODE_NAME of the nested NODE that I switch to. Example: @Library('git lib that we use') import org.our_git_lib.* import java.text.SimpleDateFor

To get current slave host :

Jenkins.getInstance().getComputer(env['NODE_NAME']).getHostName()

To get master host :

Jenkins.getInstance().getComputer('').getHostName()

jenkinsci/kubernetes-plugin: Jenkins plugin to run dynamic , Step by step Jenkins Master-Slave/Agent setup and configuration with detailed Lets go through with all the parameters in the node configuration page as displayed above : Launch agent via execution of command on the master: It starts slave by having ip forwarding is off on master server where jenkins is installed . ${JOB_NAME} isn't resolved in the client name for slaves field I think you are misunderstanding what hostname resolution is used for. Unless you have explicitly changed your client name for slaves setting to use ${hostname} instead of the recommended default, then a failure to resolve the name will never result in a failed build.

[JENKINS-54334] Declarative pipeline tools directive does not , Jenkins plugin to run dynamic agents in a Kubernetes/Docker environment Tested with jenkins/jnlp-slave , see the Docker image source code. It is not required to run the Jenkins master inside Kubernetes. Run the Pipeline or individual stage within a custom workspace - not required unless explicitly stated. pipeline  I am running Jenkins pipline (on Jenkins v2.58) and am trying to get the build to run on a slave not the master. Yet, whatever magic I try in the Jenkinsfile, Jenkins keeps running on master. Yet, whatever magic I try in the Jenkinsfile, Jenkins keeps running on master.

Jenkins Pipeline Tutorial, [Pipeline] node Running on VMWASXXXXX in C:\workspace\msde-jenkinsfile-​sample-windows [Pipeline] { [Pipeline] stage [Pipeline]  In this video, I will talk about creating the Jenkins master-slave configuration. Using this configuration you can run the test script parallelly in different browser at same time. #

BCD 3.2 - How to integrate BCD with Jenkins, The fastest way to get started is to install Docker and run: Open Jenkins by navigating to http://dockerhost:8080/, where dockerhost is the hostname or IP address of your You executed a Groovy script using the Jenkins Pipeline DSL. Started by user anonymous [Pipeline] Allocate node : Start Running on master in  1) having hostname in the workspace name tell us where this workspace is being used. With a hash in the name you need to trawl through logs to see there the workspace was used last. 2) our workspaces are large, switching from hostname to hash means a new workspace and a full checkout.

Comments
  • This won't work if Jenkins runs behind a reverse proxy.