git hook 中的 Ruby on Rails

发布于 2024-11-17 02:30:04 字数 448 浏览 1 评论 0原文

我正在构建一个 Rails 应用程序,该应用程序将连接到 git 存储库,并在推送存储库时执行一些代码。我正在尝试使用 git hooks 来执行此操作,但我似乎无法从所述钩子中使用 Rails 应用程序。

我曾尝试使用rails runner作为钩子的shebang(#!/path/to/rails runner),但由于空间不被视为shebangs中的参数分隔符,因此尝试执行一个名为“rails runner”的文件,该文件不起作用。我还尝试将脚本作为 Ruby (#!/usr/bin/ruby) 执行并包含 Rails config/environment.rb 文件,但这似乎触发了旧的“app/models/X.rb Expected to Define X”错误。我现在没有代码,所以我无法粘贴预期的功能来查看我的代码中的某个地方是否有错误,但我只想知道在外部执行 Rails 代码的正确方法是什么Rails 应用程序路径是?

谢谢

I am in the process of building a Rails app that will hook into a git repository and will execute some code whenever the repository is pushed to. I am trying to do this using git hooks, but I can't seem to be able to work with the Rails app from within said hooks.

I have tried using rails runner as the shebang (#!/path/to/rails runner) of the hooks but because the space is not considered an argument separator in shebangs this tries to execute a file called "rails runner" which does not work. I have also tried executing the script as Ruby (#!/usr/bin/ruby) and including the rails config/environment.rb file, but this seems to trigger the old "app/models/X.rb Expected to define X" error. I don't have the code on me at the moment so I can't paste the intended functionality to see if there's just an error in my code somewhere, but I just want to know what the correct approach to executing Rails code outside of the Rails app path is?

Thanks

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

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

发布评论

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

评论(2

杀手六號 2024-11-24 02:30:04

您应该能够使用 /usr/bin/env 技巧:

#!/usr/bin/env /path/to/rails runner

env 实用程序 只是将其参数作为普通命令运行,它通常用于执行 PATH 搜索 shebang 中的内容。

You should be able to use the /usr/bin/env trick:

#!/usr/bin/env /path/to/rails runner

The env utility just runs its arguments as a normal command, it is usually used to perform PATH searches for things in the shebang.

烟燃烟灭 2024-11-24 02:30:04

如果 @mu is Too Short 所建议的不起作用,您可以尝试在 Rails 应用程序中触发一个方法,将大部分代码保留在应用程序中,而不是在 git hook 中。

就像这样:

#!/bin/sh

bundle exec rails runner 'SomeJob.perform'

SomeJob#perform 方法执行实际工作,而您的 shell 脚本只是调用它。

If what @mu is too short has suggested doesn't work, you might try just triggering a method inside your Rails app, keeping most of the code inside the app, and not in the git hook.

Like this:

#!/bin/sh

bundle exec rails runner 'SomeJob.perform'

Where the #perform method of SomeJob does the actual work and your shell script is merely invoking it.

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