Skip to content

Allow custom, non-zero exit code for core.setFailed #459

Closed

Description

@sudomateo

Currently, the core.setFailed function exits with an exit code of 1. However, certain applications use a non-zero exit code to indicate behavior to the user. One such example is the -detailed-exitcode argument to terraform plan (source). The ask here is to configure the core.setFailed function to accept a custom, non-zero exit code.

This will allow users of the core package to do something like this in their JavaScript actions.

const exitCode = 2
core.setFailed(`Failed with ${exitCode}`, exitCode);

I believe this change would work but I'll admit my TypeScript knowledge is novice at best.

export function setFailed(message: string | Error, code = ExitCode.Failure): void {
  process.exitCode = code

  error(message)
}

It might be required to add logic to ensure the passed in exit code is non-zero and non-negative since this is a setFailed function after all and we don't want someone calling core.setFailed("Failed", 0).

Activity

  1. ericsciple commented on May 18, 2020

    @ericsciple
    Contributor

    Not necessarily opposed but note it's about communication to the runner.

    Today 1 indicates failure, but may add other codes in the future. For example, during initial beta, 78 (iirc?) was used to indicate success, but skip downstream steps.

  2. sudomateo commented on May 19, 2020

    @sudomateo
    Author

    Thanks for the reply!

    Reading over the documentation here I see that GitHub Actions treats an exit code of 0 as success and a non-zero exit code as failure. However just below that we see the following example of how to set a failure exit code in a JavaScript action.

    try {
      // something
    } catch (error) {
      core.setFailed(error.message);
    }
    

    Below that we see another example of how to set a failure exit code in a Docker action.

    if <condition> ; then
      echo "Game over!"
      exit 1
    fi
    

    In these examples the Docker action provides more flexibility since we're able to exit with any code whereas a user of core.setFailed has no choice but to exit with 1.

    While I can work around this in the JavaScript action, I figured it might be worth adding the ability to fail with a custom, non-zero exit code directly to the library.

  3. sudomateo commented on Mar 28, 2024

    @sudomateo
    Author

    Closing this issue due to inactivity. Thank you all for reading and reacting to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions