2017-08-08 55 views
1

我有一个Jenkins声明式管道,我在一个阶段中构建并在另一个阶段在不同的机器上测试。我还有一台和Jenkins主机一样运行的Selenium集线器。如何获得在一个从机上执行的流水线阶段内的Jenkins主IP /主机名?

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测试,这意味着我需要得到的IP地址或主机名詹金斯从奴隶主掌握机器。

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

回答

1

不知道是否有更好的方法来做到这一点,我能够在我的Jenkinsfile运行

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

。我第一次跑在我的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>In-process Script Approval批准在詹金斯方法签名失败。

0

试试这个下面从@kayvee启发使用BUILD_URL shell命令

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

,下面由我工作:

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

这将返回主人的主机名或IP,因为它会出现在建立网址。如果您还需要端口号,请删除第二个split()