This is a part of my Bitbucket pipeline:
npm_audit: &npm_audit
echo "Running npm audit..."
npm_audit_output=$(npm audit --audit-level=critical)
if [ $? -ne 0 ]; then
echo "Critical vulnerabilities found. They must be fixed manually:"
exit 1
fi
npm_audit_output=$(npm audit)
if [ $? -ne 0 ]; then
echo "Non-critical vulnerabilities found."
echo "$npm_audit_output"
echo "Attempting to fix..."
npm audit fix
else
echo "No vulnerabilities found."
fi
later called like:
- step:
...
script:
- *npm_audit
BB returns:
+ echo "Running npm audit..." npm_audit_output=$(npm audit --audit-level=critical) if [ $? -ne 0 ]; then echo "Critical vulnerabilities found. They must be fixed manually:" exit 1 fi npm_audit_output=$(npm audit) if [ $? -ne 0 ]; then echo "Non-critical vulnerabilities found." echo "$npm_audit_output" echo "Attempting to fix..." npm audit fix else echo "No vulnerabilities found." fi
bash: bashScript5261110360543614450.sh: line 5: syntax error near unexpected token `then'
Why is there a syntax error? ShellCheck is green
And why is the whole script echoed instead of just strings I asked for?
KamilCuk's comment sent me in the right direction. The script was interpreted as a single line and that caused syntex errors. I changed the anchor line to:
npm_audit: &npm_audit |
which made the lines below to be interpreted properly.