user260399
user260399

Reputation: 111

Return value from Python script to Jenkins variable

  1. I have a Jenkins job which executes a Python script (checkoutDevice.py) via shell.
  2. In checkoutDevice script, it connects to a inventory server and check out an available unit, unit's MAC address is available to return to Jenkins job
  3. I would like to return unit's MAC address from Python script to Jenkins job, so Jenkins job can pass that MAC address to another Python script.

a. How would I store unit's MAC address to Jenkins' environment variable so I can pass it to another Python script in the same job?

b. Another solution I am looking at is to write MAC address to a text file during execution of checkoutDevice script, then Jenkins will read that MAC address from the text file to store into a variable then pass to another Python script?

Upvotes: 3

Views: 11076

Answers (2)

Alex Fortin
Alex Fortin

Reputation: 91

Assuming you're using a recent version of Jenkins, one neat trick is to use Stdout to populate the groovy variable of the Jenkinsfile. Assuming your python script is not too chatty, meaning it only prints to stdOut the value you want to get out of the python script, you can do:

mac = sh(script: 'python checkoutDevice.py',returnStdout: true).trim()

Upvotes: 9

jgritty
jgritty

Reputation: 11915

There's no easy way I know of to store the "variable" in jenkins. Your best bet is to use some other place to store this MAC address. A file would be a good place, but it probably needs to be on a shared fileserver somewhere.

Upvotes: 0

Related Questions