Set an Exit Code on Custom Github Actions to Indicate a Passing or Failing Status

Colby Fayock
InstructorColby Fayock
Share this video with your friends

Social Share Links

Send Tweet

In this lesson, we'll walk through setting a failed exit code for our custom Github Action. We'll set up restrictions around our existing input value and explicitly set a failed status when trying to use an invalid input.

Colby Fayock: [0:00] We're going to start off with a custom GitHub Action that logs a Futurama quote. Inside our configuration, we're setting inputs of character so that we can choose the value, and an output so that we can use that value downstream. Inside our function, we're reaching out to grab that quote, and then logging it to the console.

[0:15] Since this is a job on our pipeline, we want to make sure that if something fails, we correctly set an exit code. To do this, GitHub provides a method in its core toolkit where we can set a failed status. To test this, I created a new array of available characters, which will only allow for our input for the API.

[0:30] Next, I'm going to create an if statement. I'm going to say that if the character input isn't in the AVAILABLE_CHARACTERS array, we're going to return.

[0:37] If we simply return, that's not going to be considered failed. I'm going to use the core toolkit and say core.setFailed(). I'm going to say unknown character, and the character name.

[0:47] We're not going to be able to test this by running the Node script because it uses the dr-zoidberg as a default value. Instead, I'm going to go ahead and clone these uses.

[0:55] I'm going to change the character to colby, my name. Also, let's get rid of this id of futurama, since this only applies to the echo statement.

[1:02] Before we push, let's build our code. Right, now after we push our code and run the job, we can see that the original step completed, but our new step failed with an unknown character of colby. One thing to also keep in mind is that when that step fails, it prevents the future ones from running.

[1:17] As you can see, even though we would echo the output from our first step, it's still not going to run. In review, in order to test the case where our code failed, we set up a new array of AVAILABLE_CHARACTERS. If our input character didn't match that array, we set a failed status of unknown character.

[1:32] To test this we created a new step where we set the character name to colby which doesn't exist. Once we push that out and it ran, we can see that we get an unknown character of colby.