jenkins pipeline: multiline shell commands with pipe

前端 未结 3 468
青春惊慌失措
青春惊慌失措 2021-01-07 16:51

I am trying to create a Jenkins pipeline where I need to execute multiple shell commands and use the result of one command in the next command or so. I found that wrapping t

3条回答
  •  不思量自难忘°
    2021-01-07 17:09

    The following scenario shows a real example that may need to use multiline shell commands. Which is, say you are using a plugin like Publish Over SSH and you need to execute a set of commands in the destination host in a single SSH session:

    stage ('Prepare destination host') {
      sh '''
          ssh -t -t user@host 'bash -s << 'ENDSSH'
          if [[ -d "/path/to/some/directory/" ]];
          then
              rm -f /path/to/some/directory/*.jar
          else
              sudo mkdir -p /path/to/some/directory/
              sudo chmod -R 755 /path/to/some/directory/
              sudo chown -R user:user /path/to/some/directory/
          fi
    ENDSSH'
         '''
    }
    

    Special Notes:

    • The last ENDSSH' should not have any characters before it. So it should be at the starting position of a new line.
    • use ssh -t -t if you have sudo within the remote shell command

提交回复
热议问题