fix(cicd): 修复 CI/CD 错误上报数据不全和 URL 跳转错误
Some checks failed
Build and Deploy Log Center / build-and-deploy (push) Failing after 1m11s

- 构建步骤改用 docker buildx build + tee 捕获实际构建日志
- URL 改用 github.run_number 替代 github.run_id(修复跳转到错误页面)
- 改为单一综合失败上报步骤,收集实际错误日志到 stack_trace
- 补充 repo_url、actor、commit 等缺失字段
- 使用 ${{ }} 模板语法替代 $GITHUB_* 环境变量

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
zyc 2026-02-26 10:15:58 +08:00
parent c8204b6d47
commit cd22a5696b

View File

@ -6,9 +6,6 @@ on:
- main - main
- master - master
env:
LOG_CENTER_URL: https://qiyuan-log-center-api.airlabs.art
jobs: jobs:
build-and-deploy: build-and-deploy:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -33,37 +30,39 @@ jobs:
# Build API Image # Build API Image
- name: Build and Push API - name: Build and Push API
id: build-api id: build-api
uses: docker/build-push-action@v4 run: |
with: set -o pipefail
context: . docker buildx build \
file: ./Dockerfile --push \
push: true --provenance=false \
provenance: false --tag ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-api:latest \
tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-api:latest -f ./Dockerfile \
. 2>&1 | tee /tmp/build-api.log
# Build Web Image # Build Web Image
- name: Build and Push Web - name: Build and Push Web
id: build-web id: build-web
uses: docker/build-push-action@v4 run: |
with: set -o pipefail
context: ./web docker buildx build \
file: ./web/Dockerfile --push \
push: true --provenance=false \
provenance: false --build-arg VITE_API_BASE_URL=https://qiyuan-log-center-api.airlabs.art \
tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-web:latest --tag ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-web:latest \
build-args: | -f ./web/Dockerfile \
VITE_API_BASE_URL=https://qiyuan-log-center-api.airlabs.art ./web 2>&1 | tee /tmp/build-web.log
# Build K8s Monitor Image # Build K8s Monitor Image
- name: Build and Push K8s Monitor - name: Build and Push K8s Monitor
id: build-monitor id: build-monitor
uses: docker/build-push-action@v4 run: |
with: set -o pipefail
context: ./k8s-monitor docker buildx build \
file: ./k8s-monitor/Dockerfile --push \
push: true --provenance=false \
provenance: false --tag ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/k8s-pod-monitor:latest \
tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/k8s-pod-monitor:latest -f ./k8s-monitor/Dockerfile \
./k8s-monitor 2>&1 | tee /tmp/build-monitor.log
- name: Setup Kubectl - name: Setup Kubectl
run: | run: |
@ -87,132 +86,95 @@ jobs:
sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-web:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-web:latest|g" k8s/web-deployment-prod.yaml sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-web:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-web:latest|g" k8s/web-deployment-prod.yaml
sed -i "s|\${CI_REGISTRY_IMAGE}/k8s-pod-monitor:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/k8s-pod-monitor:latest|g" k8s/monitor-cronjob.yaml sed -i "s|\${CI_REGISTRY_IMAGE}/k8s-pod-monitor:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/k8s-pod-monitor:latest|g" k8s/monitor-cronjob.yaml
# Apply configurations # Apply configurations and capture output
kubectl apply -f k8s/api-deployment-prod.yaml set -o pipefail
kubectl apply -f k8s/web-deployment-prod.yaml {
kubectl apply -f k8s/ingress.yaml kubectl apply -f k8s/api-deployment-prod.yaml
kubectl apply -f k8s/monitor-cronjob.yaml kubectl apply -f k8s/web-deployment-prod.yaml
kubectl apply -f k8s/ingress.yaml
kubectl apply -f k8s/monitor-cronjob.yaml
# Restart deployments # Restart deployments
kubectl rollout restart deployment/log-center-api kubectl rollout restart deployment/log-center-api
kubectl rollout restart deployment/log-center-web kubectl rollout restart deployment/log-center-web
} 2>&1 | tee /tmp/deploy.log
# ==================== CI/CD 错误上报 ==================== # ==================== CI/CD 错误上报 ====================
- name: Report API Build Failure - name: Report failure to Log Center
if: failure() && steps.build-api.outcome == 'failure' if: failure()
run: | run: |
curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \ # 收集各阶段日志(取最后 50 行)
-H "Content-Type: application/json" \ BUILD_API_LOG=""
-d '{ BUILD_WEB_LOG=""
"project_id": "log_center_api", BUILD_MONITOR_LOG=""
"environment": "cicd", DEPLOY_LOG=""
"level": "ERROR", FAILED_STEP="unknown"
"source": "cicd", PROJECT_ID="log_center_api"
"commit_hash": "'"$GITHUB_SHA"'",
"error": {
"type": "DockerBuildError",
"message": "Log Center API Docker build failed",
"file_path": null,
"line_number": null,
"stack_trace": ["API Docker build step failed. Check CI logs for details."]
},
"context": {
"workflow_name": "'"$GITHUB_WORKFLOW"'",
"job_name": "'"$GITHUB_JOB"'",
"step_name": "Build and Push API",
"run_id": "'"$GITHUB_RUN_ID"'",
"branch": "'"$GITHUB_REF_NAME"'",
"repository": "'"$GITHUB_REPOSITORY"'",
"run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'"
}
}' --connect-timeout 5 --max-time 10 || true
- name: Report Web Build Failure if [[ "${{ steps.build-api.outcome }}" == "failure" ]]; then
if: failure() && steps.build-web.outcome == 'failure' FAILED_STEP="build-api"
run: | PROJECT_ID="log_center_api"
curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \ if [ -f /tmp/build-api.log ]; then
-H "Content-Type: application/json" \ BUILD_API_LOG=$(tail -50 /tmp/build-api.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
-d '{ fi
"project_id": "log_center_web", elif [[ "${{ steps.build-web.outcome }}" == "failure" ]]; then
"environment": "cicd", FAILED_STEP="build-web"
"level": "ERROR", PROJECT_ID="log_center_web"
"source": "cicd", if [ -f /tmp/build-web.log ]; then
"commit_hash": "'"$GITHUB_SHA"'", BUILD_WEB_LOG=$(tail -50 /tmp/build-web.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
"error": { fi
"type": "DockerBuildError", elif [[ "${{ steps.build-monitor.outcome }}" == "failure" ]]; then
"message": "Log Center Web Docker build failed", FAILED_STEP="build-monitor"
"file_path": null, PROJECT_ID="log_center_api"
"line_number": null, if [ -f /tmp/build-monitor.log ]; then
"stack_trace": ["Web Docker build step failed. Check CI logs for details."] BUILD_MONITOR_LOG=$(tail -50 /tmp/build-monitor.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
}, fi
"context": { elif [[ "${{ steps.deploy.outcome }}" == "failure" ]]; then
"workflow_name": "'"$GITHUB_WORKFLOW"'", FAILED_STEP="deploy"
"job_name": "'"$GITHUB_JOB"'", PROJECT_ID="log_center_api"
"step_name": "Build and Push Web", if [ -f /tmp/deploy.log ]; then
"run_id": "'"$GITHUB_RUN_ID"'", DEPLOY_LOG=$(tail -50 /tmp/deploy.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
"branch": "'"$GITHUB_REF_NAME"'", fi
"repository": "'"$GITHUB_REPOSITORY"'", fi
"run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'"
}
}' --connect-timeout 5 --max-time 10 || true
- name: Report Monitor Build Failure # 合并错误日志
if: failure() && steps.build-monitor.outcome == 'failure' ERROR_LOG="${BUILD_API_LOG}${BUILD_WEB_LOG}${BUILD_MONITOR_LOG}${DEPLOY_LOG}"
run: | if [ -z "$ERROR_LOG" ]; then
curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \ ERROR_LOG="No captured output. Check Gitea Actions UI for details."
-H "Content-Type: application/json" \ fi
-d '{
"project_id": "log_center_api",
"environment": "cicd",
"level": "ERROR",
"source": "cicd",
"commit_hash": "'"$GITHUB_SHA"'",
"error": {
"type": "DockerBuildError",
"message": "K8s Monitor Docker build failed",
"file_path": null,
"line_number": null,
"stack_trace": ["K8s Monitor Docker build step failed. Check CI logs for details."]
},
"context": {
"workflow_name": "'"$GITHUB_WORKFLOW"'",
"job_name": "'"$GITHUB_JOB"'",
"step_name": "Build and Push K8s Monitor",
"run_id": "'"$GITHUB_RUN_ID"'",
"branch": "'"$GITHUB_REF_NAME"'",
"repository": "'"$GITHUB_REPOSITORY"'",
"run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'"
}
}' --connect-timeout 5 --max-time 10 || true
- name: Report Deploy Failure # 判断 source
if: failure() && steps.deploy.outcome == 'failure' if [[ "$FAILED_STEP" == "deploy" ]]; then
run: | SOURCE="deployment"
curl -s -X POST "${LOG_CENTER_URL}/api/v1/logs/report" \ ERROR_TYPE="DeployError"
else
SOURCE="cicd"
ERROR_TYPE="DockerBuildError"
fi
curl -s -X POST "https://qiyuan-log-center-api.airlabs.art/api/v1/logs/report" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d '{ -d "{
"project_id": "log_center_api", \"project_id\": \"${PROJECT_ID}\",
"environment": "cicd", \"environment\": \"${{ github.ref_name }}\",
"level": "ERROR", \"level\": \"ERROR\",
"source": "deployment", \"source\": \"${SOURCE}\",
"commit_hash": "'"$GITHUB_SHA"'", \"commit_hash\": \"${{ github.sha }}\",
"error": { \"repo_url\": \"https://gitea.airlabs.art/zyc/log-center.git\",
"type": "DeployError", \"error\": {
"message": "Log Center K8s deployment failed", \"type\": \"${ERROR_TYPE}\",
"file_path": null, \"message\": \"[${FAILED_STEP}] Build and Deploy Log Center failed on branch ${{ github.ref_name }}\",
"line_number": null, \"stack_trace\": [\"${ERROR_LOG}\"]
"stack_trace": ["K8s deployment step failed. Check CI logs for details."]
}, },
"context": { \"context\": {
"workflow_name": "'"$GITHUB_WORKFLOW"'", \"job_name\": \"build-and-deploy\",
"job_name": "'"$GITHUB_JOB"'", \"step_name\": \"${FAILED_STEP}\",
"step_name": "Update K8s Manifests", \"workflow\": \"${{ github.workflow }}\",
"run_id": "'"$GITHUB_RUN_ID"'", \"run_id\": \"${{ github.run_number }}\",
"branch": "'"$GITHUB_REF_NAME"'", \"branch\": \"${{ github.ref_name }}\",
"repository": "'"$GITHUB_REPOSITORY"'", \"actor\": \"${{ github.actor }}\",
"namespace": "default", \"commit\": \"${{ github.sha }}\",
"deployment_name": "log-center", \"run_url\": \"https://gitea.airlabs.art/${{ github.repository }}/actions/runs/${{ github.run_number }}\"
"run_url": "'"$GITHUB_SERVER_URL"'/'"$GITHUB_REPOSITORY"'/actions/runs/'"$GITHUB_RUN_ID"'"
} }
}' --connect-timeout 5 --max-time 10 || true }" || true