1

I'm confused as to why Jenkins is not recognizing my environment variables.

I have a String parameter named VAR_TEST set in the General section of my Jenkins job's /configure screen. I've included env in the Build shell and I see that VAR_TEST is listed as an environment variable in the console output, but then when my python code runs, None is returned.

I've tried .ini, .env, and Jenkinsfiles as suggested in related SO questions, but no luck. I've also tried a few Jenkins plugins but no luck.

Python code:

variable_test = os.getenv['VAR_TEST']

Jenkins console:

JENKINS_HOME=/opt/bitnami/apps/jenkins/jenkins_home
VAR_TEST=1,164,169
MAIL=/var/mail/tomcat
SASL_CONF_PATH=/opt/bitnami/common/etc
LANGUAGE=en_US.UTF-8
GIT_TEMPLATE_DIR=/opt/bitnami/git/share/git-core/templates
USER=tomcat
CI=true
...
{blah blah blah}
...
WORKSPACE_TMP=/opt/bitnami/apps/jenkins/jenkins_home/workspace/MY_PROJECT@tmp
+ sudo pytest -m login --headless --junit-xml=report.xml --html=index.html --maximize-window --save-screenshot --env=qa
============================= test session starts ==============================
platform linux -- Python 3.9.0, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /opt/bitnami/apps/jenkins/jenkins_home/workspace/MY_PROJECT
plugins: xdist-2.2.1, forked-1.3.0, seleniumbase-1.61.0, ordering-0.6, html-2.0.1, metadata-1.11.0, rerunfailures-9.1.1
collected 108 items / 2 errors / 81 deselected / 25 selected

==================================== ERRORS ====================================
_ ERROR collecting my_stuff/tests/test_stuff/test_env_variables_so.py _
my_stuff/tests/test_stuff/test_env_variables_so.py:21: in <module>
    var_test = os.getenv('VAR_TEST').split(",")
E   AttributeError: 'NoneType' object has no attribute 'split'

When I run this code locally in PyCharm, I simply list the Environment Variables in the Edit Configurations... view and the code runs without problem. However, it is not running in Jenkins.

rHenderson
  • 608
  • 6
  • 13

2 Answers2

1

before running your script, you need to export the variables so other shells can find 'em

FOO="bar"
export FOO

or the vaguely-less-execution-safe, but more typo-safe single line
(Why not to export variables on the same line you assign them?)

export FOO="bar"
ti7
  • 16,375
  • 6
  • 40
  • 68
  • that (FOO="bar" --> export FOO) fixes the problem when I execute the test via command line locally ... but the error mentioned above persists when executed in Jenkins. – rHenderson Jul 21 '21 at 16:04
  • @rHenderson try `print(os.environ)` to see what Python has available – ti7 Jul 21 '21 at 16:13
  • POSIX shell explicitly allows for assignments in an `export` statement. I'm not sure what old shells would break on `export FOO="bar"`. – chepner Jul 23 '21 at 19:54
  • @chepner it's certainly allowed, I mean unsafe in that it may not behave the way one expects (especially with things like `set-e`), not that it's truly an unsafe use! specifically, `export` on the same line discards the exit code of a contained command and always succeeds (assuming non-pedantic conditions like insufficient system memory) I've added a link in my answer about it! – ti7 Jul 23 '21 at 22:44
1

I found the answer here: https://stackoverflow.com/a/37396406/6321777

Set the env vars BEFORE pytest

Example: FOO=$FOO pytest -m test_foo

rHenderson
  • 608
  • 6
  • 13
  • ahah - that makes sense - sorry, I didn't spot the `pytest` tag in your original question! I'd recommend marking this as the Answer and also marking your Question as a duplicate – ti7 Jul 23 '21 at 22:49