天天看点

Git学习-->关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

因为代码都迁移到了Gitlab,所以Jenkins编译的时候我们都需要将之前的SVN信息换成现在的Git信息。最近编译一个Lib库的时候,因为团队规定上传Release版本的AAR到Maven的话,必须需要在Jenkins上编译而且Git Branch 必须是master分支才能够上传到Maven。

因此我们就需要在Gradle脚本中,获取Git Branch ,Git Commit等相关信息。但是在获取Git Branch的时候出现了问题,在本地Android Studio编译的时候能够获取到Git Branch的名字,但是使用Jenkins编译的时候,一直获取不到信息。

下面是我写的一份gradle文件,用于获取Git和Jenkins的相关信息

其中的方法,getGitBranch方法在Android Studio编译的时候,能够正常获取到Git分支名。

我在进行编译的时候,是会通过如上代码打印出Git Branch的信息。

在Android Studio 本地编译的时候,是可以打印出相关的信息的。

Git学习-->关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

但是在Jenkins编译的时候,是不能够上传的,如下所示:

Git学习-->关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

后来我尝试找了很多种方法去获取Git Branch的名字,在Android Studio本地都可以获取到,如下所示:

方法1、<code>git symbolic-ref --short -q HEAD</code>

Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

方法2、<code>git rev-parse --abbrev-ref HEAD</code>

Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

方法3、<code>git branch | grep \* | cut -d ' ' -f2</code>

Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

方法4、<code>git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"</code>

Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接
以上所有的方法,仅仅在Android Studio的终端或者本地gradle代码中有效,然而在Jenkins服务器编译的时候都是获取为空。
Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

如上所示,在上面的链接中有介绍,有几个Environment variables环境变量可以使用。

Environment variables

The git plugin sets several environment variables you can use in your scripts:

GIT_COMMIT - SHA of the current

GIT_BRANCH - Name of the remote repository (defaults to origin), followed by name of the branch currently being used, e.g. “origin/master” or “origin/foo”

GIT_LOCAL_BRANCH - Name of the branch on Jenkins. When the “checkout to specific local branch” behavior is configured, the variable is published. If the behavior is configured as null or **, the property will contain the resulting local branch name sans the remote name.

GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)

GIT_PREVIOUS_SUCCESSFUL_COMMIT - SHA of the previous successfully built commit from the same branch.

GIT_URL - Repository remote URL

GIT_URL_N - Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2

GIT_AUTHOR_NAME and GIT_COMMITTER_NAME - The name entered if the “Custom user name/e-mail address” behaviour is enabled; falls back to the value entered in the Jenkins system config under “Global Config user.name Value” (if any)

GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL - The email entered if the “Custom user name/e-mail address” behaviour is enabled; falls back to the value entered in the Jenkins system config under “Global Config user.email Value” (if any)

然后我将这几个变量,在一个app的Jenkins任务中,编译完成后的邮件内容中添加了这几个变量的内容,如下所示:

Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

在构建后的操作中,Editable Email Notification的邮件通知中,将邮件内容改为如下所示的代码。

这样编译完后,收到的邮件内容如下:

Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

如上所示,收到的邮件内容包含了Git的相关信息:

其中,GIT_BRANCH这个环境变量的值为origin/feature/UseByAnonymousDBMigrateAndApiChange,代表Jenkins上/UseByAnonymousDBMigrateAndApiChange分支远程Gitlab上该分支映射的远程分支。因此我们可以对GIT_BRANCH这个环境变量做做文章。

将之前gradle脚本中的getGitBranch方法,做如下修改,区分编译环境是Jenkins还是本地。环境不同,运行不同的脚本获取Git Branch的名字。当处于Jenkins环境的时候,先通过GIT_BRANCH这个环境变量获取到Jenkins拉下来的分支对应的远程分支,然后通过字符串分离,获取到分支名。

完整代码如下所示:

现在测试下Jenkins编译是否正常,可以看到一切都正常了。

Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接

<a href="https://wiki.jenkins.io/display/JENKINS/Git+Plugin">https://wiki.jenkins.io/display/JENKINS/Git+Plugin</a>

<a href="https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git">https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git</a>

Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉! 如果觉得本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行随意打赏。您的支持将鼓励我继续创作!
Git学习--&amp;gt;关于Jenkins编译时候,如何获取Git分支的当前分支名?一、背景二、解决方法参考链接