添加失败回调
This commit is contained in:
@@ -110,6 +110,7 @@ jobs:
|
||||
echo "镜像名称: ${IMAGE}"
|
||||
|
||||
- name: Check Dockerfile exists
|
||||
id: check-dockerfile
|
||||
run: |
|
||||
cd target_repo
|
||||
if [ ! -f "Dockerfile" ]; then
|
||||
@@ -119,6 +120,7 @@ jobs:
|
||||
echo "Dockerfile 存在"
|
||||
|
||||
- name: Login to Docker Registry
|
||||
id: login-registry
|
||||
if: github.event.inputs.dry_run != 'true'
|
||||
run: |
|
||||
echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" \
|
||||
@@ -127,10 +129,12 @@ jobs:
|
||||
echo "Docker 登录成功"
|
||||
|
||||
- name: Build Docker Image
|
||||
id: build-image
|
||||
run: |
|
||||
cd target_repo
|
||||
echo "开始构建镜像: ${IMAGE}"
|
||||
docker build -t "${IMAGE}" .
|
||||
set -o pipefail
|
||||
docker build -t "${IMAGE}" . 2>&1 | tee build.log
|
||||
echo "镜像构建完成"
|
||||
|
||||
- name: List built image
|
||||
@@ -138,25 +142,29 @@ jobs:
|
||||
docker images | grep "${IMAGE_NAME}" || echo "镜像列表查看完成"
|
||||
|
||||
- name: Push Docker Image
|
||||
id: push-image
|
||||
if: github.event.inputs.dry_run != 'true'
|
||||
run: |
|
||||
for attempt in 1 2 3; do
|
||||
echo "Starting docker push attempt ${attempt}/3 for ${IMAGE}"
|
||||
set -o pipefail
|
||||
{
|
||||
for attempt in 1 2 3; do
|
||||
echo "Starting docker push attempt ${attempt}/3 for ${IMAGE}"
|
||||
|
||||
if docker push "${IMAGE}"; then
|
||||
echo "docker push completed successfully"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "docker push failed on attempt ${attempt}/3"
|
||||
if [ $attempt -lt 3 ]; then
|
||||
echo "等待 30 秒后重试..."
|
||||
sleep 30
|
||||
fi
|
||||
done
|
||||
|
||||
if docker push "${IMAGE}"; then
|
||||
echo "docker push completed successfully"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "docker push failed on attempt ${attempt}/3"
|
||||
if [ $attempt -lt 3 ]; then
|
||||
echo "等待 30 秒后重试..."
|
||||
sleep 30
|
||||
fi
|
||||
done
|
||||
|
||||
echo "docker push failed after 3 attempts"
|
||||
exit 1
|
||||
echo "docker push failed after 3 attempts"
|
||||
exit 1
|
||||
} 2>&1 | tee push.log
|
||||
|
||||
- name: Dry Run Summary
|
||||
if: github.event.inputs.dry_run == 'true'
|
||||
@@ -184,7 +192,7 @@ jobs:
|
||||
echo "=========================================="
|
||||
|
||||
- name: Callback notification
|
||||
if: github.event.inputs.callback_url != ''
|
||||
if: always() && github.event.inputs.callback_url != ''
|
||||
run: |
|
||||
echo "正在发送回调通知..."
|
||||
|
||||
@@ -194,12 +202,41 @@ jobs:
|
||||
JOB_ID="${{ github.event.inputs.job_id }}"
|
||||
STARTUP_PARAMS='${{ github.event.inputs.startup_params }}'
|
||||
|
||||
# 判断状态与错误信息
|
||||
STATUS="SUCCESS"
|
||||
ERR_MESSAGE=""
|
||||
|
||||
if [ "${{ steps.check-dockerfile.outcome }}" == "failure" ]; then
|
||||
STATUS="FAILED"
|
||||
ERR_MESSAGE="仓库根目录下未找到 Dockerfile"
|
||||
elif [ "${{ steps.login-registry.outcome }}" == "failure" ]; then
|
||||
STATUS="FAILED"
|
||||
ERR_MESSAGE="Docker 镜像仓库登录失败"
|
||||
elif [ "${{ steps.build-image.outcome }}" == "failure" ]; then
|
||||
STATUS="FAILED"
|
||||
ERR_MESSAGE=$(tail -n 20 build.log 2>/dev/null || echo "Docker 镜像构建失败")
|
||||
elif [ "${{ steps.push-image.outcome }}" == "failure" ]; then
|
||||
STATUS="FAILED"
|
||||
ERR_MESSAGE=$(tail -n 20 push.log 2>/dev/null || echo "Docker 镜像推送失败")
|
||||
fi
|
||||
|
||||
# 使用 jq 构造 JSON 请求体
|
||||
BODY=$(jq -n \
|
||||
--arg jobId "$JOB_ID" \
|
||||
--arg imageUrl "$IMAGE" \
|
||||
--argjson startupParams "$STARTUP_PARAMS" \
|
||||
'{jobId: $jobId, imageUrl: $imageUrl, startupParams: $startupParams}')
|
||||
if [ -n "$ERR_MESSAGE" ]; then
|
||||
BODY=$(jq -n \
|
||||
--arg jobId "$JOB_ID" \
|
||||
--arg imageUrl "$IMAGE" \
|
||||
--arg status "$STATUS" \
|
||||
--arg errMessage "$ERR_MESSAGE" \
|
||||
--argjson startupParams "$STARTUP_PARAMS" \
|
||||
'{jobId: $jobId, imageUrl: $imageUrl, status: $status, errMessage: $errMessage, startupParams: $startupParams}')
|
||||
else
|
||||
BODY=$(jq -n \
|
||||
--arg jobId "$JOB_ID" \
|
||||
--arg imageUrl "$IMAGE" \
|
||||
--arg status "$STATUS" \
|
||||
--argjson startupParams "$STARTUP_PARAMS" \
|
||||
'{jobId: $jobId, imageUrl: $imageUrl, status: $status, startupParams: $startupParams}')
|
||||
fi
|
||||
|
||||
echo "回调请求体: $BODY"
|
||||
|
||||
@@ -209,7 +246,13 @@ jobs:
|
||||
-H "Content-Type: application/json" \
|
||||
-H "X-Callback-Token: $CALLBACK_TOKEN" \
|
||||
-d "$BODY" \
|
||||
"$CALLBACK_URL")
|
||||
"$CALLBACK_URL" || true)
|
||||
|
||||
# 去除空白并校验 HTTP_CODE 是否为数字
|
||||
HTTP_CODE=$(echo "$HTTP_CODE" | tr -d '[:space:]')
|
||||
if ! [[ "$HTTP_CODE" =~ ^[0-9]+$ ]]; then
|
||||
HTTP_CODE="000"
|
||||
fi
|
||||
|
||||
if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then
|
||||
echo "✅ 回调通知发送成功,HTTP 状态码: $HTTP_CODE"
|
||||
|
||||
Reference in New Issue
Block a user