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,462,549
Community Members
 
Community Events
176
Community Groups

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

Atlassian Community Events