我们最近搬到了Gitlab并开始使用管线。我们建立了一个构建服务器(Ubuntu 16.04实例)并安装了一个使用Shell执行程序的运行程序,但我不确定它是如何实际执行.gitlab-ci.yml
文件中定义的脚本的。考虑下面的代码片段:Shell执行程序如何运行脚本?
script:
- sh authenticate.sh $DEPLOY_KEY
- cd MAIN && sh deploy.sh && cd ..
- sh deploy_service.sh MATCHMAKING
- sh deauthenticate.sh
我的印象是,它只是管这些命令猛砸,因此我期待默认猛砸行为。但是,发生的情况是deploy.sh
因ssh
错误而失败;然后Bash继续执行deploy_service.sh
(这是预期的行为),但是这会失败,出现can't open deploy_service.sh
错误,并且作业会终止,而Bash不会执行最后一条语句。
根据我的理解,如果您先执行set -e
,那么Bash将只会中止错误,因此我期待所有的语句都被执行。我已经尝试添加set -e
作为第一条语句,但这并没有什么区别 - 它不会因第一个ssh
错误而终止。
我已经添加了精确的输出从Gitlab如下:
没有set -e
$ cd MAIN && sh deploy.sh && cd ..
deploy.sh: 72: deploy.sh: Bad substitution
Building JS bundles locally...
> better-npm-run build
running better-npm-run in x
Executing script: build
to be executed: node ./bin/build
-> building js bundle...
-> minifying js bundle...
Uploading JS bundles to server temp folder...
COMMENCING RESTART. 5,4,3,2,1...
ssh: Could not resolve hostname $: Name or service not known
$ sh deploy_service.sh MATCHMAKING
sh: 0: Can't open deploy_service.sh
ERROR: Job failed: exit status 1
随着set -e
$ set -e
$ cd MAIN && sh deploy.sh && cd ..
deploy.sh: 72: deploy.sh: Bad substitution
Building JS bundles locally...
> better-npm-run build
running better-npm-run in x
Executing script: build
to be executed: node ./bin/build
-> building js bundle...
-> minifying js bundle...
Uploading JS bundles to server temp folder...
COMMENCING RESTART. 5,4,3,2,1...
ssh: Could not resolve hostname $: Name or service not known
$ sh deploy_service.sh MATCHMAKING
sh: 0: Can't open deploy_service.sh
ERROR: Job failed: exit status 1
为什么,没有set -e
,终止误差(也,为什么它只会终止第二个错误而不是ssh
错误)?任何见解将不胜感激。
“SH deploy.sh”你调用'sh'这里不是'bash'。它们在您的系统上可能不是同一件事。如果你想bash,调用bash。 – Kenster
@Kenster'bash'用于在该脚本上调用'sh',并使用适当的退出代码(第一个错误为127,第二个错误为255)将错误返回给'bash'。问题是'bash'没有按照预期检测到错误。 – Amposter
来自文档:#如果构建应该在另一个用户(shell执行程序)的上下文中执行,则使用此命令 'cat generated-bash-script | su --shell/bin/bash --login user' – Amposter