Define variable in Gitlab's CI/CD pipeline with bash code?

Viewed 466

I would like to define a variable in Gitlab's CI/CD pipeline (.gitlab-ci.yml) in this spirit:

    variables:
      - TOP_DIR=$(git rev-parse --show-toplevel)

But this doesn't work, I think because the value field must be an actual string. Any workaround? Should I just create a preliminary job that does export TOP_DIR=$(git rev-parse --show-toplevel)?

1 Answers

Export the variable at the top of your script.

job-name:
  script:
    - export TOP_DIR=$(git rev-parse --show-toplevel)
    - # npm run test # etc

GitLab has a number of useful predefined environment variables that you can reference in .gitlab-ci.yml.

Related