在GCP中,当子构建失败时,如何使父云构建失败?

时间:2020-09-07 14:45:19

标签: google-cloud-build

我已经设置了monorepo,并且在存储库的根目录中有一个cloudbuild.yaml文件,第一步是剥离了子云构建作业:

# Trigger builds for all packages in the repository.
  - name: "gcr.io/cloud-builders/gcloud"
    entrypoint: "bash"
    args: [
        "./scripts/cloudbuild/build-all.sh",
        # Child builds don't have the git context, so pass them the SHORT_SHA.
        "--substitutions=_TAG=$SHORT_SHA",
      ]
    timeout: 1200s # 20 minutes

全部构建脚本是我从社区构建者存储库中复制的:

#!/usr/bin/env bash
DIR_NAME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

set -e # Sets mode to exit on an error, without executing the remaining commands.

for d in {packages,ops/helm,ops/pulumi}/*/; do
    config="${d}cloudbuild.yaml"
    if [[ ! -f "${config}" ]]; then
        continue
    fi

    echo "Building $d ... "
    (
        gcloud builds submit . --config=${config} $*
    ) &
done
wait

等待直到所有子代构建完成,然后再继续下一个...方便!

唯一的问题是,如果任何子代构建失败,它将继续进行下一步。

如果任何子代构建失败,是否有办法使此步骤失败?我猜我的脚本没有返回正确的错误代码...?

1 个答案:

答案 0 :(得分:0)

如果执行的任何命令具有error,则set -e标志应使脚本退出,但是您也可以使用$?检查命令的输出。变量,例如,您可以包括以下几行:

echo "Building $d ... "
(
    gcloud builds submit . --config=${config} $*
    if [ $? == 1 ]; then #Check the status of the last command
       echo "There was an error while building $d, exiting"
       exit 1
    fi
) &

因此,如果发生错误,脚本将退出并显示状态1(错误)