Using tasks and hooks
Defining tasks¶
Tasks are basically functions defined within Bootleg configuration files to accomplish one or more goals in the deployment process.
Example - Clear an application cache
1 2 3 4 5 6 7 8 | use Bootleg.DSL task :clear_cache do UI.puts "Clearing cache" remote :app do "rm /opt/app/cachefile" end end |
:app
servers.
Existing tasks, including those that come with Bootleg, can be redefined by you as needed.
Running tasks¶
Invoking tasks from the command line¶
Bootleg comes with several Mix tasks, but the one we'll show you here is called invoke.
Using bootleg.invoke
1 2 3 | $ mix bootleg.invoke foo Clearing cache ...[snip] |
:app
servers would appear here.
Invoking tasks from other tasks¶
Tasks can invoke other tasks.
1 2 3 4 5 6 7 8 9 | use Bootleg.DSL task :one do invoke :two end task :two do UI.puts "tada!" end |
1 2 3 4 | $ mix bootleg.invoke one tada! $ mix bootleg.invoke two tada! |
Invoking a task will also invoke any tasks that have been registered to run as hooks before/after that task.
Note
Invoking an undefined task is not an error and any registered hooks for that task will still be executed.
Using hooks¶
In addition to being run from tasks, tasks can be set to run before or after other tasks.
Defining hooks for arbitrary tasks¶
Hooks can be defined for any task (built-in or user defined), even those that do not exist. This can be used to create an "event" that you want to respond to, but has no real "implementation".
Future proofed
1 2 3 4 5 | use Bootleg.DSL before_task :the_big_one do IO.puts "it's time to get outta here!" end |
:the_big_one
.
Hooking into built-in Bootleg tasks¶
Much of Bootleg is written using Bootleg tasks. This means you can hook into many different parts of the build and deployment process.
Notifying an external service
1 2 3 4 5 6 7 8 9 | use Bootleg.DSL before_task :build do MyAPM.notify_build() end after_task :deploy do MyAPM.notify_deploy() end |
For a list of tasks that you can hook into, refer to the workflows.
Defining multiple hooks for the same task¶
You can define multiple hooks for a task, and they will be executed in the order they are defined.
Using multiple hooks
1 2 3 4 5 6 7 8 9 10 11 12 13 | use Bootleg.DSL before_task :start do IO.puts "This may take a bit" end after_task :start do IO.puts "Started app!" end before_task :start do IO.puts "Starting app!" end |
1 2 3 4 | $ mix bootleg.invoke start This may take a bit Starting app! Started app! |
When invoking a task, the order in which hooks have been defined for that task is respected.