添加失败回调

This commit is contained in:
dongmei
2026-06-11 19:00:53 +08:00
parent f4f2fdf0a7
commit c3de4b5b45

View File

@@ -110,6 +110,7 @@ jobs:
echo "镜像名称: ${IMAGE}" echo "镜像名称: ${IMAGE}"
- name: Check Dockerfile exists - name: Check Dockerfile exists
id: check-dockerfile
run: | run: |
cd target_repo cd target_repo
if [ ! -f "Dockerfile" ]; then if [ ! -f "Dockerfile" ]; then
@@ -119,6 +120,7 @@ jobs:
echo "Dockerfile 存在" echo "Dockerfile 存在"
- name: Login to Docker Registry - name: Login to Docker Registry
id: login-registry
if: github.event.inputs.dry_run != 'true' if: github.event.inputs.dry_run != 'true'
run: | run: |
echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" \ echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" \
@@ -127,10 +129,12 @@ jobs:
echo "Docker 登录成功" echo "Docker 登录成功"
- name: Build Docker Image - name: Build Docker Image
id: build-image
run: | run: |
cd target_repo cd target_repo
echo "开始构建镜像: ${IMAGE}" echo "开始构建镜像: ${IMAGE}"
docker build -t "${IMAGE}" . set -o pipefail
docker build -t "${IMAGE}" . 2>&1 | tee build.log
echo "镜像构建完成" echo "镜像构建完成"
- name: List built image - name: List built image
@@ -138,25 +142,29 @@ jobs:
docker images | grep "${IMAGE_NAME}" || echo "镜像列表查看完成" docker images | grep "${IMAGE_NAME}" || echo "镜像列表查看完成"
- name: Push Docker Image - name: Push Docker Image
id: push-image
if: github.event.inputs.dry_run != 'true' if: github.event.inputs.dry_run != 'true'
run: | run: |
for attempt in 1 2 3; do set -o pipefail
echo "Starting docker push attempt ${attempt}/3 for ${IMAGE}" {
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 failed after 3 attempts"
echo "docker push completed successfully" exit 1
exit 0 } 2>&1 | tee push.log
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
- name: Dry Run Summary - name: Dry Run Summary
if: github.event.inputs.dry_run == 'true' if: github.event.inputs.dry_run == 'true'
@@ -184,7 +192,7 @@ jobs:
echo "==========================================" echo "=========================================="
- name: Callback notification - name: Callback notification
if: github.event.inputs.callback_url != '' if: always() && github.event.inputs.callback_url != ''
run: | run: |
echo "正在发送回调通知..." echo "正在发送回调通知..."
@@ -194,12 +202,41 @@ jobs:
JOB_ID="${{ github.event.inputs.job_id }}" JOB_ID="${{ github.event.inputs.job_id }}"
STARTUP_PARAMS='${{ github.event.inputs.startup_params }}' 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 请求体 # 使用 jq 构造 JSON 请求体
BODY=$(jq -n \ if [ -n "$ERR_MESSAGE" ]; then
--arg jobId "$JOB_ID" \ BODY=$(jq -n \
--arg imageUrl "$IMAGE" \ --arg jobId "$JOB_ID" \
--argjson startupParams "$STARTUP_PARAMS" \ --arg imageUrl "$IMAGE" \
'{jobId: $jobId, imageUrl: $imageUrl, startupParams: $startupParams}') --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" echo "回调请求体: $BODY"
@@ -209,7 +246,13 @@ jobs:
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "X-Callback-Token: $CALLBACK_TOKEN" \ -H "X-Callback-Token: $CALLBACK_TOKEN" \
-d "$BODY" \ -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 if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then
echo "✅ 回调通知发送成功HTTP 状态码: $HTTP_CODE" echo "✅ 回调通知发送成功HTTP 状态码: $HTTP_CODE"