Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Is it possible to obtain GIT branch name at Bamboo Spec run time?

I already tried by using jgit:

import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.Ref;

import java.io.File;
import java.io.IOException;

Repository repository = FileRepositoryBuilder.create(new File("../.git"));
currentBranch = repository.getBranch();
System.out.println("Current branch: " + currentBranch);

 

This unfortunately throws an access denied error.

[ERROR] Failed to execute goal com.atlassian.bamboo:bamboo-specs-runner:6.9.2:run (default-cli) on project bamboo-specs-generator: Execution default-cli of goal com.atlassian.bamboo:bamboo-specs-runner:6.9.2:run failed: access denied ("java.lang.RuntimePermission" "getenv.GIT_OBJECT_DIRECTORY") -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.atlassian.bamboo:bamboo-specs-runner:6.9.2:run (default-cli) on project bamboo-specs-generator: Execution default-cli of goal com.atlassian.bamboo:bamboo-specs-runner:6.9.2:run failed: access denied ("java.lang.RuntimePermission" "getenv.GIT_OBJECT_DIRECTORY")

 

Or maybe someone who knows how to solve the access denied exception?

1 answer

Did you figure this out?  We are running into a similar problem reading environment variables in Java Specs file.  

In the path where the bamboo specs is executed contains the path name (a bit encoded, e.g. / is encoded as _2F).

So an ugly solution would be to use the path to obtain the branch name (note: no decoding done in this example). Also note that this can break at any bamboo release, since it is not something that is specified.

public static final String REPOSITORY_PATH_PREFIX = "repository-";

public static String GetBranch()
{
String path = Paths.get(".").toAbsolutePath().normalize().toString();
for (String element : path.split("/"))
{
if (element.startsWith(REPOSITORY_PATH_PREFIX))
{
return element.split("-", 3)[2].trim();
}
}

return "unknown";
}

That was a good hint.  I have it sorta working.  It cleanly identifies if I am in develop branch, but I am stumbling on the master branch.  Its probably in my setup of my test environment, but for anyone else looking at this, this looks very promising.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bamboo

Bamboo Data Center on Kubernetes

Hi, If you are running self-managed environments and looking to adopt modern infrastructure, Bamboo Data Center can now be deployed in a Kubernetes cluster. By leveraging Kubernetes, you can easily...

66 views 0 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you