I am defining a variable JWT
, I will store in it a token which I will use later inside the code.
I'm going to obtain it at the before_script
step through a curl call.
The problem is that when I try to run the pipeline, it fails with the error:
Found errors in your .gitlab-ci.yml: Included file
.gitlab-ci.yml
does not have valid YAML syntax!
I have already read this Stack Overflow answer in order to properly interpolate USER
and PASS
environment variables.
This is my .gitlab-ci.yml
file:
build-dist:
environment:
name: develop
variables:
JWT: ""
stage: build
image: node:16-alpine
cache:
key:
files:
- yarn.lock
paths:
- node_modules
- .yarn
before_script:
- if [ -z "$USER" ] || [ -z "$PASS" ]; then exit 1; fi
- apk add curl
- JWT=$(curl -s -X POST -H "Content-Type: application/json" -d '{"username": "'"$USER"'","password": "'"$PASS"'"}' "https://example.com/token")
script:
- yarn install --pure-lockfile --cache-folder .yarn
- yarn build
How should I correct the follow line inside my .gitlab-ci.yml
in order to make it work?
- JWT=$(curl -s -X POST -H "Content-Type: application/json" -d '{"username": "'"$USER"'","password": "'"$PASS"'"}' "https://example.com/token")
CodePudding user response:
First, a handy tip for such GitLab CI issues:
- assuming your have a GitLab repo on
https://gitlab.com/user/project
, - you can browse the page
https://gitlab.com/user/project/-/ci/lint
- then paste the contents of the offending
.gitlab-ci.yml
file, and click on "Validate" to get more feedback (typically, the error line number, etc.)
Regarding the YAML snippet at stake, the crux of the issue is the - JWT=$(…)
line as you mentioned in the question, more precisely:
- from a YAML point of view, the string
JWT=…
is not explicitly quoted, - and as this text contains a
:
, - the YAML parser then sees it as a map, i.e., as if you had written:
- username: "NAME" other_key: "val"
To solve this, it appears you'd just need to "quote" this sequence item with '
or "
and thus write a line of the form - "JWT=…"
, then escape the quotes accordingly inside… but I'd rather suggest (to avoid ugly escapes!) to rely on the so-called block style of YAML → this leads to:
- |
JWT=$(curl -s -X POST -H "Content-Type: application/json" -d '{"username": "'"$USER"'","password": "'"$PASS"'"}' "https://example.com/token")