1

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) ?

2
  • 2
    when using sudo You must pay attention to the option --preserve-env, without it env variables are lost when switching user context. Commented Aug 23, 2017 at 22:00
  • @Take_Care_ If you put this as an answer I will accept it. Commented Aug 24, 2017 at 3:59

1 Answer 1

2

When using sudo You must pay attention to the option --preserve-env, without it env variables are lost when switching user context.

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.