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

Allow defer to know if the task succeded or failed #1484

Closed
andreynering opened this issue Jan 26, 2024 Discussed in #1472 · 4 comments · Fixed by #1762
Closed

Allow defer to know if the task succeded or failed #1484

andreynering opened this issue Jan 26, 2024 Discussed in #1472 · 4 comments · Fixed by #1762
Labels
area: variables Changes related to variables.

Comments

@andreynering
Copy link
Member

Originally asked in #1472 by @your-diary.

A possible way to allow this is to expose one/some variables to defer. One possibility is to expose a DEFER_EXIT_CODE var which would contain 0 for success or the exit code of the task that failed.

Perhaps we could also have DEFER_SUCCESS (true/false) and DEFER_FAIL (true/false) to make a simpler check easier? (Not sure, DEFER_EXIT_CODE is probably enough).

@andreynering andreynering added type: enhancement area: variables Changes related to variables. labels Jan 26, 2024
@dorimon-1
Copy link
Contributor

dorimon-1 commented Mar 22, 2024

@andreynering Could you explain a bit deeper how you would the yml to be?
I thought about

  task-2:
    - defer_failure: echo Failure!
    - defer_failure: { task: "task-1", vars: { PARAM: "failure" } }
    - defer_success: echo Success!
    - defer_success: { task: "task-1", vars: { PARAM: "success" } }
    - echo 'cmd ran'
    - exit 1

making it very similar to the regular defer, but a bit different from what @your-diray suggested

@your-diary
Copy link

your-diary commented Mar 22, 2024

@dorimon-1
One can ignore my original suggestion. It is just an example.

As for your suggestion, if we add defer_success and defer_failure while keeping defer keyword, we can make the change backward-compatible.

Personally I prefer

A possible way to allow this is to expose one/some variables to defer. One possibility is to expose a DEFER_EXIT_CODE var which would contain 0 for success or the exit code of the task that failed.

in the OP though, because no new syntax is needed:

version: "3"

#This should be automatically set.
env:
    DEFER_EXIT_STATUS: 0

tasks:
    default:
        cmds:
            - defer: |
                  if test ${DEFER_EXIT_STATUS} == 0; then
                      echo success
                  else
                      echo failure
                  fi

@KJone1
Copy link

KJone1 commented Mar 23, 2024

Exactly what I have been searching for.

I think @dorimon-1 idea looks neat and allows for backward compatibility with regular defer.
Also having env var for defer status code could be helpful for some ppl.
If someone could implement this it would be much appreciated 😉.

@andreynering
Copy link
Member Author

This was just implemented on #1762.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: variables Changes related to variables.
Projects
None yet
5 participants