elcortegano
elcortegano

Reputation: 2694

Error using the PWD to define variables in .gitlab-ci.yml

I just noticed that when the PWD variable is accessed under script, it works as one would expect, for instance:

test:
    script:
    - docker run --rm -v $PWD/wd image [options]

But this fails when the above is replaced by a variable containing $PWD. In fact, an instruction like the following will result in an error:

variables:
    DOCKER_RUN: "docker run --rm -v $PWD:/wd"

test:
    script:
    - $DOCKER_RUN image [options]

This is because the variable DOCKER_RUN is being interpreted here as "docker run --rm -v :/wd". That is, like if PWD was empty, I cannot understand why.

I have also been exploring the gitlab predefined variables but cannot find a proper substitute for PWD, not by combining them, eg. using "${CI_BUILDS_DIR}/${CI_PROJECT_NAMESPACE}", which also results in error.

Upvotes: 6

Views: 13994

Answers (3)

Liam
Liam

Reputation: 29754

This trick borrowed from a github issue seems to solve the issue:

Ahh, late to the party, I searched, saw this, and realized if you want a "relative" path you can do this:

docker run -v `pwd`/contrib:/tmp/contrib ...

If you're running from a shell script, this is fine, you're going to be running it from somewhere, great for local development when you're standing up a few images and want to keep those folders handy.

So my command was:

script:
- docker run --rm -t --env -v `pwd`/frontend:/app snyk/snyk:node

Which expanded pwd correctly.

Upvotes: 2

Umair A.
Umair A.

Reputation: 6873

I used $CI_PROJECT_DIR

...

test:
  stage: test
  variables:
    PYTHONPATH: "$CI_PROJECT_DIR"
  script: 
    - pytest test/integration

Upvotes: 6

MrBerta
MrBerta

Reputation: 2758

You can try to use $$PWD when you define the variable instead. Then GitLab shouldn't try to interpret the variable, but instead just store a string docker run --rm -v $PWD:/wd in the variable DOCKER_RUN. As you have written it, GitLab tries to interpret the variable $PWD when parsing the .gitlab-ci.yml file. This is done on the git server, and the current working directory wouldn't make sense in this context.

When you use $PWD in a job, this is executed on a gitlab-runner as a normal shell script. Then the current working directory makes perfect sense!

Upvotes: 3

Related Questions