How to check if a Variable is set?

WET September 26, 2019

So I have 2 variables in my script, one that is set when the user triggers the deployment manually and second when the deployment is triggered automatically. Therefore, one of the variables will not be set.

I've tried:

if [[ -z "${bamboo.ManualBuildTriggerReason.userName}" ]]; then
     DEPLOY_USER="${bamboo.DependencyTriggerReason.triggeringBuildResultKey}"
else
     DEPLOY_USER="${bamboo.ManualBuildTriggerReason.userName}"
fi

but I get a bad substitution error. In the case because the bamboo.ManualBuildTriggerReason.userName doesn't exists when the job deploys automatically.

 

Then I've tried something like

DEPLOY_USER=${bamboo.DependencyTriggerReason.triggeringBuildResultKey:-${bamboo.ManualBuildTriggerReason.userName}}

Still got the same error.

What is the best way to check if the variable is set or not?

 

LE: It's related to https://community.atlassian.com/t5/Bamboo-questions/Check-if-variable-exists/qaq-p/246606

5 answers

1 vote
Mike Burnham
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 26, 2022

I found another way after reading this question, so adding it here. Might not have worked when first posted.

You can refer to Bamboo variables using an _ instead of the usual . style, and they exist as traditional shell variables that way. So if I want bamboo.badvariable, I can do this:

echo var=${bamboo_badvariable}
Phill Pafford
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 25, 2023

thanks, this helped me solve my issue

1 vote
Andrecarlossilva October 4, 2019

I made a workaround here to check:

 

PARAMETER_NO_TRAIL_SPACE="$(echo -e "${bamboo.ManualBuildTriggerReason.userName}| sed -e 's/[[:space:]]*$//')"

if [[ -z "$PARAMETER_NO_TRAIL_SPACE" ]]; then    

1 vote
Alexander
Contributor
September 26, 2019

An indicator of a referenced variable not being set is if your build fails:

You can:

  • reference a global or context specific variable in a build plan or deployment project
  • reference a variable which references another one, deep recursion is allowed

There are couple of limitations:

  • referencing a variable which isn't defined is an error, whole build or deployment will fail if you reference such variable
  • cycles are not allowed and are considered as build or deployment project error.

 If you want to check if a variable is set to the value you'd expect, I'd just create a script task that printed a message with the variable and look for it in the logs while debugging.

echo "Hello ${bamboo.ManualBuildTriggerReason.userName}!"

 

WET September 27, 2019

This doesn't help me, let's say I have some variables in my gradle.properties and they are loaded into bamboo and they are stage specific. 

I would want to check if the variable is defined like a simple bash command: 

if [[ -v blabla ]]; 

this checks that $blabla variable exists.

Is that so complex to do in Bamboo?  

0 votes
Gerd Aschbrenner
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 31, 2020

Only ASCII letters and digits and "_" are supported well on all linux systems and shell scripts as variable names. (regexp:   [a-zA-Z_][a-zA-Z_0-9]* )

See the posix definition of a name for example: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_235


So the workaround is to assign the value to a variable name without dots by using a tool that supports the dots in the variable name like:

bamboo_ManualBuildTriggerReason_userName=$(env | grep ^bamboo\\.ManualBuildTriggerReason\\.userName= | cut -d= -f2-)

 

0 votes
Andrecarlossilva October 4, 2019

I am having this same problem.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events