Add task pre and post operations (before, after)
Be great if there was a way to add post processing similar in concept to preconditions but a bit more flexible.
The goal is to be able to have pre- and post- operations surround a task sequence. For example, the pre- would be the setup (much like a test runner), and the post- would be the cleanup.
It's conceivable it can be done with sequential tasks under cmds: , but it gets messy because of the added bash code to track exit codes, shove in variables, add ignore_errors (which you may or may not forget to add or remove at times), and test those to abort follow on tasks but not the cleanup task.
It would be cleaner to have a pre- and post- sequence that could surround the cmd, instead of writing nuance around cmd sequences or adding additional tasks.
Thinking something like...
task:
example:
before:
- docker run -d --name server -p 8080:80 nginx
cmds:
- curl http://localhost:8080 | awk '{print}/error/{exit 1}'
- echo "I may or may not get here"
after:
- docker rm server -f
To extend it further, you could conceivably have different flags that could determine which operations in after should/should not run depending on the eventual exit result (it could even be a number, or simply a success/failure) ...
after:
- echo "Fires only if the cmds sequence failed"
if_errors: true
- echo "Fires if no error"
if_errors: false
- echo "Fires regardless"
- echo "Send message to slack if we got a 25, as we know where that came from"
if_error: 25
Related issues: https://github.com/go-task/task/issues/141 and maybe https://github.com/go-task/task/issues/275.
@fairbairn you may want to try: https://github.com/upcmd/up
It simply use composition model for you to assemble pre/post tasks, it is flexible to chain through, loop, do if/else logic etc.
I am not sure I understand the need for the before stanza: to me, it is the same as putting its contents as the first line (or lines) in cmds, also the expected behavior in case of error (exit with error) is the same. Or am I missing something?
On the other hand I understand the concept for after, it is equivalent to, say, Python finally: execute no matter what happened in cmds. While the proposal on_error from #141 is only in case of error.
My 2 cents is that after should be simple (no if_errors) and that it should be added after on_error is added.
I like how ConcourseCI does this. It feels very natural and quite powerful:
https://concourse-ci.org/modifier-and-hook-steps.html
This need for the if_errors and if_error could be replaced with the if operator from https://github.com/go-task/task/issues/608 if a special env var or variable was made available in an after clause with the return code of the last task run in the block.
I'm going to close this as a duplicate of #475 now that defer: is implemented:
- https://github.com/go-task/task/pull/626
- https://taskfile.dev/usage/#doing-task-cleanup-with-defer
The ability to run something on error is being track on #141.