How to use Bamboo plan variables in an inline script task?
Asked Answered
A

2

9

When defining a Bamboo plan variable, the page has this.

For task configuration fields, use the syntax ${bamboo.myvariablename}. For inline scripts, variables are exposed as shell environment variables which can be accessed using the syntax $BAMBOO_MY_VARIABLE_NAME (Linux/Mac OS X) or %BAMBOO_MY_VARIABLE_NAME% (Windows).

However, that doesn't work in my Linux inline script. For example, I have the following defined a a plan variable

name: my_plan_var    value: some_string

My inline script is simply...

PLAN_VAR=$BAMBOO_MY_PLAN_VAR
echo "Plan var: $PLAN_VAR"

and I just get a blank string.

I've tried this

PLAN_VAR=${bamboo.my_plan_var}

But I get

${bamboo.my_plan_var}: bad substitution

on the log viewer window.

Any pointers?

Aaron answered 22/5, 2017 at 20:53 Comment(4)
did you tried using lowercase? like $bamboo_MY_PLAN_VARFiji
@BorysKupar, case doesn't matter, thanks.Aaron
This works for me like a charm, maybe you can post a picture on how the variables are declared at the plan level?Shire
I was having a similar issue with a Windows inline script where incorrect casing of 'bamboo' in the variable name was causing the problem. When I used ${Bamboo.MyVariable} it didn't work, however changing it to ${bamboo.MyVariable} worked as expected.Camion
E
7

I tried the following and it works:

On the plan, I set my_plan_var to "it works" (w/o quotes)

In the inline script (don't forget the first line):

#/bin/sh

PLAN_VAR=$bamboo_my_plan_var
echo "testing: $PLAN_VAR"

And I got the expected result:

testing: it works

Epexegesis answered 4/6, 2017 at 3:6 Comment(1)
have you tried using that PLAN_VAR into another task? may i know how did you do ?Amaya
T
0

I also wanted to create a Bamboo variable and the only thing I've found to share it between scripts is with inject-variables like following:

  1. Add to your bamboo-spec.yaml the following after your script that will create the variable:
Build:
  tasks:
    - script: create-bamboo-var.sh
    - inject-variables:
        file: bamboo-specs/vars.yaml
        scope: RESULT
        # namespace: plan
    - script: echo ${bamboo.inject.GIT_VERSION} # just for testing

Note: Namespace defaults to inject.

  1. In create-bamboo-var.sh create the file bamboo-specs/vars.yaml:
#!bin/bash
versionStr=$(git describe --tags --always --dirty --abbrev=4)
echo "GIT_VERSION: ${versionStr}" > ./bamboo-specs/vars.yaml

Or for multiple lines you can use:

SW_NUMBER_DIGITS=${1}    # Passed as first parameter to build script
cat <<EOT > ./bamboo-specs/vars.yaml
GIT_VERSION: ${versionStr}
SW_NUMBER_APP: ${SW_NUMBER_DIGITS}
EOT

Scope can be local or result. Local means it's only available for current job and result means it can be used in subsequent stages of this plan and releases that are created from the result.

Namespace is just used to avoid naming collisions with other variables.

With the above you can use that variable in later scripts with ${bamboo.inject.GIT_VERSION}. The last script task is just to see that it is working in other scripts. You can also see the variables in the web app as build meta data.

I'm using the above script before the build (in my case compiling C-Code) takes place so I can also create a version.h file that can be used by the source code.

This is still a bit cumbersome but I'm happy with it and I hope it will help others to configure Bamboo. Bamboo documentation could be better. (Still a lot try and error)

Theocracy answered 7/2, 2020 at 12:59 Comment(1)
Note that the revision of the repositories attached to your plan are directly available from Bamboo with variable names bamboo.planRepository.1.revision, see confluence.atlassian.com/bamboo/bamboo-variables-289277087.htmlShire

© 2022 - 2024 — McMap. All rights reserved.