从 Jenkins 管道获取 ssh-steps 输出

Get ssh-steps output from Jenkins pipeline

我对 Jenkins 很陌生,所以问题可能很明显。

我在 Windows 机器上安装了 Jenkins,我需要在远程 nix 机器上 运行 命令,我有一个 ssh 访问权限(通过用户名/密码)。我有一个管道,并为管道使用 ssh-steps 插件,我可以连接并执行命令,但我需要获取命令的输出才能继续,但我找不到正确的方法。

def remote = [:]
remote.name = 'UAT'
remote.host = 'test.domain'
remote.user = 'username'
remote.password = 'pass'
remote.allowAnyHosts = true
stage('Remote SSH') {
  sshCommand remote: remote, command: "ls -ll"
}

是否可以使用这个插件来完成,或者我需要使用另一个?据我了解,此插件是专门为在管道脚本中使用 ssh 而创建的。

试试这个:

def remote = [:]
remote.name = 'UAT'
remote.host = 'test.domain'
remote.user = 'username'
remote.password = 'pass'
remote.allowAnyHosts = true
stage('Remote SSH') {
   def commandResult = sshCommand remote: remote, command: "ls -ll"
   echo "Result: " + commandResult
}

因为没有记录所以不容易弄清楚!

请记住,如果 sshCommand 失败,它不会 return 任何东西。 您可以通过带有 'failOnError: false' 标志的 运行 sshCommand 解决此问题,然后自己处理 return。