Better way for multiline ssh command



currently I use this code to deploy my application:

    - |
        ssh /bin/bash -s << EOT
        cd code
        TIMESTAMP=\$(date +%Y-%m-%d_%H-%M-%S)
        mkdir -p "\$DEPLOY_DIR"
        tar -xzvf code.tar.gz -C "\$DEPLOY_DIR" && rm code.tar.gz
        sudo /usr/sbin/service webapp stop
        ln -snf "\$DEPLOY_DIR" current
        ./current/bin/webapp migrate
        ./current/bin/webapp seed
        sudo /usr/sbin/service webapp start

But this has a few problems:

  • Within the runner logs in the gitlab interface I only see: collapsed multi-line command
  • If one of the commands fails with an non 0 exit code the gitlab runner still marks that deployment as successful

How can this be improved to see everything in the logs and fail properly if something goes wrong within the ssh commands?