Saving Git SHA1 when building with Hudson similar

2019-04-02 14:00发布

We have migrated from CVS to Git. Our Hudson build script used to save the value of the CVS_BRANCH environment variable in the generated build along with the Hudson BUILD_ID to allow for later investigation.

I cannot figure out how Hudson (or the Git plugin) presents the Git SHA1 name of the current commit to the ant script, as I cannot locate any definite source saying where I should look.

I'd prefer not to invoke git to get it if it is present in the environment, but can do if that is necessary.

What have I missed?

4条回答
在下西门庆
2楼-- · 2019-04-02 14:38

The git plugin exposes a GIT_COMMIT environment variable.

查看更多
Evening l夕情丶
3楼-- · 2019-04-02 14:46

It turned out that jGit supports the "rev-parse HEAD" command, which in combination with the <java jar="jgit...jar" fork="true" args="rev-parse HEAD" outputproperty="git.SHA1" /> did exactly what I wanted, namely got the SHA1 into an ant property so I could use it later.

查看更多
Luminary・发光体
4楼-- · 2019-04-02 14:59

Well, if you really want to avoid calling git command (git describe or git rev-parse), then you can do the following:

  1. Look up $GIT_DIR/HEAD file. If it is symbolic link, its target is fully qualified name of current branch (e.g. 'refs/heads/master' if current branch is 'master'); shouldn't happen except in very old repositories managed by very old git.

    If it is ordinary file, it is either of the form ref: refs/heads/<branch> (so called symref), or it contains SHA-1 id of current commit (so called "detached HEAD" aka. anonymous branch: '(no branch)' in git branch output.

  2. Current commit is either in $GIT_DIR/refs/head/branch file, or it can be found in the $GIT_DIR/packed-refs file. If both exist, then loose ref (in a seperate file named after fully qualified branch name) wins.

But I am not sure if it is worth it.

查看更多
祖国的老花朵
5楼-- · 2019-04-02 14:59

To get buildnumber from git using Ant and JGit you can use jgit-buildnumber-ant-task. It can give you tag name, branch name, commit sha1, and commit's count in current branch as Ant variables.

查看更多
登录 后发表回答