Better way for multiline ssh command

,

currently I use this code to deploy my application:

script:
    - |
        ssh webapp@www.myserver.de /bin/bash -s << EOT
        cd code
        TIMESTAMP=\$(date +%Y-%m-%d_%H-%M-%S)
        DEPLOY_DIR="releases/\$TIMESTAMP"
        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
        EOT

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?

You could put all of your commands into a shell script in your project repo with the “set -e” command at the top so that it errors out whenever any individual command returns a non-zero return code. Then change your SSH command in your .gitlab-ci.yml to look like this:

ssh webapp@www.myserver.de "/bin/bash -s " < yourdeployscript.sh

2 Likes

thanks for your kind help, it makes me sense since meet similar problems recently.