Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,458,720
Community Members
 
Community Events
176
Community Groups

How to check if a Variable is set?

Edited

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

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    

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}!"

 

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?  

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}

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-)

 

I am having this same problem.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events