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

标签 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'
      }
    }
  }
}

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

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

最佳答案

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

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

在我的 Jenkinsfile 中。我第一次在我的 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)

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

关于jenkins - 如何在从站上执行的管道阶段中获取 Jenkins 主 IP/主机名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45578971/

相关文章:

jenkins - 我可以使用 Jenkinsfile 中的函数构建阶段吗?

docker - Jenkins 管道即代码,出现 Docker 错误

jenkins - 如何继续度过 Jenkins 声明式管道语法的失败阶段

java - 如何通过(Maven、TestNG、Java)获取运行时值

java.lang.UnsupportedClassVersionError : com/android/build/gradle/AppPlugin : Unsupported major. 次要版本 52.0

Jenkins 管道 s3 文件下载

jenkins - 检查资源是否被锁定

jenkins - 在声明式 Jenkinsfile 中动态加载代理

ubuntu - Jenkins 通过命令行 Ubuntu 20.04 更新

jenkins - 在 127.0.0.1 上启动 https Jenkins 服务器