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
8
  • Let me see if I got it rigjt: bamboo replace your bash script variables before runing the script,? If yes try to escape the dollar signe the braces are there to help parsing the Var name and won't be interpreted after an escaped $, for the note $VAR is the same as ${VAR}, the brace are useful when concatenating to precise which part is the var
    – Tensibai
    Feb 1 '18 at 23:27
  • Previous comment is badly written but I hope you get the idea, it's too late to try to correct on phone :)
    – Tensibai
    Feb 1 '18 at 23:36
  • 1
    May you try echo "\${VAR}"?
    – Tensibai
    Feb 2 '18 at 9:48
  • Maybe of use: stackoverflow.com/a/45918619/3627607
    – Tensibai
    Feb 2 '18 at 9:56
  • 1
    Ok so I think that's a simple bash variable replacement problem
    – Tensibai
    Feb 2 '18 at 10:21
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.

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.

5
  • Do you mean my answer? I guess what I was trying to convey was that his assumption that bamboo replaces undeclared variables with empty values is wrong.
    – Preston Martin
    Feb 1 '18 at 23:27
  • My bad, I did miss the first case, it would have been better without the bamboo prefix to showcase the same usage as question state, first step being écho ${VAR}
    – Tensibai
    Feb 1 '18 at 23:32
  • (I wouldn't be surprised bamboo parser only takes special behavior on bamboo. Prefixed variables)
    – Tensibai
    Feb 1 '18 at 23:35
  • Yep that's exactly what it does. Even if you prefix it with Bamboo, if that variable doesn't exist, it won't replace it with anything.
    – Preston Martin
    Feb 1 '18 at 23:37
  • thanks - please share which version of Bamboo do you have? see update above - Bamboo 5.13.1 and 6.3.0 behaves not the way you suggest. Maybe there is some setting for parsing and substituting variables? Feb 2 '18 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.