Jenkins 依次执行 ssh 命令

Jenkins execute sshCommand sequentialy

我正在尝试使用带有 SSHCommand 的管道执行多个命令,但我无法执行我想要的操作:

pipeline {
   agent any

   stages {
      stage('Generate File') {
         steps {
           script {
             def remote = [:]
             remote.name = 'test'
             remote.host = 'xxxxxxx'
             remote.user = 'xxxxxxx'
             remote.port = xxxxxxxx
             remote.password = 'xxxxxxxx'
             remote.allowAnyHosts = true
             sshCommand remote: remote, command: "cd /var/my/directory"
             sshCommand remote: remote, command: "touch pipeline"
           }
         } 
      }
   }
}

但是文件“pipeline”是在“/home/myUser”中创建的,而不是在/var/my/directory中创建的

我该怎么做?我需要使用“&&”?

这行得通:

sshCommand remote: remote, command: "cd /var/my/directory && touch MyFile"

但我不喜欢“单行”

谢谢,

基本上发生的事情是,每次调用 sshCommand 都会启动它自己的 shell(非常类似于您要在计算机上打开两个不同的终端)。命令后 shell 再次关闭。

因此您的代码可以:

  1. 打开远程 shell(在主目录中)
  2. 转到目录 /var/my/directory"
  3. 关闭shell
  4. 开始一个新的(在主目录中)
  5. 触摸管道

解决此问题的一种方法是使用单个命令 touch /var/my/directory/pipeline 或如您所说 &&。如果你想把它放在多行中,你可以使用 \.

sshCommand remote: remote, command: "cd /var/my/directory \
                                      && touch pipeline"