我們最近搬到了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