Git學習-->關於Jenkins編譯時候,如何獲取Git分支的當前分支名?

1、背景

由於代碼都遷移到了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編譯的時候,一直獲取不到信息。css

下面是我寫的一份gradle文件,用於獲取Git和Jenkins的相關信息java

/** * 獲取Git 分支名 */
def getGitBranch() {
    return 'git symbolic-ref --short -q HEAD'.execute().text.trim()
}

/** * 獲取Git 版本號 */
def getGitSHA() {
    return 'git rev-parse --short HEAD'.execute().text.trim()
}

/** * 獲取Git Tag */
def getGitTag() {
    return 'git describe --tags'.execute([], project.rootDir).text.trim()
}

/** * 獲取Git 提交次數 */
def getGitCommitCount() {
    return 100 + Interger.parse('git rev-list --count HEAD'.execute([], project.rootDir).text.trim())
}

/** * 判斷是否有jenkins */
boolean isInJenkins() {
    Map<String, String> map = System.getenv()
    if (map == null) {
        return false
    }
    String str = map.get("Path")
    if (str != null) {
        //it's windows
        return false
    } else {
        str = ""
        Iterator it = map.iterator()
        while (it.hasNext()) {
            str += it.next()
        }
        return str.contains("jenkins")
    }
}
/** * 獲取jenkins任務名 */
def getJenkinsName() {
    boolean flag = isInJenkins()
    if (flag) {
        ext.env = System.getenv()
        ext.name = env.JOB_URL
        String[] stringArray = ext.name.split("/")
        if (stringArray.length > 0) {
            return stringArray[stringArray.length - 1]
        } else {
            return "Local"
        }
    } else {
        return "Local"
    }
}

/** * 獲取Jenkins Build 號 * @return */
def getJenkinsBuildCode() {
    boolean flag = isInJenkins()
    if (flag) {
        ext.env = System.getenv()
        ext.buildNumber = env.BUILD_NUMBER?.toInteger()
        return "$buildNumber"
    } else {
        return 0
    }
}

/** * 定義幾個變量,在build.gradle裏面引用 */
ext {
    gitTag = getGitTag()
    gitBranch = getGitBranch()
    gitSHA = getGitSHA()
    jenkinsRevision = getJenkinsBuildCode()
    jenkinsName = getJenkinsName()
}

其中的方法,getGitBranch方法在Android Studio編譯的時候,可以正常獲取到Git分支名。android

println "pom_version_type = " + pom_version_type
    println "jenkinsName = " + jenkinsName
    println "gitBranch = " + gitBranch

我在進行編譯的時候,是會經過如上代碼打印出Git Branch的信息。git

在Android Studio 本地編譯的時候,是能夠打印出相關的信息的。windows

這裏寫圖片描述

可是在Jenkins編譯的時候,是不可以上傳的,以下所示:
這裏寫圖片描述ruby

2、解決方法

後來我嘗試找了不少種方法去獲取Git Branch的名字,在Android Studio本地均可以獲取到,以下所示:bash

參考連接:https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git服務器

方法一、git symbolic-ref --short -q HEAD微信

D:\GitLab Source\XTCLint>git symbolic-ref --short -q HEAD
master

D:\GitLab Source\XTCLint>

這裏寫圖片描述

方法二、git rev-parse --abbrev-ref HEADmarkdown

D:\GitLab Source\XTCLint>git rev-parse --abbrev-ref HEAD
master

這裏寫圖片描述

方法三、git branch | grep \* | cut -d ' ' -f2

D:\GitLab Source\XTCLint>git branch | grep \* | cut -d ' ' -f2
master

這裏寫圖片描述

方法四、git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"

D:\GitLab Source\XTCLint>git symbolic-ref HEAD | sed -e "s/^refs\/heads\///"
master

這裏寫圖片描述

以上全部的方法,僅僅在Android Studio的終端或者本地gradle代碼中有效,然而在Jenkins服務器編譯的時候都是獲取爲空。

後來我查看了Jenkins的Git插件上的介紹,參考連接:https://wiki.jenkins.io/display/JENKINS/Git+Plugin

這裏寫圖片描述

如上所示,在上面的連接中有介紹,有幾個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任務中,編譯完成後的郵件內容中添加了這幾個變量的內容,以下所示:

這裏寫圖片描述

在構建後的操做中,Editable Email Notification的郵件通知中,將郵件內容改成以下所示的代碼。

$DEFAULT_CONTENT

<br />
<font color="#0B610B">單元測試</font>
  <li>Launcher單元測試報告&nbsp;:<a href="${BUILD_URL}testReport">點擊查看測試報告</a></li>
  <li>Launcher代碼覆蓋率&nbsp;:<a href="${BUILD_URL}jacoco">點擊查看代碼覆蓋率</a></li>
  <li>Launcher Android Lint&nbsp;:<a href="${BUILD_URL}androidLintResult">點擊查看Android Lint</a></li>


<br />
 <li>GIT_COMMIT&nbsp;:${GIT_COMMIT}</a></li>
 <li>GIT_BRANCH&nbsp;:${GIT_BRANCH}</a></li>
 <li>GIT_LOCAL_BRANCH&nbsp;:${GIT_LOCAL_BRANCH}</a></li>
 <li>GIT_PREVIOUS_COMMIT&nbsp;:${GIT_PREVIOUS_COMMIT}</a></li>
 <li>GIT_PREVIOUS_SUCCESSFUL_COMMIT&nbsp;:${GIT_PREVIOUS_SUCCESSFUL_COMMIT}</a></li>
 <li>GIT_URL&nbsp;:${GIT_URL}</a></li>
 <li>GIT_URL_N&nbsp;:${GIT_URL_N}</a></li>
 <li>GIT_AUTHOR_NAME&nbsp;:${GIT_AUTHOR_NAME}</a></li>
 <li>GIT_COMMITTER_NAME&nbsp;:${GIT_COMMITTER_NAME}</a></li>
 <li>GIT_AUTHOR_EMAIL&nbsp;:${GIT_AUTHOR_EMAIL}</a></li>
 <li> GIT_COMMITTER_EMAIL&nbsp;:${ GIT_COMMITTER_EMAIL}</a></li>

這樣編譯完後,收到的郵件內容以下:
這裏寫圖片描述

如上所示,收到的郵件內容包含了Git的相關信息:

GIT_COMMIT118fa74e6a09c8c5ae713523692add256bfa6afb
GIT_BRANCH :origin/feature/UseByAnonymousDBMigrateAndApiChange
GIT_LOCAL_BRANCH${GIT_LOCAL_BRANCH}
GIT_PREVIOUS_COMMIT118fa74e6a09c8c5ae713523692add256bfa6afb
GIT_PREVIOUS_SUCCESSFUL_COMMIT118fa74e6a09c8c5ae713523692add256bfa6afb
GIT_URL :git@172.28.1.116:Android/WatchApp/Third/NetEaseCloudMusic.git
GIT_URL_N${GIT_URL_N}
GIT_AUTHOR_NAME${GIT_AUTHOR_NAME}
GIT_COMMITTER_NAME${GIT_COMMITTER_NAME}
GIT_AUTHOR_EMAIL${GIT_AUTHOR_EMAIL}
GIT_COMMITTER_EMAIL${ GIT_COMMITTER_EMAIL}

其中,GIT_BRANCH這個環境變量的值爲origin/feature/UseByAnonymousDBMigrateAndApiChange,表明Jenkins上/UseByAnonymousDBMigrateAndApiChange分支遠程Gitlab上該分支映射的遠程分支。所以咱們能夠對GIT_BRANCH這個環境變量作作文章。

將以前gradle腳本中的getGitBranch方法,作以下修改,區分編譯環境是Jenkins仍是本地。環境不一樣,運行不一樣的腳本獲取Git Branch的名字。當處於Jenkins環境的時候,先經過GIT_BRANCH這個環境變量獲取到Jenkins拉下來的分支對應的遠程分支,而後經過字符串分離,獲取到分支名。

/**
 * 獲取Git 分支名
 *
 *參考Jenkins git 建立文檔: https://wiki.jenkins.io/display/JENKINS/Git+Plugin
 *   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)
 *
 *
 */
def getGitBranch() {
    //判斷是否處於Jenkins編譯環境
    boolean flag = isInJenkins()
    if (flag) {
        ext.env = System.getenv()
        ext.gitBranch = env.GIT_BRANCH
        String[] stringArray = ext.gitBranch.split("/")
        if (stringArray.length > 0) {
            return stringArray[stringArray.length - 1]
        } else {
            return "UnKnown Branch"
        }
    } else {
        return 'git symbolic-ref --short -q HEAD'.execute().text.trim()
    }
}

完整代碼以下所示:

/** * 獲取Git 分支名 * *參考Jenkins git 建立文檔: https://wiki.jenkins.io/display/JENKINS/Git+Plugin * 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) * * */ def getGitBranch() { //判斷是否處於Jenkins編譯環境 boolean flag = isInJenkins() if (flag) { ext.env = System.getenv() ext.gitBranch = env.GIT_BRANCH String[] stringArray = ext.gitBranch.split("/") if (stringArray.length > 0) { return stringArray[stringArray.length - 1] } else { return "UnKnown Branch" } } else { return 'git symbolic-ref --short -q HEAD'.execute().text.trim() } } /** * 獲取Git 版本號 */ def getGitSHA() { return 'git rev-parse --short HEAD'.execute().text.trim() } /** * 獲取Git Tag */ def getGitTag() { return 'git describe --tags'.execute([], project.rootDir).text.trim() } /** * 獲取Git 提交次數 */ def getGitCommitCount() { return 100 + Interger.parse('git rev-list --count HEAD'.execute([], project.rootDir).text.trim()) } /** * 判斷是否有jenkins */ boolean isInJenkins() { Map<String, String> map = System.getenv() if (map == null) { return false } String str = map.get("Path") if (str != null) { //it's windows return false } else { str = "" Iterator it = map.iterator() while (it.hasNext()) { str += it.next() } return str.contains("jenkins") } } /** * 獲取jenkins任務名 */ def getJenkinsName() { boolean flag = isInJenkins() if (flag) { ext.env = System.getenv() ext.name = env.JOB_URL String[] stringArray = ext.name.split("/") if (stringArray.length > 0) { return stringArray[stringArray.length - 1] } else { return "Local" } } else { return "Local" } } /** * 獲取Jenkins Build 號 * @return */ def getJenkinsBuildCode() { boolean flag = isInJenkins() if (flag) { ext.env = System.getenv() ext.buildNumber = env.BUILD_NUMBER?.toInteger() return "$buildNumber" } else { return 0 } } /** * 定義幾個變量,在build.gradle裏面引用 */ ext { gitTag = getGitTag() gitBranch = getGitBranch() gitSHA = getGitSHA() jenkinsRevision = getJenkinsBuildCode() jenkinsName = getJenkinsName() } 

如今測試下Jenkins編譯是否正常,能夠看到一切都正常了。

這裏寫圖片描述

參考連接


這裏寫圖片描述

做者:歐陽鵬 歡迎轉載,與人分享是進步的源泉!
轉載請保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/77802596

若是以爲本文對您有所幫助,歡迎您掃碼下圖所示的支付寶和微信支付二維碼對本文進行隨意打賞。您的支持將鼓勵我繼續創做!

這裏寫圖片描述

相關文章
相關標籤/搜索