【发布时间】:2019-06-30 10:47:51
【问题描述】:
通常我在.gitlab-ci.yml 中做以下工作,通过 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
"
它可以正常工作,但是 YAML 代码看起来太复杂了:
-
set -x命令与其说是有用的代码,不如说是一个样板。普通 CI 命令不需要它,因为 GitLab CI 会自动打印它们。 -
每行上的
&&也是样板文件。当其中一个命令失败时,它们会使执行停止。否则下一个命令将在一个失败时执行(与普通作业命令相反)。 - 所有 SSH 命令都是单个 YAML 字符串,因此编辑器不会突出显示 cmets 和命令,因此代码难以阅读。
有没有更清晰便捷的方式在远程机器上通过 SSH 执行多个命令而没有上述缺点?
我不想使用像 Ansible 这样的外部部署工具来保持 CD 配置尽可能简单(欢迎使用默认 POSIX/Linux 命令)。我也考虑过在单独的ssh 调用中运行每个命令,但我担心它可能会因为多个 SSH 连接建立而增加作业执行时间(但我不确定):
deploy:
script:
- ssh gitlab@example.com "cd /var/www/example"
- ssh gitlab@example.com "command1"
- ssh gitlab@example.com "command2"
# ...
【问题讨论】:
标签: linux shell ssh gitlab-ci continuous-deployment