set parameter with python script

Answered

I have a python script which finds an account alias, which then gets stored as variable decomaccountalias

I have a env.variable in teamcity called env.DECOM_ACCOUNT_ALIAS, and i want to set the variable in my python script (decomaccountalias) to that to utilise in the next build step

My code for doing that

print(f"Setting env.DECOM_ACCOUNT_ALIAS={decomaccountalias}")

print(f"##teamcity[setParameter name='env.DECOM_ACCOUNT_ALIAS' value='{decomaccountalias}']")

the build repsonds with 

##teamcity[setParameter name='env.DECOM_ACCOUNT_ALIAS' value='['test-account']

error while parsing TeamCity service message: property value not found (no "=" sign). Valid service message has a form of "##teamcity{messageName name1='escaped_value' etc etc

i did try and escape the values but never worked either

print(f"##teamcity|[setParameter name=|'env.DECOM_ACCOUNT_ALIAS|' value=|'{decomaccountalias}|'|]")

Any help would be greatly appreciated 

Thank you

0
5 comments

Update - When i added in the escaped characers the error message did dissapear 

However it's still not setting the env.DECOM_ACCOUNT_ALIAS

build returned

##teamcity|[setParameter name=|'env.DECOM_ACCOUNT_ALIAS|' value=|'['test-account']'|]

0

Hi,

What version of Python are you using? I tried to replicate your issue using Python 3.8.5, but it works as expected. I have a Python build step that runs a simple script of the following:

decomaccountalias = "asdfasdf"
print(f"Setting env.DECOM_ACCOUNT_ALIAS={decomaccountalias}")
print(f"##teamcity[setParameter name='env.DECOM_ACCOUNT_ALIAS' value='{decomaccountalias}']")

I have another Python build step that issues this single command:

print("%env.DECOM_ACCOUNT_ALIAS%")

In the build log, I can see the value of env.DECOM_ACCOUNT_ALIAS has been set to "asdfasdf" in the second step.

[11:51:38] : Step 1/2: Python
...
[11:51:38] :     [Step 1/2] Python run
[11:51:38]i:         [Python run] Process started
...
[11:51:38] :         [Python run] Setting env.DECOM_ACCOUNT_ALIAS=asdfasdf
[11:51:38]i:         [Python run] ##teamcity[setParameter name='env.DECOM_ACCOUNT_ALIAS' value='asdfasdf']
[11:51:38] :     [Step 1/2] Process exited with code 0
[11:51:38] :         [Python run] Process successfully exited with code 0
[11:51:38] : Step 2/2: Python
...
[11:51:38] :     [Step 2/2] Python run
[11:51:38]i:         [Python run] Process started
...
[11:51:39] :         [Python run] asdfasdf
[11:51:39] :     [Step 2/2] Process exited with code 0
0

Hi. It looks like you are escaping square brackets and single quotes in the entire service message. They only need to be escaped in the value parts of the service message. I marked them as XXXX and YYYY in this example:

echo "##teamcity[setParameter name='XXXX' value='YYYY']"

You can try something like this:

print("##teamcity[setParameter name='env.DECOM_ACCOUNT_ALIAS' value='|[|'test-account|'|]']")

https://www.jetbrains.com/help/teamcity/service-messages.html#Escaped+Values

Cheers,
Anatoly

0

Okay, I think I just realized that you're setting decomaccountalias = "['test-account']". If I include the brackets, I am able to reproduce your issue as this seems to be messing with the service message format. I escaped the `, [, and ] characters and it works then:

[12:08:53] : Step 1/2: Python
...
[12:08:53] :     [Step 1/2] Python run
[12:08:53]i:         [Python run] Process started
...
[12:08:53] :         [Python run] Setting env.DECOM_ACCOUNT_ALIAS=|[|'asdfasdf|'|]
[12:08:53]i:         [Python run] ##teamcity[setParameter name='env.DECOM_ACCOUNT_ALIAS' value='|[|'asdfasdf|'|]']
[12:08:53] :     [Step 1/2] Process exited with code 0
[12:08:53] :         [Python run] Process successfully exited with code 0
[12:08:53] : Step 2/2: Python
...
[12:08:53] :     [Step 2/2] Python run
[12:08:53]i:         [Python run] Process started
...
[12:08:53] :         [Python run] ['asdfasdf']
[12:08:53] :     [Step 2/2] Process exited with code 0
[12:08:53] :         [Python run] Process successfully exited with code 0
0

One way to resolve this would be to include a slice of the decomaccountalias variable in the service message and add the ', [ , and ] characters back in like this:

decomaccountalias = "['asdfasdf']"
print(f"Setting env.DECOM_ACCOUNT_ALIAS={decomaccountalias}")
print(f"##teamcity[setParameter name='env.DECOM_ACCOUNT_ALIAS' value='|[|'{decomaccountalias[2:-2]}|'|]']")
0

Please sign in to leave a comment.