天天看點

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分支的目前分支名?一、背景二、解決方法參考連結