set parameter with python script.

Hi. I have a bash script where in the last line i run this command: 

echo "##teamcity[setParameter name='env.projectVersion' value='${DOCKER_TAG}']"

How can I achive this with python. I have tried this one > 
print("##teamcity[setParameter name='env.projectVersion' value="${DOCKER_TAG}"]")

But it does not work as expected. 
0
5 comments

It could be that you have a mix of " and ' in your print command. It seems to work for me using:

print("##teamcity[setParameter name='env.projectVersion' value='${DOCKER_TAG}']")

 

0

Sorry it was typo. 

print("##teamcity[setParameter name='env.projectVersion' value='${DOCKER_TAG}']")

When I run this it should assign value of the ${DOCKER_TAG} to "projectVersion"  environment varaiable. But instead in the next step it is return ${DOCKER_TAG} as a string. 
So in the next step I am building image as this: 

docker build . -t %docker.image.name%:$projectVersion    
The command should return me image like this:   api:1.0 instead it returns  api:${DOCKER_TAG} and that is the reason my pipeline is failing. Let me share whole script. 

import os
import json

branch = "%teamcity.build.branch%"
with open('package.json', 'r') as f:
data = json.load(f)

if branch == "integration":
DOCKER_TAG="latest"
elif branch == 'release':
DOCKER_TAG=data["version"]

print("##teamcity[setParameter name='env.projectVersion' value='${DOCKER_TAG}']")

So I basically I want to assign DOCKER_TAG to env.projectVersion because of I need that variable in the next step. 

0

Hi Dodbeg,

Once you set the parameters, in the next step you would access them in the same way as you would any other normal parameter. On your docker step you are using $projectVersion, which, I assume, is the one you want to point to the parameter set in the previous step. If that is the case, you would need to get the value of that parameter using %env.projectVersion% instead of $projectVersion.

0

Thanks for sharing your entire script, I see what you mean. I think the issue has to do with the way Python is interpreting the print statement because it results in the same thing if I just run it in Python outside of TeamCity. Try using this print statement and let me know if it would work for you:

print("##teamcity[setParameter name='env.projectVersion' value='" + DOCKER_TAG + "']")

Additionally, as Olga has pointed out, I think you'll need to access the parameter using the TeamCity parameter syntax:

docker build . -t %docker.image.name%:%env.projectVersion%
0

Eric Borchardt
Wonderful, thank you ! 

print("##teamcity[setParameter name='env.projectVersion' value='" + DOCKER_TAG + "']")

It worked as expected. 

Regards

0

Please sign in to leave a comment.