2

In Atlassian Bamboo, in script tasks you can use special Bamboo environment variables e.g. for current build number

${bamboo.buildNumber}

Now I have the impression if I have a code piece like ${VAR} which I want to pass to other context, Bamboo's templating logic will kick in and render this part as an empty string.

Proof:

echo "my foo ${VAR}!"

Output:

01-Feb-2018 20:47:58    my foo !

Is there a way to escape curly braces in this context?

What I have tried:

  • googling it: nope
  • ${{VAR}} - bad substution error
  • $\{VAR\} leads to exactly same output as it escaped which I do not want to have either.

UPDATE from comments - making sure there is no existing variable being just empty.

Input:

echo "my foo random ${RANDOM_VAR_ABCFOO}!"
echo "my foo random with prefix ${bamboo.RANDOM_VAR_ABCFOO}!"

Output:

Bamboo 5.13.1

02-Feb-2018 10:26:11    my foo random !
02-Feb-2018 10:26:11    /home/bambooagent/temp/FOO-JOB1-4-ScriptBuildTask-3130782940072218698.sh: 2: /home/bambooagent/temp/FOO-JOB1-4-ScriptBuildTask-3130782940072218698.sh: Bad substitution

Bamboo 6.3.0

02-Feb-2018 10:22:02    my foo random !
02-Feb-2018 10:22:02    /data/bamboo-agent/temp/FOO-JOB1-24-ScriptBuildTask-7913400670965638044.sh: line 2: my foo random with prefix ${bamboo.RANDOM_VAR_ABCFOO}!: bad substitution
Ta Mu
  • 6,792
  • 5
  • 43
  • 83

2 Answers2

1

I don't know the inner of bamboo, but I assume it works like a bash script.

What happens is that variables are replaced before execution, in bash that would be echo "echo $VAR" > test.sh and test.sh will only contain "echo" because VAR is replaced by it's value before the command is executed.
If you want test.sh to contain echo $VAR you have to tell bash to ignore the replacement on the first call by escaping the $ sign:

echo "echo \$var" > test.sh will give echo $VAR in the file.

In the same note, the notation ${VAR} and $VAR are the same, using braces is a good practice when you do concatenation like in echo "Size is ${VAR}Kb" as without the braces like this $VARKb bash would try to find a variable named VARKb and return an empty value.

To address PrestonM different behavior, I assume there's either - the fact running under windows with the powershell interpreter doesn't behave the same - or just that bamboo has a special case for variables prefixed with bamboo. and as such escape them.

If someone want to test you may try:

echo "${VAR}"
echo "${bamboo.VAR}"
echo "\${VAR}"

and edit the results in this answer.

Tensibai
  • 11,416
  • 2
  • 37
  • 63
0

If your variable is not declared in Bamboo (either by injections, build/deploy variables etc.) it will not inject the variable and will use the string as is. It should only render the variable as an empty string if that is the value you set for that particular variable. As an example, my build plan has the following variables:

Variable Name    Value
version.major    1
version.minor    12

In my build plan, I have the following script job:

echo "${bamboo.substituteVar}"
echo "${bamboo.version.major}"
echo "${bamboo.version.minor}"

In my logs, the output will be:

C:\Users\TestUser\bamboo-home\xml-data\build-dir\12345678\MANG-TC-TBV>echo "${bamboo.substituteVar}"

"${bamboo.substituteVar}"

C:\Users\TestUser\bamboo-home\xml-data\build-dir\12345678\MANG-TC-TBV>echo "1"

"1"

C:\Users\TestUser\bamboo-home\xml-data\build-dir\12345678\MANG-TC-TBV>echo "12"

"12"

As long as your variables and namespaces are unique and descriptive for the plan you are working on, you should not have any issues with name conflicts. If it is replacing it with an empty string, my guess is that you may have that variable created somewhere in Bamboo, but no value assigned to it.

Preston Martin
  • 3,288
  • 4
  • 18
  • 39