I need current branch name of the project where I'm deploying a plugin, So code for current branch name in the plugin's groovy class goes like this,
def getCurrentGitBranch() {
def gitBranch = "Unknown branch"
try {
def workingDir = new File(ProjectConfig.projectDir)
def result = 'git rev-parse --abbrev-ref HEAD'.execute(null, workingDir)
result.waitFor()
if (result.exitValue() == 0) {
gitBranch = result.text.trim()
}
} catch (Exception e) {
throw new Exception("Problem occurred while fetching current branch name from git.")
}
log.info("Branch Name [ "+ gitBranch + "]")
return gitBranch
}
But for some reason when while deploying the project on cloud using jenkins this function return HEAD instead of current branch of the project for which I'm trying to deploy.
I tried a lot with different scenarios but no luck, how do I make sure the plugin I'm using fetches the current branch of the project for which deployment is being done.