Search code examples
pythonpython-3.xgitlabgitlab-cigitlab-ci-runner

Gitlab Job variable to use in python code


I am trying to create a pipeline and have defined some variable in my gitlab job which will be used by python script to populate results.

Is there any way I can call/define those variable in my python script.

For example:

.test-report:
  extends: .test_report_setup
  tags: *def_runners_tags
  variables:
    value-path: ${CI_PROJECT_DIR}/value

Now in my python script I want to call the variable 'value-path' to fetch or read files located in that directory. (Please note that variable is a custom variable on gitlab)

file_path = '<value-path>'    <---- To get the gitlab job variable here

file_in = open(file_path + "id.txt")

Please help me how I can get it in my python script as I am a bit stuck on it.

Any suggestion/help on this will be appreciated. Thanks in advance.


Solution

  • You can access environment variables with os.environ or os.getenv. os.environ is a dict with the environment variables and will fail if you attempt to retrieve a key that doesn't exist. os.getenv is basically os.environ.get, which allows you to set a default value if there is no environment variable with that name.