[英]gitlab-ci.yml: syntax error near unexpected token `fi'
我正在我的 Gitlab 項目中實現 Auto Build。 為此,我將gitlab-ci.yml
文件與包含 shell 命令的多行 YAML 塊一起使用,代碼如下:
if [ "${GITLAB_USER_LOGIN}" != "nadirabbas" ]
then
echo "Building"
if [ ! -d "dist" ]; then mkdir dist; fi
if [ ! -f "dist/index.html" ]; then touch dist/index.html; fi
fi
我嘗試了很多解決方案,例如放置一個;
在 if 語句之后,也在fi
關鍵字之后,但似乎沒有任何效果,我的作業日志返回以下語法錯誤:
syntax error near unexpected token `fi'
我試圖用谷歌搜索它,但其他解決方案似乎不起作用。 我的跑步者使用的外殼是bash
。
正如我在評論中指出的那樣,最簡單的方法可能是將腳本放在自己的文件中(確保它是可執行的!),然后從 gitlab ci 中調用它。
例如,你可以有一個build.sh
文件:
#!/bin/bash
if [ "${GITLAB_USER_LOGIN}" != "nadirabbas" ]
then
echo "Building"
if [ ! -d "dist" ]; then mkdir dist; fi
if [ ! -f "dist/index.html" ]; then touch dist/index.html; fi
fi
然后從 yml 中調用它:
some_task:
image: ubuntu:18.04
script:
- ./build.sh
問題是, gitlab-ci.yml
並沒有真正允許多行腳本(這更多的是 YAML 的限制而不是 gitlab 的限制)。
因此,如果您不想使用腳本(如@Mureinik 所建議的那樣),您可以將所有內容折疊為一行:
script:
- if [ "${GITLAB_USER_LOGIN}" != "nadirabbas" ]; then echo "Building"; mkdir -p dist; touch -a dist/index.html; fi
(我還刪除了內部if
-conditions;因為您可以使用mkdir
和touch
標志來獲得大致相同的行為)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.