octopathTraveller
octopathTraveller

Reputation: 105

Python list as environment variable in Jenkins

I am trying to set an environment variable in Jenkins that I want my python script in Github to use. I can access variables once they're not a collection but i would like to set a collection variable. In this case, a list.

I have a list variable like: list = ["item1","item2"]

When i try to print it from Jenkins using: print(os.environ['list']) it prints the whole list as a single string and using print(type(os.environ['list'])) prints "string".

Upvotes: 0

Views: 2928

Answers (2)

octopathTraveller
octopathTraveller

Reputation: 105

Create a string variable in jenkins and then use str.split() in the python script to make a list.

In Jenkins: jenkinslist = item1 item2 item3

In Github/python: pythonlist = os.environ['jenkinslist'].split()

Upvotes: -1

user2201041
user2201041

Reputation:

Environment variables are just text. In cases (e.g. the PATH and PYTHONPATH variables), this can be done by using os.pathsep to separate elements of the list. E.g.

os.environ['list'] = os.pathsep.join(["item1", "item2"])

and then reconstruct

print(os.environ['list'].split(os.pathsep))

You could also use some serialization standard like JSON.

I don't believe it would work to store the list itself.

Upvotes: 2

Related Questions