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:
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.
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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.