我有一個引數化的詹金斯管道作業。唯一的引數是 Node 型別(名為 myNode),在這里我始終可以使用 jenkins 服務器的所有節點。

在我的 groovy 管道中,我想設定兩個變數:
- hostname of the node (myNode)
- IP address of the node (myNode)
我嘗試了很多選項,但我無法同時獲得主機名和 IP 地址,而且我也會得到不同的結果,具體取決于從屬設備是 Windows 還是 linux。在我看來,因為 jenkins 知道節點,所以這很簡單,但似乎并非如此。
我試過的:
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 }
String rawIpAddress = addr[0]
ipAddress = rawIpAddress.substring(rawIpAddress.lastIndexOf("/") 1)
print "ipAddress: " ipAddress
return host
}
}
node('master') {
stage('stage1') {
println "hostname: " find_ip(env.myNode)
}
}
如果從屬設備是 Windows,我會正確獲取主機名和 IP 地址。如果是 linux,我會在兩個欄位中都獲得 IP 地址。
提前致謝
uj5u.com熱心網友回復:
def find_ip(node_name){
def node = Jenkins.instance.slaves.find{it.name.trim()==node_name.trim()}
if(node) {
def addr = InetAddress.getByName(node.computer.hostName)
def ip = addr.getHostAddress()
def host = addr.getHostName()
println "host=${host} ip=${ip}"
return host
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/476098.html
標籤:詹金斯 时髦的 詹金斯管道 jenkins-groovy
