All checks were successful
Build and Deploy Log Center / build-and-deploy (push) Successful in 5s
Each build now tags images with both commit hash and latest, enabling precise rollback with kubectl set image. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
203 lines
9.0 KiB
YAML
203 lines
9.0 KiB
YAML
name: Build and Deploy Log Center
|
||
|
||
on:
|
||
push:
|
||
branches:
|
||
- master
|
||
- dev
|
||
|
||
jobs:
|
||
build-and-deploy:
|
||
runs-on: ubuntu-latest
|
||
steps:
|
||
- name: Checkout
|
||
run: |
|
||
git clone --depth=1 --branch=${{ github.ref_name }} https://gitea.airlabs.art/${{ github.repository }}.git .
|
||
|
||
- name: Set environment by branch
|
||
run: |
|
||
# 版本标签:commit 短哈希
|
||
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
||
echo "IMAGE_TAG=${SHORT_SHA}" >> $GITHUB_ENV
|
||
|
||
if [[ "${{ github.ref_name }}" == "master" ]]; then
|
||
echo "CR_ORG=prod" >> $GITHUB_ENV
|
||
echo "DEPLOY_ENV=production" >> $GITHUB_ENV
|
||
echo "DOMAIN_API=qiyuan-log-center-api.airlabs.art" >> $GITHUB_ENV
|
||
echo "DOMAIN_WEB=qiyuan-log-center-web.airlabs.art" >> $GITHUB_ENV
|
||
elif [[ "${{ github.ref_name }}" == "dev" ]]; then
|
||
echo "CR_ORG=dev" >> $GITHUB_ENV
|
||
echo "DEPLOY_ENV=development" >> $GITHUB_ENV
|
||
echo "DOMAIN_API=qiyuan-log-center-api.test.airlabs.art" >> $GITHUB_ENV
|
||
echo "DOMAIN_WEB=qiyuan-log-center-web.test.airlabs.art" >> $GITHUB_ENV
|
||
fi
|
||
|
||
- name: Login to Volcano Engine CR
|
||
run: |
|
||
echo "${{ secrets.CR_PASSWORD }}" | docker login --username "${{ secrets.CR_USERNAME }}" --password-stdin ${{ secrets.CR_SERVER }}
|
||
|
||
# Build API Image
|
||
- name: Build and Push API
|
||
id: build-api
|
||
run: |
|
||
set -o pipefail
|
||
DOCKER_BUILDKIT=0 docker build \
|
||
--tag ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-api:${{ env.IMAGE_TAG }} \
|
||
--tag ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-api:latest \
|
||
-f ./Dockerfile \
|
||
. 2>&1 | tee /tmp/build-api.log
|
||
docker push ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-api:${{ env.IMAGE_TAG }} 2>&1 | tee -a /tmp/build-api.log
|
||
docker push ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-api:latest 2>&1 | tee -a /tmp/build-api.log
|
||
|
||
# Build Web Image
|
||
- name: Build and Push Web
|
||
id: build-web
|
||
run: |
|
||
set -o pipefail
|
||
DOCKER_BUILDKIT=0 docker build \
|
||
--build-arg VITE_API_BASE_URL=https://${{ env.DOMAIN_API }} \
|
||
--tag ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-web:${{ env.IMAGE_TAG }} \
|
||
--tag ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-web:latest \
|
||
-f ./web/Dockerfile \
|
||
./web 2>&1 | tee /tmp/build-web.log
|
||
docker push ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-web:${{ env.IMAGE_TAG }} 2>&1 | tee -a /tmp/build-web.log
|
||
docker push ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-web:latest 2>&1 | tee -a /tmp/build-web.log
|
||
|
||
# Build K8s Monitor Image
|
||
- name: Build and Push K8s Monitor
|
||
id: build-monitor
|
||
run: |
|
||
set -o pipefail
|
||
DOCKER_BUILDKIT=0 docker build \
|
||
--tag ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/k8s-pod-monitor:${{ env.IMAGE_TAG }} \
|
||
--tag ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/k8s-pod-monitor:latest \
|
||
-f ./k8s-monitor/Dockerfile \
|
||
./k8s-monitor 2>&1 | tee /tmp/build-monitor.log
|
||
docker push ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/k8s-pod-monitor:${{ env.IMAGE_TAG }} 2>&1 | tee -a /tmp/build-monitor.log
|
||
docker push ${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/k8s-pod-monitor:latest 2>&1 | tee -a /tmp/build-monitor.log
|
||
|
||
- name: Setup Kubectl
|
||
run: kubectl version --client
|
||
|
||
- name: Set kubeconfig
|
||
run: |
|
||
mkdir -p $HOME/.kube
|
||
if [[ "${{ github.ref_name }}" == "master" ]]; then
|
||
echo "${{ secrets.VOLCANO_PROD_KUBE_CONFIG }}" > $HOME/.kube/config
|
||
elif [[ "${{ github.ref_name }}" == "dev" ]]; then
|
||
echo "${{ secrets.VOLCANO_TEST_KUBE_CONFIG }}" > $HOME/.kube/config
|
||
fi
|
||
chmod 600 $HOME/.kube/config
|
||
|
||
- name: Deploy to K3s
|
||
id: deploy
|
||
run: |
|
||
echo "Environment: ${{ env.DEPLOY_ENV }}"
|
||
echo "API Domain: ${{ env.DOMAIN_API }}"
|
||
echo "Web Domain: ${{ env.DOMAIN_WEB }}"
|
||
|
||
# Replace image placeholders with versioned tag
|
||
sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-api:latest|${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-api:${{ env.IMAGE_TAG }}|g" k8s/api-deployment-prod.yaml
|
||
sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-web:latest|${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/log-center-web:${{ env.IMAGE_TAG }}|g" k8s/web-deployment-prod.yaml
|
||
sed -i "s|\${CI_REGISTRY_IMAGE}/k8s-pod-monitor:latest|${{ secrets.CR_SERVER }}/${{ env.CR_ORG }}/k8s-pod-monitor:${{ env.IMAGE_TAG }}|g" k8s/monitor-cronjob.yaml
|
||
|
||
# Replace domain placeholders in ingress
|
||
sed -i "s|qiyuan-log-center-api.airlabs.art|${{ env.DOMAIN_API }}|g" k8s/ingress.yaml
|
||
sed -i "s|qiyuan-log-center-web.airlabs.art|${{ env.DOMAIN_WEB }}|g" k8s/ingress.yaml
|
||
|
||
# Replace LOG_CENTER_URL in monitor
|
||
sed -i "s|https://qiyuan-log-center-api.airlabs.art|https://${{ env.DOMAIN_API }}|g" 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
|
||
} 2>&1 | tee /tmp/deploy.log
|
||
|
||
# ==================== CI/CD 错误上报 ====================
|
||
|
||
- name: Report failure to Log Center
|
||
if: failure()
|
||
run: |
|
||
# 收集各阶段日志(取最后 50 行)
|
||
BUILD_API_LOG=""
|
||
BUILD_WEB_LOG=""
|
||
BUILD_MONITOR_LOG=""
|
||
DEPLOY_LOG=""
|
||
FAILED_STEP="unknown"
|
||
PROJECT_ID="log_center_api"
|
||
|
||
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
|
||
|
||
# 合并错误日志
|
||
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
|
||
|
||
# 判断 source
|
||
if [[ "$FAILED_STEP" == "deploy" ]]; then
|
||
SOURCE="deployment"
|
||
ERROR_TYPE="DeployError"
|
||
else
|
||
SOURCE="cicd"
|
||
ERROR_TYPE="DockerBuildError"
|
||
fi
|
||
|
||
curl -s -X POST "https://${{ env.DOMAIN_API }}/api/v1/logs/report" \
|
||
-H "Content-Type: application/json" \
|
||
-d "{
|
||
\"project_id\": \"${PROJECT_ID}\",
|
||
\"environment\": \"${{ env.DEPLOY_ENV }}\",
|
||
\"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\": {
|
||
\"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 }}\"
|
||
}
|
||
}" || true
|