elixir_git_hooks
elixir_git_hooks copied to clipboard
Document how to override the MIX_ENV when running hooks
I have a typical use case, a pre_push
hook will run tests before uploading patches to the remote repository. The default environment is dev
so , but my tests should run in the test
environment. I end up having to run this awkward command to get the correct environment:
MIX_ENV=test git push
Is there a recommended way to transparently override the environment? Should this be included in the documentation?
Perhaps the git_hooks configuration could support a general environment override like,
environment:
MIX_ENV: test
... or a specific one like,
mix_env: test
For me, a reasonable workaround is to manually edit .git/hooks/pre_push
to include the line,
MIX_ENV=test mix git_hooks.run pre_push "$@"
but it would be better to set the environment within the git_hooks.run mix task, to make it dynamically configurable without having to reinstall the hook.
@adamwight can you post your git_hooks
configuration?
If you use a string to run the mix task, you will need to declare the mix environment, as in your example. But instead of that, you can use {:mix_task, :test}
and you won't need to do anything else.
The README states that are the preferred option but feel free to submit any PR if you believe it's not clear enough ☺️
I see what you mean, now. "mix test" was a bad example because it correctly assumes it should be running in the test environment. However, I have other tasks which still need the environment set explicitly—maybe the problem is in how I've configured these tasks, not in how git_hooks runs them.
My configuration looks like this,
if Mix.env() == :test do
config :git_hooks,
verbose: true,
hooks: [
pre_push: [
tasks: [
{:mix_task, :clean},
{:mix_task, :compile, ["--warnings-as-errors"]},
{:mix_task, :format, ["--check-formatted"]},
{:mix_task, :credo, ["--strict"]},
{:mix_task, :test},
{:mix_task, :coveralls},
{:mix_task, :dialyzer},
{:mix_task, :doctor}
]
]
],
extra_success_returns: [
# The compile step is being obstinate, it seems to give a special result
# when run with --warnings-as-errors.
{:ok, []}
]
and in my mix.exs, I have deps like:
{:credo, "~> 1.0", only: :test, runtime: false}
So the "mix credo" task is only accessible when MIX_ENV=test.
This is the magic I was looking for... feel free to close the issue!
# mix.exs
def project do
[
# ...
preferred_cli_env: [{:"git_hooks.run", :test}]
]
end
I checked how to change the mix environment at runtime some time ago but couldn't find a way.
You could use credo on dev
environment as an alternative, but glad you worked it out!