Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Attempting to fail a pipeline conditionally from within a node.js script called from bash

Andy Rouse March 2, 2023

As part of an automated pipeline, I'd like to run some data quality checks on the files being merged. To do this, I'm running a node.js script. If data quality constraints aren't met, I'd like to fail the pipeline with a message.

I've tried every suggested option in the following stack exchange article, but none are working: How do I fail a script running in Bitbucket Pipelines? 

I can see in the output from the Pipeline that the exception is being thrown:

Screenshot 2023-03-02 091314.png

However, the pipeline is just carrying on to the next line in the bash script, rather than failing with the message that has been set.

1 answer

1 accepted

0 votes
Answer accepted
Erez Maadani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 2, 2023

Hey @Andy Rouse 

If I understand correctly, you have the pipeline script which triggers a node.js script. 

Usually, shell scripts tend to ignore errors and use only the last command result code as the whole script result code. So if you have a script which looks like that:

func1 //This complete successfully

func2 //This fails

func3 //This complete successfully

Then the result of the whole script is successful.

In such cases you have two options:

  1. Check the result of each func and exit if the result code is not 0.
  2. Try and add flags which control the script behavior. If you are using bash or shell scripts: "set -e" and/or "set -o pipefail" will help you to stop the execution in case of an error in one of the func calls. 
Andy Rouse March 5, 2023

Hey @Erez Maadani - Thanks for the reply. The script I'm calling is Node.js.

For clarity - Are you saying there's nothing i can do directly in the Node.js script to cause the pipeline to to fail? That's useful to know.

Additionally, I'm inspecting the error code from the node.js (which I'm having throw an error) and the exit code is still 0. Is there an established way to set the exit code from node.js?

Is there any way to inspect the message in the exception being thrown by the node.js script? It's not 100% necessary for this specific script, as the action is simple and it can only really fail for one reason. However, it would be good to know if there's a general model for inspecting an error code/message from a node.js script in a pipeline.

Erez Maadani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 5, 2023

Hey @Andy Rouse 

I'm saying that even if your Node.js fails, I believe you would still need to change your pipeline bash script. 

To exit your Node.js script with a return code which is not 0, try (in your Node.js code) catching the error and then calling: 

process.exit(1);

See here: https://nodejs.org/api/process.html#processexitcode 

Andy Rouse March 5, 2023

@Erez Maadani - Thank you for your help so far. Unfortunately - that's not working.

Whatever I'm doing (I've tried multiple approaches), the exit code is always 0. I've thrown an exception, caught the exception and then used process.exit, the result is still a zero exit code.

My latest version is as follows

Screenshot 2023-03-05 182739.png

Screenshot 2023-03-05 183026.png

Screenshot 2023-03-05 183231.png

Screenshot 2023-03-05 183334.png

I've tried multiple variations of the code, but whatever happens, I get a zero exit code. As you can see, I've attempted to call process.exit() within the logic in the first screenshot (I commented out the "throw" line), I've also tried making the process async and not async, in case that makes a difference. The differences tend to be that the custom thrown exception will or won't be shown in the output from the pipeline, but the exit code is always the same.

Erez Maadani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 5, 2023

@Andy Rouse 

As mentioned before, the command `echo $?` shows the exit code of the previous command, which is `echo exit code`...

Try this in your Node.js code:

if (dupeCustomLabels.size >0){
console.log(...)
process.exit(1)
}

And in your pipeline script:

node yourScript.js
rc=$?
echo $rc
[[ "$rc" -ne "0" ]] && exit(1) #this will fail the pipeline if rc is not zero  
Like Patrik S likes this
Andy Rouse March 7, 2023

Hey @Erez Maadani - I got this working. There were a couple of things wrong with my code, a few of which I was able to resolve with your help:

  • The "echo exit code" row was preventing me from capturing the exit code from the JS
  • I hadn't properly imported the process module, so my process.on() method wasn't firing properly

Once I'd resolved those two, I no longer had to use process.exit(), as throwing the exception within the process.on() method implicitly set the exit code to 1 (and I also see the exception method in the log)

The if statement in your response was helpful, didn't work for me. I think it had too many speech marks in it, as it caused an error at runtime. However, it did point me in the right direction and I was able to adapt it slightly to make it work for me:

Screenshot 2023-03-07 182650.png

With these in place, I was now able to throw an exception, so that the Pipeline will fail and the error message is available in the log for the pipeline. Thank you.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events