LoginSignup
4
1

More than 5 years have passed since last update.

JenkinsPipelineで多段SSHした時に1コマンドしか実行されずconnection to server1 closedになる対処

Last updated at Posted at 2018-04-24

やりたいこと

jenkins pipleneから多段sshをしてコマンドを実行する.(commandにはパラメータからの文字列を代入するものもある)

stage('TEST') {
    sh 'ssh -t -t server1 \'ssh server2 \" command1 && command2 && ...\"\''
}

※なぜかcommand1の前に空白がないとcommand1さえ実行されなかった...
-t -tをつけないとPseudo-terminal will not be allocated because stdin is not a terminal.になった.
参考: ssh を用いてリモートでスクリプトを実行

問題

最初のコマンドのみしか実行されない. command2以降のコマンドが実行されずconnecion closedになる.

[sso_stg@2] Running shell script
+ ssh -t -t server1 'ssh server2 'command1
Connection to server1 closed.

解決策

シングルクオートとダブルクオートの順番を気をつける.
順番は以下に示す通り.

stage('TEST') {
    sh "ssh -t -t server1 'ssh server2 \" command1 && command2 && ...\"'"
}

まとめ

ダブルクオートなのかシングルクオートを使うのかではまったのでメモをしました.jenkinsからパラメーターを入力してそれをcommandに含める時の場合は上記クオートの順番で試してみてください.

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1