name: Build and Deploy Web 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: | SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7) BUILD_DATE=$(date +%Y%m%d) if [[ "${{ github.ref_name }}" == "master" ]]; then echo "IMAGE_TAG=prod-${BUILD_DATE}-${SHORT_SHA}" >> $GITHUB_ENV echo "CR_SERVER_ACTIVE=gitea-prod-cn-shanghai.cr.volces.com" >> $GITHUB_ENV echo "CR_USERNAME_ACTIVE=seaislee@76339115" >> $GITHUB_ENV echo "CR_PASSWORD_ACTIVE=${{ secrets.CR_PROD_PASSWORD }}" >> $GITHUB_ENV echo "CR_ORG=prod" >> $GITHUB_ENV echo "DEPLOY_ENV=production" >> $GITHUB_ENV elif [[ "${{ github.ref_name }}" == "dev" ]]; then echo "IMAGE_TAG=dev-${BUILD_DATE}-${SHORT_SHA}" >> $GITHUB_ENV echo "CR_SERVER_ACTIVE=${{ secrets.CR_SERVER }}" >> $GITHUB_ENV echo "CR_USERNAME_ACTIVE=${{ secrets.CR_USERNAME }}" >> $GITHUB_ENV echo "CR_PASSWORD_ACTIVE=${{ secrets.CR_PASSWORD }}" >> $GITHUB_ENV echo "CR_ORG=dev" >> $GITHUB_ENV echo "DEPLOY_ENV=development" >> $GITHUB_ENV fi - name: Login to Container Registry run: | echo "${{ env.CR_PASSWORD_ACTIVE }}" | docker login --username "${{ env.CR_USERNAME_ACTIVE }}" --password-stdin ${{ env.CR_SERVER_ACTIVE }} - name: Build and Push Web id: build_web run: | set -o pipefail for attempt in 1 2 3; do echo "Build web attempt $attempt/3..." DOCKER_BUILDKIT=0 docker build \ --tag ${{ env.CR_SERVER_ACTIVE }}/${{ env.CR_ORG }}/airlabs-manage-web:${{ env.IMAGE_TAG }} \ --tag ${{ env.CR_SERVER_ACTIVE }}/${{ env.CR_ORG }}/airlabs-manage-web:latest \ ./frontend 2>&1 | tee /tmp/build.log && break echo "Attempt $attempt failed, retrying in 10s..." && sleep 10 done for attempt in 1 2 3; do docker push ${{ env.CR_SERVER_ACTIVE }}/${{ env.CR_ORG }}/airlabs-manage-web:${{ env.IMAGE_TAG }} && \ docker push ${{ env.CR_SERVER_ACTIVE }}/${{ env.CR_ORG }}/airlabs-manage-web:latest && break echo "Push attempt $attempt failed, retrying in 10s..." && sleep 10 done - name: Setup Kubectl run: | if ! command -v kubectl &>/dev/null; then for attempt in 1 2 3; do curl -LO "https://files.m.daocloud.io/dl.k8s.io/release/v1.28.0/bin/linux/amd64/kubectl" && break echo "Download attempt $attempt failed, retrying in 5s..." && sleep 5 done chmod +x kubectl && mv kubectl /usr/bin/kubectl fi kubectl version --client - name: Set kubeconfig run: | mkdir -p $HOME/.kube if [[ "${{ github.ref_name }}" == "master" ]]; then printf '%s\n' '${{ secrets.VOLCANO_PROD_KUBE_CONFIG }}' > $HOME/.kube/config elif [[ "${{ github.ref_name }}" == "dev" ]]; then printf '%s\n' '${{ secrets.VOLCANO_TEST_KUBE_CONFIG }}' > $HOME/.kube/config fi chmod 600 $HOME/.kube/config echo "kubeconfig lines: $(wc -l < $HOME/.kube/config)" grep server $HOME/.kube/config || echo "WARNING: no server found in kubeconfig" - name: Deploy to K3s id: deploy run: | echo "Environment: ${{ env.DEPLOY_ENV }}" CR_IMAGE="${{ env.CR_SERVER_ACTIVE }}/${{ env.CR_ORG }}" # Replace image placeholders sed -i "s|\${CI_REGISTRY_IMAGE}/web:latest|${CR_IMAGE}/airlabs-manage-web:${{ env.IMAGE_TAG }}|g" k8s/web-deployment-prod.yaml for attempt in 1 2 3; do echo "Deploy attempt $attempt/3..." { # Create/update image pull secret kubectl create secret docker-registry cr-pull-secret \ --docker-server="${{ env.CR_SERVER_ACTIVE }}" \ --docker-username="${{ env.CR_USERNAME_ACTIVE }}" \ --docker-password="${{ env.CR_PASSWORD_ACTIVE }}" \ --dry-run=client -o yaml | kubectl apply -f - kubectl apply -f k8s/cert-manager-issuer.yaml kubectl apply -f k8s/web-deployment-prod.yaml kubectl apply -f k8s/web-ingress.yaml kubectl rollout restart deployment/airlabs-manage-web } 2>&1 | tee /tmp/deploy.log && break echo "Attempt $attempt failed, retrying in 10s..." sleep 10 done - name: Report failure to Log Center if: failure() run: | BUILD_LOG="" DEPLOY_LOG="" FAILED_STEP="unknown" if [[ "${{ steps.build_web.outcome }}" == "failure" ]]; then FAILED_STEP="build" if [ -f /tmp/build.log ]; then BUILD_LOG=$(tail -50 /tmp/build.log | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g') fi elif [[ "${{ steps.deploy.outcome }}" == "failure" ]]; then FAILED_STEP="deploy" 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_LOG}${DEPLOY_LOG}" if [ -z "$ERROR_LOG" ]; then ERROR_LOG="No captured output. Check Gitea Actions UI for details." fi 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\": \"airlabs_manage_web\", \"environment\": \"${{ env.DEPLOY_ENV }}\", \"level\": \"ERROR\", \"source\": \"${SOURCE}\", \"commit_hash\": \"${{ github.sha }}\", \"repo_url\": \"https://gitea.airlabs.art/${{ github.repository }}.git\", \"error\": { \"type\": \"${ERROR_TYPE}\", \"message\": \"[${FAILED_STEP}] Build and Deploy Web 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 - name: Docker Cleanup if: always() run: | docker container prune -f docker image prune -f docker builder prune -a -f echo "Disk usage after cleanup:" df -h / | tail -1