1

I want to have the current Git branch in the Version name of my Android app. I built in this snippet from here into my build.gradle:

def getWorkingBranch() {
    // Triple double-quotes for the breaklines
    def workingBranch = """git --git-dir=${rootDir}/git
                               --work-tree=${rootDir}/..
                               rev-parse --abbrev-ref HEAD""".execute().text.trim()
    print "Working branch: " + workingBranch
    return workingBranch
}

The snippet is slightly altered as suggested in the first comment on the article.

It works fine when I build my app locally, but doesn't when the app is built with Jenkins. I also tried the original variant of the snippet, in both cases the workingBranch String is empty.

Jenkins seems to be 'aware' of the branch it builds, it lists the branches under

Git Build Data / Built Branches

, so it should be possible

fweigl
  • 21,278
  • 20
  • 114
  • 205

1 Answers1

4

If this is only for testers/downstream, what about using Jenkins' Git environment variables instead, e.g. $GIT_BRANCH?

Locally, it will show no version/empty when you build, but when you build on Jenkins it will show the correct branch name in your 'version' string.

To get around the local builds producing an empty string, you could use your above snippet to set the branch into GIT_BRANCH. Not perfect, but it should work.

acanby
  • 2,936
  • 24
  • 26
  • Thanks, could I reference this variable from my build.gradle file? If yes, how? – fweigl Jun 11 '15 at 11:24
  • Sorry, my gradle foo isn't up to scratch, but I think https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_properties_and_system_properties should help. It would be essentially the same as passing a property to maven ala `mvn clean install -DGIT_BRANCH=FOO` – acanby Jun 11 '15 at 11:29
  • This might also be useful/necessary: http://stackoverflow.com/questions/23689054/problems-passing-system-properties-and-parameters-when-running-java-class-via-gr – acanby Jun 11 '15 at 11:33