name: Build and Deploy Web 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 Web uses: docker/build-push-action@v4 with: context: . push: true provenance: false tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/rtc-web:latest - name: Setup Kubectl run: | curl -LO "https://files.m.daocloud.io/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 run: | # 1. 判断分支 if [[ "${{ github.ref_name }}" == "main" || "${{ github.ref_name }}" == "master" ]]; then echo "Environment: Production" DEPLOY_FILE="k8s/web-deployment-prod.yaml" INGRESS_FILE="k8s/ingress.yaml" else echo "Environment: Development" DEPLOY_FILE="k8s/web-deployment-dev.yaml" INGRESS_FILE="k8s/ingress-dev.yaml" fi # 2. 替换镜像地址 sed -i "s|\${CI_REGISTRY_IMAGE}/web:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/rtc-web:latest|g" $DEPLOY_FILE # 3. 应用配置 kubectl apply -f $DEPLOY_FILE kubectl apply -f $INGRESS_FILE # 4. 根据分支重启对应服务 if [[ "${{ github.ref_name }}" == "main" || "${{ github.ref_name }}" == "master" ]]; then kubectl rollout restart deployment/rtc-web else kubectl rollout restart deployment/rtc-web-dev fi - name: Report failure to Log Center if: failure() run: | curl -s -X POST "https://qiyuan-log-center-api.airlabs.art/api/v1/logs/report" \ -H "Content-Type: application/json" \ -d "{ \"project_id\": \"rtc_web\", \"environment\": \"${{ github.ref_name }}\", \"level\": \"ERROR\", \"source\": \"cicd\", \"commit_hash\": \"${{ github.sha }}\", \"repo_url\": \"https://gitea.airlabs.art/zyc/rtc_web.git\", \"error\": { \"type\": \"CICDFailure\", \"message\": \"Build and Deploy failed on branch ${{ github.ref_name }}\", \"stack_trace\": [ \"Repository: ${{ github.repository }}\", \"Branch: ${{ github.ref_name }}\", \"Commit: ${{ github.sha }}\", \"Run ID: ${{ github.run_id }}\", \"Actor: ${{ github.actor }}\" ] }, \"context\": { \"workflow\": \"${{ github.workflow }}\", \"run_id\": \"${{ github.run_id }}\", \"branch\": \"${{ github.ref_name }}\", \"actor\": \"${{ github.actor }}\" } }" || true