每个分支的 Git 提交挂钩

发布于 2024-11-15 12:25:52 字数 168 浏览 4 评论 0原文

我正在努力学习 git 的一些更高级的用法,我认为 hooks 是我想要的方式,也许有人可以在这里给我一些建议。

我的计划是拥有一个包含 3 个分支(开发、登台和生产)的 git 存储库。我希望提交这 3 个分支中的每一个,以在提交后触发不同的脚本。

git 有能力做到这一点还是我找错了树?

I'm working on getting into some more advanced usage of git, and I think hooks are the way that I want to go, perhaps somebody can give me some advice here.

My plan is to have a git repository with 3 branches (development, staging, and production). I want commits to each of these 3 branches to trigger a different script post-commit.

Does git have the capability to do this or am I barking up the wrong tree?

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

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

发布评论

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

评论(3

素食主义者 2024-11-22 12:25:52

在提交后挂钩中,您可以执行以下操作:

if [ `git rev-parse --abbrev-ref HEAD` == "development" ]; then
   echo "development-script"
elif [ `git rev-parse --abbrev-ref HEAD` == "staging" ]; then
   echo "staging-script"
elif [ `git rev-parse --abbrev-ref HEAD` == "production" ]; then
   echo "production-script"
fi

In a post-commit hook you could do the following:

if [ `git rev-parse --abbrev-ref HEAD` == "development" ]; then
   echo "development-script"
elif [ `git rev-parse --abbrev-ref HEAD` == "staging" ]; then
   echo "staging-script"
elif [ `git rev-parse --abbrev-ref HEAD` == "production" ]; then
   echo "production-script"
fi
沉鱼一梦 2024-11-22 12:25:52

我为自己编写了一个脚本来执行此功能。

https://github.com/fotuzlab/githubdump-php

将此文件托管在您的服务器上,最好是 repo root 并在 github webhooks 中定义 url。将第 8 行的“allcommits”更改为您的分支名称,并在第 18 行添加您的代码/函数。

您的所有 3 个实例将需要单独的文件和 Webhook。

I had written a script for myself to do this functionality.

https://github.com/fotuzlab/githubdump-php

Host this file on your server, preferably repo root and define the url in github webhooks. Change 'allcommits' on line 8 with your branch name and add your code/function at line 18.

You will need separate files and webhooks for all your 3 instances.

一片旧的回忆 2024-11-22 12:25:52

Simon Boison 示例的轻量级替代方案:

branch="$(git rev-parse --abbrev-ref HEAD)"

if [ $branch = development ]; then
    echo "development-script"

elif [ $branch = staging ]; then
    echo "staging-script"

elif [ $branch = production ]; then
    echo "production-script"

fi

A lightweight alternative to Simon Boison's example:

branch="$(git rev-parse --abbrev-ref HEAD)"

if [ $branch = development ]; then
    echo "development-script"

elif [ $branch = staging ]; then
    echo "staging-script"

elif [ $branch = production ]; then
    echo "production-script"

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