2

I wrote a very simple .gitlab-ci.yml as below:

image: horchen/vscode-ext-dev

job1:
  script:
    - echo "Hello World"

Just echo Hello World, I think it should be able to run on any platform. But gitlab-ci always tells me there is an syntax error in it.

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

Does anyone know why it reports such an error?

--------------- Append Data -------------------

Actually I really developed on windows but I have checked the CR/LF style, also checked hexdump. And have tried to edit it on webIDE and linux VIM, both didn't find ^M in it.

  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".
chhx001
  • 99
  • 1
  • 10
  • I posted an answer below, but there may be other possible causes of this error. Let me know if my solution works. – DV82XL Aug 17 '20 at 12:58
  • What kind of image is `horchen/vscode-ext-dev`? Windows or Linux? What version exactly? Also, I added another troubleshooting step to my answer. Let me know if it helps. – DV82XL Aug 18 '20 at 02:36

1 Answers1

5

The step_script stage of the job includes the before_script and script sections. Your executor is trying to execute your script section, which is causing the error.

End of line error

The unexpected end of file (expecting "then") error is often seen with Windows vs. Linux file formats. See Syntax error: end of file unexpected (expecting "then"). Try converting your gitlab-ci.yml file to have Linux line endings and see if that fixes it. There are a number of tools that can do this (see link above), including Notepad++.

If this is your issue, you may have a bigger problem: are you developing in Windows and running code in Linux? If so, you may need to setup your .gitattirbutes file to automatically convert Windows EOL characters to Linux and vice versa. See How to change line-ending settings.

Add second job without echo

If that doesn't work, then try adding another job to your pipeline that doesn't print anything. Just add something like x=2 to the script section. This might eliminate the echo in the script as a source of error.

Assign jobs to a stage

Just to be safe, create a stages section and assign both jobs to the same stage, like this:

stages:
  - test

job2:
  stage: test
  script:
    - x = 2

Troubleshoot Dockerfile

If that still doesn't help, then I would start suspecting your Docker image: horchen/vscode-ext-dev. It's possible that GitLab is trying to execute the script from the job in the container, which may be executing some code first. You may need to post your Dockerfile to troubleshoot that.

I would also suspect the EOL CRLF/LF characters of any scripts called by the Dockerfile.

DV82XL
  • 5,350
  • 5
  • 30
  • 59
  • Not this cause, I have checked the CRLF/LF style on my windows machine and verified it in hexdump(edited above). Also tried to edit it on webIDE and linux VIM, still didn't find any ^M. – chhx001 Aug 18 '20 at 02:19
  • You are right, it is docker image issue, I changed to ubuntu:latest then the echo works. It seems the image I made from webhippie/nodejs has some problems. – chhx001 Aug 19 '20 at 02:39
  • 1
    @chhx001 Yeah! Now on to bigger, better problems! Good luck with your project! – DV82XL Aug 19 '20 at 04:34