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

How to access build variables from a bamboo task

Deleted user November 28, 2013

This is a very similar question to one that has been asked before on here, but thought I would repeat it since it was never answered properly.

What I am trying to do is get some bamboo variable values to get the path to the Git executable to perform some commits, tags, etc. in the plugin.

Using the following lets me get any custom variables that are defined in the build plan:

taskContext.getBuildContext().getVariableContext().getDefinitions()

However, it doesn't provide any of the bamboo system variables.

As with the previous question asked about this, I have tried all of the other potential candidates (runtime context, common context, etc.) with no luck.

The answer suggested by Atlassian before was getCustomDataMap, but as pointed out previously, this doesn't exist in the API.

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Deleted user December 4, 2013

After a lot of trial and error I have managed to answer my own question. It appears that this is not documented anywhere in the plugin development guide. The CustomVariableContext and CapabilityContext classes are injected by bamboo when your task is initialised.

Accessing Build Variables

Build variables are accessed from within a task using the CustomVariableContext bean. Add the following code to your plugin to access them:

// Define a custom variable context property at the class level
private CustomVariableContext customVariableContext;

public CustomVariableContext getCustomVariableContext() {
    return customVariableContext;
}

public void setCustomVariableContext(CustomVariableContext customVariableContext) {
    this.customVariableContext = customVariableContext;
}

// Within your task, variables can be accessed like this:
Map<String, String> customVariables = this.customVariableContext.getVariables(taskContext.getCommonContext());

Accessing Capabilities

Accessing build capabilities (e.g. Git, Maven, MSBuild, etc.) can be done in a similar way using the following code:

// Define a capability context at the class level
private CapabilityContext capabilityContext;

public CapabilityContext getCapabilityContext() {
    return capabilityContext;
}

public void setCapabilityContext(CapabilityContext capabilityContext) {
    this.capabilityContext = capabilityContext;
}

// Use the capabilities like this in your Task
String mavenExecutable = this.capabilityContext.getCapabilityValue("maven");

Erb Cooper October 21, 2014

The good thing about this approach is that it works the same for builds and deployments. The problem with {code}taskContext.getBuildContext().getVariableContext(){code} is that it works only for builds.

1 vote
Rasike Abeyratne May 23, 2018
VariableContext variables = taskContext.getBuildContext().getVariableContext();
VariableDefinitionContext variable = variables.getEffectiveVariables().get("release_new_tag");
String newTag = variable.getValue();

Through above was able to access the bamboo custom variable "release_new_tag" - define for the specific plan

For access bamboo variables should use methods provided by sdk. e.g. taskContext.getBuildContext().getBuildNumber();
1 vote
Cintia Calvo July 3, 2014

I was able to access all the job variables using

taskContext.getBuildContext().getVariableContext(). variableContext.getDefinitions()

The definition is particularly useful, you can find the type (if it's a PLAN, MANUAL) and so on.

TAGS
AUG Leaders

Atlassian Community Events