task icon indicating copy to clipboard operation
task copied to clipboard

Add task pre and post operations (before, after)

Open fairbairn opened this issue 5 years ago • 4 comments

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
   

fairbairn avatar Jun 12 '20 14:06 fairbairn

Related issues: https://github.com/go-task/task/issues/141 and maybe https://github.com/go-task/task/issues/275.

andreynering avatar Jun 13 '20 00:06 andreynering

@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.

stephencheng avatar Jul 18 '20 16:07 stephencheng

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.

marco-m avatar Aug 18 '20 06:08 marco-m

I like how ConcourseCI does this. It feels very natural and quite powerful:

https://concourse-ci.org/modifier-and-hook-steps.html

ghostsquad avatar Dec 09 '21 03:12 ghostsquad

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.

markmsmith avatar Feb 11 '23 20:02 markmsmith

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.

andreynering avatar Feb 11 '23 22:02 andreynering