3

I'm trying to fill some parameters using Command Line build step.

Here is the code:

#!/bin/bash -x
VERSIONCODE=123 
VERSIONNAME=1.2.0
echo "##teamcity[setParameter name='env.VERSION_NAME' value='$VERSIONNAME']" 
echo "##teamcity[setParameter name='env.VERSION_CODE' value='$VERSIONCODE']"

Build Log:

[09:14:06][Step 1/8] + VERSIONCODE=123 
[09:14:06][Step 1/8] + VERSIONNAME=1.2.0 
[09:14:06][Step 1/8] + echo ' 
[09:14:06][Step 1/8] ##teamcity[setParameter name='\''env.VERSION_NAME'\'' value='\''1.2.0'\''] 
[09:14:06] 
[Step 1/8] Incorrect property name. 
Valid property list format is (name( )*=( )*'escaped_value'( )*)* where escape symbol is "|" 
[09:14:06][Step 1/8] ' 
[09:14:06][Step 1/8] + echo ' 
[09:14:06][Step 1/8] ##teamcity[setParameter name='\''env.VERSION_CODE'\'' value='\''123'\''] 
[09:14:06] 
[Step 1/8] Incorrect property name. 
Valid property list format is (name( )*=( )*'escaped_value'( )*)* where escape symbol is "|" 
[09:14:06][Step 1/8] '

I've been trying to google it for several hours and got nothing. what am I doing wrong?

ttmask
  • 81
  • 1
  • 6

2 Answers2

6

The -x bash option is pretty useful and the solution need not involve removing that option. Instead, let's see why the problem occurs in the first place in custom scripts.

With bash's -x option, bash will echo the line it's about to execute, including literal escape chars. TeamCity will try to parse this output (which we don't want at this point as the command line echo isn't meant to be parsed). If TC sees the echoed command line as malformed (e.g., because of the escape chars), it will emit an Incorrect property name. warning:

echo "##teamcity[buildStatisticValue key=\'warnings\' value=\'42\']"

However, TC will see the executed output of that command as fine.

If TC parses an echoed command line correctly as a TC message, it's even worse because it will then parse it again when that line executes, potentially leading to double-counting and other subtle errors.

Instead, if you must have ## in your custom script and -x is enabled, just be sure to escape the two hashes like so:

echo \#\#teamcity[buildStatisticValue key=\'warnings\' value=\'42\']

Bash will print the exact command line with the escaped hashes and TC won't try to parse it (and either fail or double-count), but when the line is executed it will echo as ##teamcity[buildStatisticValue key='warnings' value='42' which TC will parse as intended.

MartyMacGyver
  • 9,483
  • 11
  • 47
  • 67
5

I've asked it on teamcity-support.jetbrains.com and got an answer:

The issue is with the "-x" from the script definition. It seems to go to extra lengths for tracing the script, which ends in the script failing. Removing the -x will make the script work properly. You can verify that the parameters are set at the end of the build by checking on the build result page, parameters tab.

It works.

ttmask
  • 81
  • 1
  • 6