I am trying to fetch the branch the current git head is in as:
String branchName = gitBranchName().toLowerCase()
String gitBranchName() {
def branch = System.env.GIT_BRANCH
if (!branch){
// For local build
def proc = "git rev-parse --abbrev-ref HEAD".execute()
proc.in.eachLine { line -> branch = line }
proc.err.eachLine { line -> println line }
proc.waitFor()
}
return branch
}
on running this it always returns null and hence code breaks when executing gitBranchName().toLowerCase()
as toLowerCase() throws error on running for null.
* What went wrong:
Execution failed for task ':init_env'.
> Cannot invoke method toLowerCase() on null object
I guess it is sending the value of branch returned from System.env.GIT_BRANCH which is not set on local machine and it is not waiting for the if block to complete.
Any idea what is wrong and why?