'Run multiple SSH commands in the same SSH session, fail/exit on first error
I have a step in my Jenkins file, like this:
withCredentials([
file(credentialsId: 'mysrv-key', variable: 'keyfile'),
string(credentialsId: 'mysrv-host', variable: 'srvhost')
]) {
sh '''ssh -v -o StrictHostKeyChecking=no -i ${keyfile}
root@${srvhost} /bin/bash << EOF
echo "0"
grep "inexisting-text" /path/to/somefile
echo "1"
EOF'''
}
This experiment is to see if grep returning 1 would fail the sh step, but doesn't seem to. "1" is also printed in the output.
Is it possible to run several commands through SSH, but without having to run multiple ssh commands, so that we don't have to open a new shell session for every single of them?
Something equivalent to running sh, but all wrapped in an SSH session. Similar to how we do withCredentials, and run stuff under that block.
Solution 1:[1]
It's not obvious what are you asking. Your question (highlighted in bold in your post) is:
Is it possible to run several commands through SSH, but without having to run multiple ssh commands, so that we don't have to open a new shell session for every single of them?
but before that question you demonstrate that you already run several commands (grep and echo) in a single SSH session
If your goal is to prevent further commands if grep command fails then you have to add set -o errexit (or set -e) to your EOF block.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
