name: Build and Deploy LTY on: push: branches: - main - master jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 with: config-inline: | [registry."docker.io"] mirrors = ["https://docker.m.daocloud.io", "https://docker.1panel.live", "https://hub.rat.dev"] - name: Login to Huawei Cloud SWR uses: docker/login-action@v2 with: registry: ${{ secrets.SWR_SERVER }} username: ${{ secrets.SWR_USERNAME }} password: ${{ secrets.SWR_PASSWORD }} - name: Build and Push Backend id: build-backend run: | set -o pipefail docker buildx build \ --push \ --provenance=false \ --tag ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/lty-backend:latest \ ./qy_lty 2>&1 | tee /tmp/build.log - name: Build and Push Admin Frontend id: build-admin run: | set -o pipefail docker buildx build \ --push \ --provenance=false \ --tag ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/lty-admin:latest \ ./qy-lty-admin 2>&1 | tee -a /tmp/build.log - name: Setup Kubectl run: | curl -LO "https://dl.k8s.io/release/v1.28.2/bin/linux/amd64/kubectl" || \ curl -LO "https://cdn.dl.k8s.io/release/v1.28.2/bin/linux/amd64/kubectl" chmod +x kubectl mv kubectl /usr/local/bin/ - name: Deploy to K3s uses: Azure/k8s-set-context@v3 with: method: kubeconfig kubeconfig: ${{ secrets.KUBE_CONFIG }} - name: Update K8s Manifests id: deploy run: | # Replace image placeholders sed -i "s|\${CI_REGISTRY_IMAGE}/lty-backend:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/lty-backend:latest|g" k8s/backend-deployment-prod.yaml sed -i "s|\${CI_REGISTRY_IMAGE}/lty-admin:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/lty-admin:latest|g" k8s/admin-deployment-prod.yaml # Apply manifests and restart set -o pipefail { kubectl apply -f k8s/backend-deployment-prod.yaml kubectl apply -f k8s/admin-deployment-prod.yaml kubectl apply -f k8s/ingress.yaml kubectl rollout restart deployment/lty-backend kubectl rollout restart deployment/lty-admin } 2>&1 | tee /tmp/deploy.log - name: Report failure to Log Center if: failure() run: | BUILD_LOG="" DEPLOY_LOG="" FAILED_STEP="unknown" if [ -f /tmp/build.log ]; then BUILD_LOG=$(tail -50 /tmp/build.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g') FAILED_STEP="build" fi if [ -f /tmp/deploy.log ]; then DEPLOY_LOG=$(tail -50 /tmp/deploy.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g') if [ -n "$DEPLOY_LOG" ]; then FAILED_STEP="deploy" fi fi if [ -z "$BUILD_LOG" ] && [ -z "$DEPLOY_LOG" ]; then BUILD_LOG="No captured output. Check Gitea Actions UI for details." FAILED_STEP="pre-build" fi ERROR_LOG="${BUILD_LOG}${DEPLOY_LOG}" curl -s -X POST "https://qiyuan-log-center-api.airlabs.art/api/v1/logs/report" \ -H "Content-Type: application/json" \ -d "{ \"project_id\": \"lty\", \"environment\": \"${{ github.ref_name }}\", \"level\": \"ERROR\", \"source\": \"cicd\", \"commit_hash\": \"${{ github.sha }}\", \"repo_url\": \"https://gitea.airlabs.art/zyc/lty.git\", \"error\": { \"type\": \"CICDFailure\", \"message\": \"[${FAILED_STEP}] Build and Deploy 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_id }}\", \"branch\": \"${{ github.ref_name }}\", \"actor\": \"${{ github.actor }}\", \"commit\": \"${{ github.sha }}\" } }" || true