I have a deploy script which builds production assets locally and transfers them to the server via scp in a .zip format.
Then it uses ssh to connect there and execute a series of commands besides unzipping the transferred file.
An example that works:
# local script commands here ...
# ssh with remote commands
ssh $SSH_SERVER -p $SSH_PORT <<- HERE
cd $REMOTE_PATH
# move our zip to /www folder
mv $LOCAL_TEMP_FOLDER_NAME.zip ../
# move to www
cd ..
# zip our whole domain folder (backup)
zip -q -r $DOMAIN_FOLDER_IN_WWW.zip $DOMAIN_FOLDER_IN_WWW
# remove contents of our domain folder
rm -rf $DOMAIN_FOLDER_IN_WWW/*
# unzip our new deploy folder in our domain folder
unzip -q -o $LOCAL_TEMP_FOLDER_NAME
mv $LOCAL_TEMP_FOLDER_NAME/* $DOMAIN_FOLDER_IN_WWW
# remove our deploy folder zip and the unzipped (now empty) folder too
rm -rf $LOCAL_TEMP_FOLDER_NAME.zip $LOCAL_TEMP_FOLDER_NAME
# activate our theme & plugins
cd $DOMAIN_FOLDER_IN_WWW
wp theme activate mytheme/resources
wp plugin activate --all
# assign menus
wp menu location assign main-menu primary_navigation
wp menu location assign footer-menu footer-menu
exit 0
HERE
# local script commands here ...
Q: How do I stop execution and exit prematurely if any command fails? And continue with the local script of course.
Most of them are conditional and the previous commands need to work for the next one to work too.
A sub-question here, but important in my case: can I set which commands are conditional and which are not (which will trigger exit) - for example some are not that important and can "fail", but moving to a folder and zip/unzip should not.
And how to tell if there was an error programmatically back in my local script? Would be nice to adjust the output and not say "Deploy successful" :)
Edit: if I add "set -e" on top of ssh commands it exits and does not return to local script. I also cannot control which commands are allowed to fail (setting up menus can fail for example, not an issue).