0

I am trying to update a Gitlab environment variable from a python scripts :

In the gitlab-ci.yml

gitlab_job:
  stage: gitlab_stage
  script:
    - python set_myvar.py
    - echo $MYVAR

In the set_myvar.py

import os
os.environ["MYVAR"] = my_value

I don't need the variable to persist. I just need it to be programmaticaly updated (from python).

So far, it doesnt do anything.

1
  • os.environ doesn't allow you to modify your shell's environment variables, it's simply a dictionary representation of them. You'd be much better off setting the environment variable in bash, and using the python to create an output for it. Something like export MYVAR=$(python -c 'print("NewValue")') Commented Feb 10, 2020 at 16:38

1 Answer 1

1

That won't work. The UNIX process model requires certain attributes to be private to a process. This includes such things as the current working directory of the process and its environment variables. Each of these private attributes is inherited; either implicitly or explicitly depending on how the child process is spawned by its parent process. A child process cannot modify those private attributes of its parent.

There are ways to workaround the aforementioned limitation. For example, the child process could write the new var=value pairs to stdout. The parent process that runs your python program can then read those strings and add them to its environment. In your case your python program would do print("MYVAR=my_value") and you would run it from your gitlab script as eval $(python set_myvar.py).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.