git post-receive hook 来更新多个服务器

发布于 2024-10-01 17:56:47 字数 508 浏览 6 评论 0原文

我正在使用 git post-receive hook 用于在三台服务器(开发、测试和生产)上部署来自三个分支(主、临时和稳定)的 Web 应用程序版本。分支和服务器之间的配对当前在脚本中硬编码。不过,我想删除此限制,并使此挂钩可以管理无限数量的分支。可以通过以下方式完成:

  • 将所有每个分支的配置选项移动到一些单独的文件,例如 .git/???/
  • 主脚本将检查该文件是否是适用于每个分支,获取它,然后使用该文件中的配置参数将其部署到远程服务器上。

但是我不知道在 .git 目录中确切的位置可以放置这些文件。或者也许有更好的解决方案?

I'm using git post-receive hook to deploy versions of the web application from three branches (master, staging, and stable) on three servers (development, testing and production). The pairing between branches and servers is currently hard-coded in the script. However I'd like to remove this restriction and make this hook possible to manage unlimited quantity of branches. It can be done in the following way:

  • move all per-branch config options to some separate files, for example .git/???/<branch_name>
  • the main script will check if such file is available for every branch, source it and then deploy on the remote server using configuration parameters from that file.

However I don't know where exactly in .git directory can I place such files. Or maybe there is a better solution?

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

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

发布评论

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

评论(1

梦太阳 2024-10-08 17:56:47

据我所知,您的选择是:

  • 使用更强的服务器命名约定,以便除了基本域名之外不需要进行配置。将其留在脚本中。 (即,branchX ->branchX-deploy.example.com。)

  • 按照您的建议,将配置文件放在 .git 目录中的任何位置。如果它不是 git 关心的文件名,它永远不会注意到它。不过,我不确定您为什么要这样做。

  • 将其放入 .git/config 中。 Git 允许您定义branch..foo 形式的任意配置参数。 (我不知道这是一个功能还是一个疏忽。据我所知,它没有记录在案。)在您的情况下,类似 branch.master.deploy_server 设置为 development。 example.com。然后,您的脚本可以遍历所有分支,并检查该配置选项是否已设置。 (使用git config --get。)

  • 将配置文件放入存储库中。这似乎比隐藏起来要好得多。您不妨跟踪设置。如果有必要,提供一种方法来覆盖它们 - 提供不同的配置文件作为参数,提供单独的分支/服务器作为选项,交互式提示,任何适合您的内容。

就我个人而言,我可能会做最后一项。跟踪设置,即使它们只是默认设置,也不会伤害您。

Your options, as far as I can tell:

  • Use a stronger convention for server naming, so that config isn't necessary, beyond the base domain name. Leave that in the script. (That is, branchX -> branchX-deploy.example.com.)

  • Put a config file wherever you feel like in the .git directory, like you suggested. If it's not a filename git cares about, it'll never notice it. I'm not sure why you'd want to do this, though.

  • Put it in .git/config. Git lets you define arbitrary config parameters of the form branch.<name>.foo. (I have no idea if this is a feature or an oversight. It's not documented, as far as I know.) In your case, something like branch.master.deploy_server set to development.example.com. Your script could then just go through all branches, and check whether that config option is set or not. (Use git config --get.)

  • Put a config file in your repository. This seems a lot better than hiding it away. You might as well track the settings. If necessary, provide a way to override them - supply a different config file as an argument, supply individual branches/servers as options, interactive prompts, whatever suits you.

Personally, I'd probably do the last one. Tracking settings, even if they're only default settings, can't hurt you.

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