Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task pre and post operations (before, after) #344

Closed
fairbairn opened this issue Jun 12, 2020 · 6 comments
Closed

Add task pre and post operations (before, after) #344

fairbairn opened this issue Jun 12, 2020 · 6 comments

Comments

@fairbairn
Copy link

fairbairn commented Jun 12, 2020

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
   
@andreynering andreynering added type: feature A new feature or functionality. proposal labels Jun 13, 2020
@andreynering
Copy link
Member

Related issues: #141 and maybe #275.

@stephencheng

This comment was marked as off-topic.

@marco-m
Copy link
Contributor

marco-m commented Aug 18, 2020

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.

@ghostsquad
Copy link
Contributor

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

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

@markmsmith
Copy link

This need for the if_errors and if_error could be replaced with the if operator from #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.

@andreynering
Copy link
Member

andreynering commented Feb 11, 2023

I'm going to close this as a duplicate of #475 now that defer: is implemented:

The ability to run something on error is being track on #141.

@pd93 pd93 removed type: feature A new feature or functionality. type: proposal labels Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants