Use a heredoc to execute many commands remotely

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-04-25

I wanted a low effort build and deploy script for a server accessible via SSH with the alias schillerchorchat.

Here's was the code. Notice how the whole script exists within a HEREDOC passed to SSH:

ssh schillerchorchat << HERE
  # Important: Exit early on failure works here
  set -e

  # NOTICE: Changing a directory also works
  cd webapp
  mv .env.production .env

  rm -rf ./.jitsi-meet-cfg
  mkdir -p ./.jitsi-meet-cfg/{web/letsencrypt,transcripts,prosody,jicofo,jvb}

  docker-compose down -v
  docker-compose up -d
HERE