gitlab-ci.yml: syntax error near unexpected token `fi'

Viewed 4637

I am implementing Auto Build in my Gitlab project. For that I am using gitlab-ci.yml file with multi-line YAML blocks containing shell commands, the code is as follows:

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

I have tried many solutions such as putting a ; after the if statement, also after the fi keyword, but nothing seems to work, My job log returns the following syntax error:

syntax error near unexpected token `fi'

I have tried to google it, but other solutions don't seem to work. The shell my runner is using is bash.

2 Answers

As I noted in the comments, the easiest approach would probably to have the script in its own file (make sure it's executable!), and then just call it from gitlab ci.

E.g., you can have a build.sh file:

#!/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

And then call it from the yml:

some_task:
  image: ubuntu:18.04
  script:
  - ./build.sh

The problem is, that gitlab-ci.yml doesn't really allow for multi-line scripts (which is more a restriction of YAML than of gitlab).

So if you don't want to use a script (as suggested by @Mureinik), you could collapse everything into a single line:

  script:
  - if [ "${GITLAB_USER_LOGIN}" != "nadirabbas" ]; then echo "Building"; mkdir -p dist; touch -a dist/index.html; fi

(I also removed the inner if-conditions; as you can use flags to mkdir and touch to get approximately the same behaviour)

Related