Question

Initialize rbenv and run ruby script from shell script

I want svnserve to run pre-commit hook, written on ruby. As the svnserve is run as root user, it knows nothing about user rbenv installation.

I set a soft link /usr/bin/ruby -> /home/admin/.rbenv/shims/ruby . As result, when i try

#!/usr/bin/ruby
puts "Pre-commit hook!"

It shows error:

Transmitting file data .svn: Commit failed (details follow):
svn: Commit blocked by pre-commit hook (exit code 255) with no output.

When i run manually on Server:

admin $ sudo ./pre-commit
/usr/bin/ruby: line 4: exec: rbenv: not found

So, i suppose, that rbenv initialization is needed, but how?

Was it helpful?

Solution

In hooks path:

pre-commit:

#!/bin/bash
export HOME=/home/user
if [ -d $HOME/.rbenv ]; then
  export PATH="$HOME/.rbenv/shims:$PATH"
  eval "$(rbenv init -)"
fi
$0.rb $*

pre-commit.rb:

#!/usr/bin/env ruby
ARGV.each_with_index { |arg, index| puts "Index #{index}: Argument #{arg}" }

OTHER TIPS

you should initialize rbenv before using it.

/path/to/user/home/.rbenv/bin/rbenv init

then set ruby version globally:

rbenv global DESIRED-RUBY-VERSION

or localy:

rbenv local DESIRED-RUBY-VERSION

or per shell:

rbenv shell DESIRED-RUBY-VERSION

though not sure shell setting will work without a tty.

so you could create a shell script, pre-commit.sh and register it as a svn hook.

inside it initialize rbenv and call your pre-commit.rb file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top