-1

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).

jww
  • 97,681
  • 90
  • 411
  • 885
trainoasis
  • 6,419
  • 12
  • 51
  • 82
  • 3
    Add `set -e` to your remote script? [What does set -e mean in a bash script?](https://stackoverflow.com/q/19622198/3776858) and [Stop on first error](https://stackoverflow.com/q/3474526/3776858) – Cyrus Aug 16 '19 at 07:23
  • @Cyrus thanks, unfortunately it's not much better with set -e, perhaps because I have it in my local script too? It just exits ssh without returning to local script. Also, the command that fails should be allowed to fail and not exit ssh if possible. – trainoasis Aug 16 '19 at 07:31
  • Disable it in your local script with `set +e` before `ssh ...`. – Cyrus Aug 16 '19 at 07:37
  • That works too yeah, thanks! How Do I tell if the error occurred in the ssh commands so that I can adjust output locally accordingly then? Welcome to add an answer with all this and I ll accept that. – trainoasis Aug 16 '19 at 07:42
  • 1
    Maybe use something like `command || exit 42` in your remote script and test return code of ssh in local script. Quick test: `ssh server "exit 42"; [[ $? -eq 42 ]] && echo "Don't panic!"` – Cyrus Aug 16 '19 at 07:50
  • Your latest edit appears to heap on yet another additional requirement which wasn't in the original. If you really need more help, maybe post a new isolated question about your specific problem. But that too is a simple duplicate; `set -e` will cause the script to return an error which you can observe from the caller. Basically `if ssh <<'...'; then : nothing; else echo Failure; fi` – tripleee Aug 16 '19 at 07:57

1 Answers1

2

Ouch, this was an easy one it seems (lucky for me).

Thanks to @Cyrus I added set -e to my ssh commands, then for commands that were allowed to fail just used || true like so:

command || true

See Bash ignoring error for a particular command too.

About checking the ssh status I just kept "set -e" in my ssh commands on the beginning and then immediately after ssh command used :

if [ $? -ne 0 ]; then
    done_alert_ssh_error_and_exit # local function that prints error output and exits
fi
trainoasis
  • 6,419
  • 12
  • 51
  • 82