diff --git a/README.md b/README.md index 6dfa26ea..ed6afca2 100644 --- a/README.md +++ b/README.md @@ -908,6 +908,8 @@ You can run custom scripts at specific points with hooks. Hooks should be stored in the .mrsk/hooks folder. Running mrsk init will build that folder and add some sample scripts. +You can change their location by setting `hooks_path` in the configuration file. + If the script returns a non-zero exit code the command will be aborted. There are currently two hooks: diff --git a/lib/mrsk/commands/hook.rb b/lib/mrsk/commands/hook.rb index 5082e20c..912d4480 100644 --- a/lib/mrsk/commands/hook.rb +++ b/lib/mrsk/commands/hook.rb @@ -1,6 +1,6 @@ class Mrsk::Commands::Hook < Mrsk::Commands::Base def run(hook, **details) - [ ".mrsk/hooks/#{hook}", env: tags(**details).env ] + [ hook_file(hook), env: tags(**details).env ] end def hook_exists?(hook) @@ -9,6 +9,6 @@ class Mrsk::Commands::Hook < Mrsk::Commands::Base private def hook_file(hook) - ".mrsk/hooks/#{hook}" + "#{config.hooks_path}/#{hook}" end end diff --git a/lib/mrsk/configuration.rb b/lib/mrsk/configuration.rb index f4837079..c21c0149 100644 --- a/lib/mrsk/configuration.rb +++ b/lib/mrsk/configuration.rb @@ -6,7 +6,7 @@ require "erb" require "net/ssh/proxy/jump" class Mrsk::Configuration - delegate :service, :image, :servers, :env, :labels, :registry, :builder, :stop_wait_time, to: :raw_config, allow_nil: true + delegate :service, :image, :servers, :env, :labels, :registry, :builder, :stop_wait_time, :hooks_path, to: :raw_config, allow_nil: true delegate :argumentize, :argumentize_env_with_secrets, :optionize, to: Mrsk::Utils attr_accessor :destination @@ -197,6 +197,10 @@ class Mrsk::Configuration raw_config.traefik || {} end + def hooks_path + raw_config.hooks_path || ".mrsk/hooks" + end + private # Will raise ArgumentError if any required config keys are missing def ensure_required_keys_present diff --git a/test/commands/hook_test.rb b/test/commands/hook_test.rb index 6d1380fd..f88d87b3 100644 --- a/test/commands/hook_test.rb +++ b/test/commands/hook_test.rb @@ -25,8 +25,18 @@ class CommandsHookTest < ActiveSupport::TestCase ], new_command.run("foo") end + test "run with custom hooks_path" do + assert_equal [ + "custom/hooks/path/foo", + { env: { + "MRSK_RECORDED_AT" => @recorded_at, + "MRSK_PERFORMER" => @performer, + "MRSK_VERSION" => "123" } } + ], new_command(hooks_path: "custom/hooks/path").run("foo") + end + private - def new_command - Mrsk::Commands::Hook.new(Mrsk::Configuration.new(@config, version: "123")) + def new_command(**extra_config) + Mrsk::Commands::Hook.new(Mrsk::Configuration.new(@config.merge(**extra_config), version: "123")) end end