添加失败回调
This commit is contained in:
@@ -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,8 +142,11 @@ 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: |
|
||||||
|
set -o pipefail
|
||||||
|
{
|
||||||
for attempt in 1 2 3; do
|
for attempt in 1 2 3; do
|
||||||
echo "Starting docker push attempt ${attempt}/3 for ${IMAGE}"
|
echo "Starting docker push attempt ${attempt}/3 for ${IMAGE}"
|
||||||
|
|
||||||
@@ -157,6 +164,7 @@ jobs:
|
|||||||
|
|
||||||
echo "docker push failed after 3 attempts"
|
echo "docker push failed after 3 attempts"
|
||||||
exit 1
|
exit 1
|
||||||
|
} 2>&1 | tee push.log
|
||||||
|
|
||||||
- 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 请求体
|
||||||
|
if [ -n "$ERR_MESSAGE" ]; then
|
||||||
BODY=$(jq -n \
|
BODY=$(jq -n \
|
||||||
--arg jobId "$JOB_ID" \
|
--arg jobId "$JOB_ID" \
|
||||||
--arg imageUrl "$IMAGE" \
|
--arg imageUrl "$IMAGE" \
|
||||||
|
--arg status "$STATUS" \
|
||||||
|
--arg errMessage "$ERR_MESSAGE" \
|
||||||
--argjson startupParams "$STARTUP_PARAMS" \
|
--argjson startupParams "$STARTUP_PARAMS" \
|
||||||
'{jobId: $jobId, imageUrl: $imageUrl, startupParams: $startupParams}')
|
'{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"
|
||||||
|
|||||||
Reference in New Issue
Block a user