Jenkins version: 2.74
As part of my build step I am running a python script and was hoping to access Jenkins build variables like BUILD_ID, BUILD_NUMBER, BUILD_URL etc.
I did the following in my script just to try it out but did not see any of the keys above listed:
import os
print os.environ
Output:
{
'USERNAME': 'root',
'LANG': 'en_US.UTF-8',
'TERM': 'xterm-256color',
'SHELL': '/bin/bash',
'XDG_RUNTIME_DIR': '/run/user/1000',
'SUDO_COMMAND': '/bin/su myuser -c python jenkinstest.py',
'SHLVL': '1',
'HOME': '/usr/home1/myuser',
'SUDO_UID': '117',
'SUDO_GID': '125',
'PWD': '/var/lib/jenkins/workspace/script-test',
'LOGNAME': 'myuser',
'USER': 'myuser',
'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games',
'MAIL': '/var/mail/myuser',
'SUDO_USER': 'jenkins',
'XDG_SESSION_ID': '6',
'_': '/usr/bin/python'
}
Why so?
And how can I access the keys I mentioned above from within the python script (without using the Jenkins python/json API) ?
sudoYou must pay attention to the option--preserve-env, without it env variables are lost when switching user context.