git-revision-webpack-plugin icon indicating copy to clipboard operation
git-revision-webpack-plugin copied to clipboard

Use git log instead git rev-parse to get current branch

Open folmert opened this issue 5 years ago • 5 comments

git rev-parse --abbrev-ref HEAD isn't very helpful, as it always returns HEAD when you're in detached HEAD state (i.e. when deploying via gitlab).

I suggest using git log -n 1 --pretty=%d HEAD instead, in detached HEAD state it produces a more meaningful output: (HEAD, origin/sandbox/test_git_revision_plugin)

folmert avatar Jan 17 '19 16:01 folmert

I agree, when deploying via travis it's useless - it always returns HEAD.

jpogorzelski avatar May 18 '19 13:05 jpogorzelski

I agree, when deploying via gitlab it's useless - it always returns HEAD.

luckyyyyy avatar Jun 14 '19 23:06 luckyyyyy

When building on CI servers, the value should come from specific environment variables (for more info see https://github.com/n0mer/gradle-git-properties/blob/master/src/main/groovy/com/gorylenko/properties/BranchProperty.groovy)

  • Jenkins/Hudson: GIT_LOCAL_BRANCH / GIT_BRANCH / BRANCH_NAME
  • TravisCI: TRAVIS_BRANCH
  • TeamCity: teamcity.build.branch
  • Gitlab: CI_COMMIT_REF_NAME
  • Bamboo: BAMBOO_PLANREPOSITORY_BRANCH

ttha1 avatar Feb 25 '20 22:02 ttha1

I use this configuration:

new GitRevisionPlugin({
  branch: true,
  versionCommand: "describe --tags --always --dirty | sed -En 's/-dirty/~/p'",
  branchCommand:
    "describe --tags --exact-match 2> /dev/null || git symbolic-ref -q --short HEAD || git rev-parse --short HEAD",
});

Which gives nicely formatted version strings no matter if you're currently on a tag, a branch, or worst-case a detached head, in which case it falls back to the short git hash.

jameswilson avatar Mar 23 '21 06:03 jameswilson

  • GIT_BRANCH

For Jenkins, the env var, "BRANCH_NAME", is only available for a multibranch project. And there are no "GIT_LOCAL_BRANCH" and "GIT_BRANCH" in Jenkins. I'm using Jenkins 2.303.2

yh36 avatar Oct 25 '21 05:10 yh36