Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Jenkins Environment variables in python script

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

like image 218
Asad S. Malik Avatar asked Jun 12 '13 17:06

Asad S. Malik


People also ask

How do I run an environment variable in python?

With the environ dictionary variable value of the environment variable can be set by passing the key in the dictionary and assigning the value to it. With setdefault a default value can be assigned to the environment variable. Bypassing the key and the default value in the setdefault method.

How do I read Jenkins environment variables?

An easy way to obtain the Jenkins environment variables list from your local installation is to append env-vars. html to the server's URL. For a locally hosted Jenkins server, the URL would be: http://localhost:8080/env-vars.html.


1 Answers

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]
like image 191
Tomasz Elendt Avatar answered Sep 18 '22 12:09

Tomasz Elendt