Usually I make the following job in my .gitlab-ci.yml to execute commands on a remote server via SSH:
# The job
deploy:
script:
# I've omitted the SSH setup here
- |
ssh gitlab@example.com "
# Makes the server print the executed commands to stdout. Otherwise only the command output is printed. Required for monitoring and debug.
set -x &&
# Executes some commands
cd /var/www/example &&
command1 &&
command2 &&
command3 &&
command4 &&
command5
"
It works correctly, but the YAML code looks too complicated:
- The
set -xcommand is more a boilerplate than a useful code. It's not required for ordinary CI commands because GitLab CI prints them automatically. &&on every line is boilerplate too. They make the execution stop when one of a commands fails. Otherwise the next commands will be executed when one fails (in contrast to ordinary job commands).- All the SSH commands are a single YAML string therefore editors don't highlight the comments and commands so the code is hard to read.
Is there a more clear and convenient way to execute multiple commands on a remote machine through SSH without the cons described above?
I'd like to not use external deployment tools like Ansible to keep the CD configuration as simple as possible (default POSIX/Linux commands are welcome). I'v also considered running each command in a separate ssh call but I'm afraid it may increase the job execution time because of multiple SSH connection establishments (but I'm not sure):
deploy:
script:
- ssh gitlab@example.com "cd /var/www/example"
- ssh gitlab@example.com "command1"
- ssh gitlab@example.com "command2"
# ...