我如何确定源自GitLab中提交的行动?
在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上。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了通常在其他环境变量。
github_via
最接近的是gl_protocol
,它可以告诉您一些信息,例如使用HTTP/SSH或Web UI进行推动。该变量的值将http
ssh
或Web
相应地。因此,在您的情况下,您可能会做类似的事情:
值得一提/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 beGL_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 eitherhttp
ssh
orweb
accordingly.So, in your case you might do something like:
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.