2

I've got a very basic pre-commit-hook with the following code in it:

#!/usr/bin/env bash
vagrant ssh -c "/var/www/public/tests.sh"

But whenever I try to commit I get the following message:

.git/hooks/pre-commit: line 2: vagrant: command not found

Vagrant is installed and it is running. If I execute the command manually it works, only if Git tries to run it, it doesn't.

I already tried the following:

  • Add an extra script pre-commit.sh in the root of my project and calling that from the pre-commit hook.
  • Added absolute path to Vagrant binary in my pre-commit hook, but then I get the message my virtual box is not running (although it is).
Giel Berkers
  • 2,852
  • 3
  • 39
  • 58

1 Answers1

1

The pre-commit is running in its own shell with its own environment. It does not necessarily share your environment, aka your PATH or current directory. You need to be explicit in your pre-commit hook. So yes, you either need to specify the full path to the vagrant binary or you have to make sure that PATH is properly setup for the environment which runs the pre-commit hook.

The reason you get the "virtual box is not running" message when using the full path is, that the commit hook is not executed in the same directory as your Vagrantfile. Is your Vagrantfile in the root of the git repository. Then I would expect it to work - see In a git hook is the current working directory guaranteed to be within the git repository?

If your Vagrantfile is located somehwere else, you have a few options. You can set VAGRANT_CWD and give Vagrant a hint where your Vagrantfile is located (see https://www.vagrantup.com/docs/vagrantfile/) or you specify the id of the running box:

vagrant ssh <box-id> ...

One can use

vagrant global-status 

to determine this id. Or you just make sure that the script changes into the directory of the Vagrantfile.

Community
  • 1
  • 1
Hardy
  • 18,659
  • 3
  • 49
  • 65