2017-02-06 70 views
1

我在SSH管道腳本中使用SSH從節點作爲節點。在Jenkins管道中獲取SSH從節點主機名/ IP

有沒有辦法獲取管道(Jenkinsfile)腳本內節點的主機名/ IP?

我正在部署到參數化節點,並希望在腳本結尾處回顯節點的IP。

即:

node('master') { 
    checkout scm 
    stash name: 'deploy', includes: 'modules/ci/,modules/compose/' 
} 

stage ('Deploy to remote server (SSH)') { 
    node(${NODE}) { 
     unstash 'deploy' 
     withEnv(["BRANCH=${BRANCH}"]) { 
      sh "chmod +x modules/ci/deployment/*" 
      sh "modules/ci/deployment/update.sh" 
     } 
     echo 'Deployment was successful, branch ${BRANCH} was deployed to https://104.xx.xxx.xx (node IP/hostname)' 
    } 
} 

回答

2

據我知道有沒有快速簡便的方法來做到這一點。以下檢索曾經在配置的主機字段中輸入的內容:

import jenkins.model.Jenkins; 
import hudson.slaves.SlaveComputer; 
import hudson.slaves.DumbSlave; 
import hudson.plugins.sshslaves.SSHLauncher; 

def getHost() { 
    def computer = Jenkins.getInstance().getComputer(env.NODE_NAME); 
    if (!(computer instanceof SlaveComputer)) { 
    error "Not a ordinary slave"; 
    } 
    def node = computer.getNode(); 
    if (!(node instanceof DumbSlave)) { 
    error "Not a dumb slave"; 
    } 
    def launcher = node.getLauncher(); 
    if (!(launcher instanceof SSHLauncher)) { 
    error "Not a SSHLauncher"; 
    } 
    return launcher.getHost(); 
} 

不幸的是,如果你使用你需要白名單一大堆的方法沙箱,它可能有一些安全方面的後果。最好的辦法是把這個實用方法放在全局公共庫中