如何在 bash 完成代码构建后停止执行?

How can I stop the execution when the codebuild is completed in bash?

我正在创建一个在 AWS Codebuild 中启动构建的脚本。除了 运行ning 之外,我还希望在构建完成(到达 COMPLETED 阶段)并读取字符串“COMPLETED”时停止。

这是脚本:

#!/bin/bash
# Deploy API Gateway Authorizer

BUILD_PROJECT=
ENVIRONMENT=

if [ -z "$BUILD_PROJECT" ]
then
    echo "BUILD_PROJECT is empty, exiting...."
    exit 1
fi

if [ -z "$ENVIRONMENT" ]
then
    echo "ENVIRONMENT is empty, exiting...."
    exit 1
fi

function getStatus {
    id=$(aws codebuild list-builds-for-project --project-name "${BUILD_PROJECT}-${ENVIRONMENT}" | jq -r '.ids[0]')
    aws codebuild batch-get-builds --ids "$id" | jq '.builds[].phases[] | select (.phaseType=="COMPLETED") | .phaseType'
}

echo "----------------------------------------------------"
echo "Deploying: ${BUILD_PROJECT}-${ENVIRONMENT}"
echo "----------------------------------------------------"
aws codebuild start-build --project-name "${BUILD_PROJECT}-${ENVIRONMENT}"

while [ getStatus != "COMPLETED" ]
do
    echo "Deploying..."
    sleep 5
    getStatus
done 

我已经尝试使用 getStatus 函数,然后在 while 循环中直到函数的结果 != "COMPLETED" 但即使它在控制台输出中显示 COMPLETED , 它一直在执行。

这是我运行脚本时的控制台输出,例如:

----------------------------------------------------
Deploying: api-gateway-authorizer-us2-stage
----------------------------------------------------
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
Deploying...
"COMPLETED"
Deploying...
"COMPLETED"
Deploying...
"COMPLETED"
Deploying...
"COMPLETED"
Deploying...

您的 COMPLETE 实际上是 "COMPLETE",因为您没有在 jq 中使用 -r 标志。此外,您还必须将 getStatus 包装为 $(getStatus) 并在循环中使用 [[

#!/bin/bash
# Deploy API Gateway Authorizer

BUILD_PROJECT=
ENVIRONMENT=

if [ -z "$BUILD_PROJECT" ]
then
    echo "BUILD_PROJECT is empty, exiting...."
    exit 1
fi

if [ -z "$ENVIRONMENT" ]
then
    echo "ENVIRONMENT is empty, exiting...."
    exit 1
fi

function getStatus {
    id=$(aws codebuild list-builds-for-project --project-name "${BUILD_PROJECT}-${ENVIRONMENT}" | jq -r '.ids[0]')
    aws codebuild batch-get-builds --ids "$id" | jq -r '.builds[].phases[] | select (.phaseType=="COMPLETED") | .phaseType'
}

echo "----------------------------------------------------"
echo "Deploying: ${BUILD_PROJECT}-${ENVIRONMENT}"
echo "----------------------------------------------------"
aws codebuild start-build --project-name "${BUILD_PROJECT}-${ENVIRONMENT}"

while [[ $(getStatus) != "COMPLETED" ]]
do
    echo "Deploying..."
    sleep 5
    getStatus
done