如何在从站上执行的管道阶段中获取 Jenkins 主 IP/主机名?

5 jenkins jenkins-pipeline jenkins-declarative-pipeline

我有一个 Jenkins 声明式管道,我在一个阶段构建并在另一个阶段在不同的机器上进行测试。我还有一个 Selenium 集线器,它与 Jenkins 主机在同一台机器上运行。

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'
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望在测试阶段运行的 Selenium 测试连接回 Jenkins 主机上的 Selenium 集线器,这意味着我需要从从机获取 Jenkins 主机的 IP 地址或主机名。

有没有办法做到这一点?Jenkins 主 URL / 主机名不在环境变量中,我不确定如何获取 Jenkins 主的 IP 地址。

iva*_*sim 5

不确定是否有更好的方法来做到这一点,我可以运行

def masterIP = InetAddress.localHost.hostAddress
println "Master located at ${masterIP}"
Run Code Online (Sandbox Code Playgroud)

在我的詹金斯文件中。我第一次在 Jenkinsfile 中运行此代码时,构建失败并显示

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)
Run Code Online (Sandbox Code Playgroud)

我必须通过导航到Manage Jenkins>来批准 Jenkins 中的方法签名In-process Script Approval