Reputation: 1255
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
.
Upvotes: 3
Views: 10061
Reputation: 123
I had the same issue and the issue wasn't in the multi-line, which is supported by GitLab now. The problem was running the bash logic in sh shell container docker:dind
or alpine
the fix is running the logic with bash:
job-name:
script:
- apk update && apk add bash
- |+
bash -c '
if [[ -n "$DOWNLOAD_FILE" ]]; then
DOWNLOAD_LIST=($DOWNLOAD_FILE)
for file in ${DOWNLOAD_LIST[@]}; do
echo "downloading $file"
wget -q $file
done
fi
'
Upvotes: 0
Reputation: 31254
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)
Upvotes: 1
Reputation: 8220
For me, using the - |
(mind the space between the -
and the |
) syntax works for multi-line bash scripts:
script:
- |
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
Mind that the lines of the script have to start at the same level of indentation.
See also the official GitLab documentation
Upvotes: 2
Reputation: 310993
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
Upvotes: 2