【发布时间】:2021-01-12 00:56:43
【问题描述】:
当我将某个分支合并到主分支(在 GitHub 中)时,会触发 jenkins 管道。
有没有办法在Jenkins当前执行期间识别使用git的任何命令“这是一个合并提交”? (不是 webhook - 我正在寻找另一个解决方案)
简单:我有提交 id 28c7be3b705fb517b09067e059fdlskkdjsa7ce0fd3,我想知道这个合并提交(PR 到 master)。
【问题讨论】:
当我将某个分支合并到主分支(在 GitHub 中)时,会触发 jenkins 管道。
有没有办法在Jenkins当前执行期间识别使用git的任何命令“这是一个合并提交”? (不是 webhook - 我正在寻找另一个解决方案)
简单:我有提交 id 28c7be3b705fb517b09067e059fdlskkdjsa7ce0fd3,我想知道这个合并提交(PR 到 master)。
【问题讨论】:
一种简单的方法是:检查此提交是否有第二个父项
if git rev-parse --verify -q $commitid^2 > /dev/null; then
echo "commit $commitid is a merge commit"
else
echo "commit $commitid is a simple commit"
fi
--verify 验证是否提供了一个参数,并且可以将其转换为可用于访问对象数据库的原始 20 字节 SHA-1。如果是,则将其发送到标准输出;否则,出错。-q 仅在 --verify 模式下有意义。如果第一个参数不是有效的对象名称,则不要输出错误消息;而是以非零状态静默退出。$commitid 提交的长或短 SHA-1 哈希。^2 ^ 表示“该提交对象的第一个父级”,附加了 2 这将变为“该提交对象的第二个父级”,因此 ^n = “该提交对象的第 n 个父级”,如果提交没有第n个父命令返回值为128(否则0)/dev/null -- 成功后,此命令会将输入转换为其完整的 sha 并在标准输出上打印。git rev-parse 的完整文档可用here。
如果你想检查 $commitid 是否是 Github 的 Pull 请求之一的一部分,这是一个不同的要求。
拉取请求信息的一部分(例如:特定 PR 指向的提交)存储在 git 中,但您需要使用 Github API (link here) 访问其他信息(例如:PR 状态,它的作者,它的讨论提要等...)
Github 在 git 中为每个以 refs/pull/ 前缀的拉取请求创建引用;对于拉取请求{xx},拉取请求的头部存储在refs/pull/{xx}/head,如果PR被合并,合并的结果将存储在refs/pull/{xx}/merge。
您可以使用它来确定当前提交是否是拉取请求的头部:
# fetch references starting with 'refs/pull/...',
# store them locally next to the remote branches : 'refs/remotes/origin/pull/...'
# (note: you can choose whatever pattern you want to store these references locally)
git fetch origin "+refs/pull/*:refs/remotes/origin/pull/*"
# check if one of these refs point to $commitid :
git for-each-ref --points-at "$commitid" refs/remotes/orign/pull
# you can use '--format' to customize the output :
# for example you can remove the leading 'refs/remotes/origin/pull' to have a shorter output :
$ git for-each-ref --format="%(refname:lstrip=4)" --points-at "$commitid" refs/remotes/orign/pull
42/head # <- this means it is the current head of PR #42
您现在可以使用 API 检查 PR #42 是否仍然打开:
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/{myuser}/{myrepo}/pulls/42
【讨论】:
我想扩展LeGEC's answer。
if git rev-parse --verify -q $commitid^2 > /dev/null; then
echo "commit $commitid is a merge commit"
else
echo "commit $commitid is a simple commit"
fi
--verify 验证是否提供了一个参数,并且可以将其转换为可用于访问对象数据库的原始 20 字节 SHA-1。如果是,则将其发送到标准输出;否则,出错。-q 仅在 --verify 模式下有意义。如果第一个参数不是有效的对象名称,则不要输出错误消息;而是以非零状态静默退出。$commitid 提交的长或短 SHA-1 哈希。^2 ^ 表示“该提交对象的第一个父级”,附加了 2 这将变为“该提交对象的第二个父级”,因此 ^n = “该提交对象的第 n 个父级”,如果提交没有第n个父命令返回值为128(否则0)提供完整文档here。
【讨论】: