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

Jorie July 9, 2019

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

0 votes
james_mckean January 13, 2022

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

Peter Broere January 14, 2022

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";
}
james_mckean January 18, 2022

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
AUG Leaders

Atlassian Community Events