在Jenkins上使用Gradle添加git分支作为属性 [英] Adding git branch as a properties using Gradle on Jenkins

查看:182
本文介绍了在Jenkins上使用Gradle添加git分支作为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目的构建过程中将当前的git分支添加到属性中。

我的项目既可以在本地构建,也可以使用Jenkins构建。当项目在本地构建时,这非常简单,我定义了以下任务来成功获取分支:

 任务getBranch { 
def cmd =git rev-parse --abbrev-ref HEAD
def proc = cmd.execute()
def branch = proc.text.trim()
ext .branch = branch
}

然而,Jenkins构建过程的一部分包括检查最后在一个分支上提交到它自己的分支中,让项目处于分离状态:

  commit_hash = $ {git rev- parse refs / remotes / origin / BRANCH ^ {commit}} 
git checkout -f $ {commit_hash}

此时,我可以运行 git reflog show -n1 来获得:

  HEAD @ {0}:checkout:从BRANCH移动到$ commit_hash 

m将不得不写一些 groovy 代码从该格式的字符串中获取BRANCH的值。但是,我想知道是否有一种更直接的方式从git获得BRANCH?

解决方案

正如如何在分离的HEAD状态下找到当前的git分支,首先检查环境变量 GIT_BRANCH 没有你要找的东西。



否则,你需要解析reflog或远程分支(因为Jenkins Git repo克隆工作空间不有当地分支机构)

pre $ g $ git for-each-ref --format =%(objectname)%(refname:short) refs / remotes / origin | awk/ ^ $(git rev-parse HEAD)/ {print $ 2}


I would like to add the current git branch to properties during the build process of my project.

My project can either be built locally or using Jenkins. When the project is built locally this is quite straightforward, I have the following task defined that gets the branch successfully:

task getBranch {
    def cmd = "git rev-parse --abbrev-ref HEAD"
    def proc = cmd.execute()
    def branch = proc.text.trim()
    ext.branch = branch
}

However, part of the Jenkins build process involves checking out the last commit on a branch into its own branch, leaving the project in a detached head state:

commit_hash=${git rev-parse refs/remotes/origin/BRANCH^{commit}}
git checkout -f ${commit_hash}

At this point I can run git reflog show -n1 to get:

HEAD@{0}: checkout: moving from BRANCH to $commit_hash

I'm going to have to write some groovy code to get the value of BRANCH from a string in that format. However, I was wondering if there was a more straightforward way to get BRANCH from git?

解决方案

As mentioned in "How to find the current git branch in detached HEAD state", check first if the environment variable GIT_BRANCH does not have what you are looking for.

Otherwise, you need to parse reflog, or the remote branches (since a Jenkins Git repo cloned workspace does not have local branches)

git for-each-ref --format="%(objectname) %(refname:short)" refs/remotes/origin | awk "/^$(git rev-parse HEAD)/ {print $2}"

这篇关于在Jenkins上使用Gradle添加git分支作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆