gitlab-ci.yml:意外标记“fi”附近的语法错误
gitlab-ci.yml: syntax error near unexpected token `fi'
我正在我的 Gitlab 项目中实现自动构建。为此,我使用 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
关键字后,但似乎没有任何效果,我的作业日志 returns 以下语法错误:
syntax error near unexpected token `fi'
我试过 google 它,但其他解决方案似乎不起作用。我的跑步者使用的 shell 是 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
确实不允许 multi-line 脚本(这是 YAML 的限制,而不是 gitlab 的限制)。
因此,如果您不想使用脚本(如@Mureinik 所建议),您可以将所有内容折叠成一行:
script:
- if [ "${GITLAB_USER_LOGIN}" != "nadirabbas" ]; then echo "Building"; mkdir -p dist; touch -a dist/index.html; fi
(我还删除了内部 if
条件;因为您可以使用 mkdir
和 touch
的标志来获得大致相同的行为)
我正在我的 Gitlab 项目中实现自动构建。为此,我使用 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
关键字后,但似乎没有任何效果,我的作业日志 returns 以下语法错误:
syntax error near unexpected token `fi'
我试过 google 它,但其他解决方案似乎不起作用。我的跑步者使用的 shell 是 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
确实不允许 multi-line 脚本(这是 YAML 的限制,而不是 gitlab 的限制)。
因此,如果您不想使用脚本(如@Mureinik 所建议),您可以将所有内容折叠成一行:
script:
- if [ "${GITLAB_USER_LOGIN}" != "nadirabbas" ]; then echo "Building"; mkdir -p dist; touch -a dist/index.html; fi
(我还删除了内部 if
条件;因为您可以使用 mkdir
和 touch
的标志来获得大致相同的行为)