9

so I have a bash script in which I use the environment variables from Jenkins for example: QUALIFIER=echo $BUILD_ID | sed "s/[-_]//g" | cut -c1-12

Essentially I'm taking the build id, along with job name to determine which script to call from my main script. I want to use python instead so I was wondering whether I can use these variables without the jenkins python api.

I hope the question makes sense. Thanks

3
  • 1
    can you explain what you want? Commented Jun 12, 2013 at 17:42
  • 1
    I need to use the environment variables in Jenkins such as JOB_NAME to decide which script to call. I have a bash script which gets these variables but I wanted to do this in a python script. How would I get these variables in python? Commented Jun 12, 2013 at 17:45
  • see this,docs.python.org/2/library/subprocess.html Commented Jun 12, 2013 at 17:49

2 Answers 2

15

That's what you need if I understand you correctly:

QUALIFIER="$(echo $BUILD_ID | sed "s/[-_]//g" | cut -c1-12)"
export QUALIFIER
python my_script.py

And in your Python script:

import os
qualifier = os.environ['QUALIFIER']

or without the shell part:

import os
import re
qualifier = re.sub(r'[-_]+', '', os.environ['BUILD_ID'])[0:12]
Sign up to request clarification or add additional context in comments.

2 Comments

But would I need my bash script as well as the python one takes the 'QUALIFIER'? The $BUILD_ID makes the jenkins environment variable available to the bash script. Is there somehow I can make it available to the python script without having to use the bash one?
You can access $BUILD_ID exactly the same way (using os.environ) and then remove dashes and slice it in Python.
2

import os

os.environ.get("variable_name")

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.