Issue
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.
Solution
I found the answer here: https://stackoverflow.com/a/37396406/6321777
Set the env vars BEFORE pytest
Example:
FOO=$FOO pytest -m test_foo
Answered By - rHenderson
Answer Checked By - David Goodson (JavaFixing Volunteer)