git hook 中的 Ruby on Rails
我正在构建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够使用
/usr/bin/env
技巧:env
实用程序 只是将其参数作为普通命令运行,它通常用于执行PATH
搜索 shebang 中的内容。You should be able to use the
/usr/bin/env
trick:The
env
utility just runs its arguments as a normal command, it is usually used to performPATH
searches for things in the shebang.如果 @mu is Too Short 所建议的不起作用,您可以尝试在 Rails 应用程序中触发一个方法,将大部分代码保留在应用程序中,而不是在 git hook 中。
就像这样:
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:
Where the
#perform
method ofSomeJob
does the actual work and your shell script is merely invoking it.