Ruby
rake

Rakeの終了時に実行したtaskの成否の通知を送る方法

More than 1 year has passed since last update.

at_exitを使うと終了時に処理を実行できる。
通知を送る処理をat_exitで登録するRakeタスクを定義し成否を通知したいタスクの依存タスクに追加するとべんり。

Rakefile
desc "notify the tasks finished successfully or not"
task :notify_tasks_finished do
  at_exit do
    tasks = Rake.application.top_level_tasks.join(", ")
    if $!.nil? || $!&.success?
      puts "success: #{tasks}"
    else
      puts "fail: #{tasks}"
    end
  end
end

desc "hey"
task hey: :notify_tasks_finished do
  puts "hey"
end


desc "yo"
task yo: :notify_tasks_finished do
  yo
end
% rake hey
hey
success: hey
% rake yo
rake aborted!
NameError: undefined local variable or method `yo' for main:Object
/home/sei/tmp/rake.rb:22:in `block in <top (required)>'
Tasks: TOP => yo
(See full trace by running task with --trace)
fail: yo