Bitbucket Pipelines 的 YAML 锚中内联的 Bash 脚本中存在语法错误

问题描述 投票:0回答:1

这是我的 Bitbucket 管道的一部分:

  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

后来这样称呼:

  - step:
      ...
      script:
        - *npm_audit

BB回归:

+ 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'

为什么会出现语法错误? ShellCheck 为绿色

为什么会回显整个脚本而不是我要求的字符串?

bash sh bitbucket bitbucket-pipelines
1个回答
1
投票

KamilCuk 的评论给我指明了正确的方向。该脚本被解释为一行并导致语法错误。我将锚线更改为:

npm_audit: &npm_audit |

这使得下面的行能够被正确解释

© www.soinside.com 2019 - 2024. All rights reserved.