很简单gitlab-ci.yml报语法错误

Very simple gitlab-ci.yml reports syntax error

我写了一个非常简单的.gitlab-ci.yml如下:

image: horchen/vscode-ext-dev

job1:
  script:
    - echo "Hello World"

echo Hello World就好了,我觉得应该可以在任何平台运行。但是 gitlab-ci 总是告诉我里面有语法错误。

Running with gitlab-runner 13.2.2 (a998cacd)
  on nodejs-ci aPuhC2uZ
Preparing the "docker" executor
Using Docker executor with image horchen/vscode-ext-dev ...
Using locally found image version due to if-not-present pull policy
Using docker image sha256:17af24ad877daa656e102508585c7262585be04b05a75cc89833afcfe7403ebb for horchen/vscode-ext-dev ...
Preparing environment
Running on runner-apuhc2uz-project-1-concurrent-0 via cf9f41a14878...
Getting source from Git repository
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /builds/horchen/vscode-rlcv/.git/
Checking out 93d3b0e1 as master...
Skipping Git submodules setup
Executing "step_script" stage of the job script
[: line 1: syntax error: unexpected end of file (expecting "then")
ERROR: Job failed: exit code 2

有谁知道为什么会报这样的错误吗?

----------------追加数据--------------------

其实我是在windows上开发的,但是我检查了CR/LF风格,也检查了hexdump。并尝试在 webIDE 和 linux VIM 上编辑它,但都没有在其中找到 ^M。

  Offset: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F   
00000000: 69 6D 61 67 65 3A 20 68 6F 72 63 68 65 6E 2F 76    image:.horchen/v
00000010: 73 63 6F 64 65 2D 65 78 74 2D 64 65 76 0A 0A 61    scode-ext-dev..a
00000020: 66 74 65 72 5F 73 63 72 69 70 74 3A 0A 20 20 2D    fter_script:...-
00000030: 20 22 72 6D 20 2A 2E 76 73 69 78 22 0A 0A 6A 6F    ."rm.*.vsix"..jo
00000040: 62 31 3A 0A 20 20 73 63 72 69 70 74 3A 0A 20 20    b1:...script:...
00000050: 20 20 2D 20 65 63 68 6F 20 22 48 65 6C 6C 6F 20    ..-.echo."Hello.
00000060: 57 6F 72 6C 64 22 0A                               World".

作业的 step_script 阶段包括 before_scriptscript 部分。您的执行人正在尝试执行您的 script 部分,这导致了错误。

行尾错误

unexpected end of file (expecting "then") 错误经常出现在 Windows 与 Linux 文件格式中。参见 Syntax error: end of file unexpected (expecting "then")。尝试将您的 gitlab-ci.yml 文件转换为具有 Linux 行结尾,看看是否可以解决问题。有许多工具可以执行此操作(请参阅上面的 link),包括 Notepad++.

如果这是你的问题,你可能有一个更大的问题:你是在Windows和运行代码中开发Linux吗?如果是这样,您可能需要设置 .gitattirbutes 文件以自动将 Windows EOL 字符转换为 Linux ,反之亦然。参见 How to change line-ending settings

不带回显添加第二个作业

如果这不起作用,请尝试向您的管道添加另一个不打印任何内容的作业。只需将 x=2 之类的内容添加到 script 部分。这可能会消除 script 中的 echo 作为错误来源。

将作业分配到阶段

为了安全起见,创建一个 stages 部分并将两个作业分配到同一阶段,如下所示:

stages:
  - test

job2:
  stage: test
  script:
    - x = 2

故障排除 Docker文件

如果仍然没有帮助,那么我将开始怀疑您的 Docker 图片:horchen/vscode-ext-dev。 GitLab 可能正在尝试从容器中的作业执行 script,这可能会先执行一些代码。您可能需要 post 您的 Docker 文件来解决这个问题。

我还怀疑 Docker 文件调用的任何脚本的 EOL CRLF/LF 字符。