Ruby on Rails in a git hook
I am in the process of building a Rails app that will hook into a git repository and will execute some c开发者_运维百科ode 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
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.
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.
精彩评论