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