我如何确定源自GitLab中提交的行动?

发布于 2025-02-13 12:06:00 字数 1359 浏览 1 评论 0 原文

在github中,诸如github_via之类的环境变量揭示了起源于git commit的动作。在下面的示例中,取自在这里,这用于保护默认分支。

Gitlab有类似的东西吗?我正在编码预先接收的钩子,我找不到Gitlab文档上任何地方记录的。

#!/bin/bash
#
# This hook restricts changes on the default branch to those made with the GUI Pull Request Merge button, or the Pull Request Merge API.
#
DEFAULT_BRANCH=$(git symbolic-ref HEAD)
while read -r oldrev newrev refname; do
  if [[ "${refname}" != "${DEFAULT_BRANCH:=refs/heads/master}" ]]; then
    continue
  else
    if [[ "${GITHUB_VIA}" != 'pull request merge button' && \
          "${GITHUB_VIA}" != 'pull request merge api' ]]; then
      echo "Changes to the default branch must be made by Pull Request. Direct pushes, edits, or merges are not allowed."
      exit 1
    else
      continue
    fi
  fi
done

我正在寻找可以在gitlab上的预亲人的上下文中使用的环境变量,例如这些在GHE上。

In Github there are environment variables like GITHUB_VIA that expose the action that originated a git commit. In the example below, taken from here, this is used to protect default branches.

Is there something similar for Gitlab? I'm coding a pre-receive hook and I can't find this documented anywhere on Gitlab's doc.

#!/bin/bash
#
# This hook restricts changes on the default branch to those made with the GUI Pull Request Merge button, or the Pull Request Merge API.
#
DEFAULT_BRANCH=$(git symbolic-ref HEAD)
while read -r oldrev newrev refname; do
  if [[ "${refname}" != "${DEFAULT_BRANCH:=refs/heads/master}" ]]; then
    continue
  else
    if [[ "${GITHUB_VIA}" != 'pull request merge button' && \
          "${GITHUB_VIA}" != 'pull request merge api' ]]; then
      echo "Changes to the default branch must be made by Pull Request. Direct pushes, edits, or merges are not allowed."
      exit 1
    else
      continue
    fi
  fi
done

I'm looking for environment variables I could use within the context of a pre-receive on Gitlab, such as these ones on GHE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2025-02-20 12:06:00

除了通常在其他环境变量 github_via 最接近的是 gl_protocol ,它可以告诉您一些信息,例如使用HTTP/SSH或Web UI进行推动。该变量的值将 http ssh Web 相应地。

因此,在您的情况下,您可能会做类似的事情:

if [[ "$GL_PROTOCOL" != "web" ]]; then
  echo "Changes to the default branch must be made by Pull Request. Direct pushes, edits, or merges are not allowed."
  exit 1
fi   

值得一提/project/protected_branches.html“ rel =“ nofollow noreferrer”>受保护的分支。例如,您可以创建一个受保护的分支规则,该规则禁止将其推入分支,并且只允许通过MR进行更改。因此,您应该能够在GitLab中实现相同的效果,而无需预先接管钩子。

Besides the information that is normally exposed in git pre-receive hooks, GitLab does provide some additional environment variables. The closest thing to GITHUB_VIA would be GL_PROTOCOL, which can tell you some information like if a push was made using http/ssh or the web UI. The value of this variable will be either http ssh or web accordingly.

So, in your case you might do something like:

if [[ "$GL_PROTOCOL" != "web" ]]; then
  echo "Changes to the default branch must be made by Pull Request. Direct pushes, edits, or merges are not allowed."
  exit 1
fi   

It is worth mentioning, however, that GitLab has controls for this use case directly in the project settings in protected branches. For example, you can create a protected branch rule that prohibits pushes to the branch and only allows changes via an MR. So you should be able to achieve the same effect in GitLab without a pre-receive hook.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文