From bd006bcbeaac54d575b12e02fa81f0c49def5b37 Mon Sep 17 00:00:00 2001 From: gitea_admin Date: Fri, 19 Dec 2025 01:25:05 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20.github/workflows/buidl=5F?= =?UTF-8?q?and=5Fpush=5Fghcr.yml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/buidl_and_push_ghcr.yml | 41 ++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/.github/workflows/buidl_and_push_ghcr.yml b/.github/workflows/buidl_and_push_ghcr.yml index fdde176..bdc0d35 100644 --- a/.github/workflows/buidl_and_push_ghcr.yml +++ b/.github/workflows/buidl_and_push_ghcr.yml @@ -1,4 +1,4 @@ -name: Build with Kaniko (Clean & Fixed) +name: Build and Deploy (Kaniko + Kubectl) on: push: @@ -6,6 +6,7 @@ on: workflow_dispatch: jobs: + # --- 第一部分:构建并推送镜像 (保持原样,稍作变量优化) --- build: runs-on: ubuntu-latest steps: @@ -31,4 +32,42 @@ jobs: echo \"{\\\"auths\\\":{\\\"\$HOST\\\":{\\\"auth\\\":\\\"\$AUTH_STR\\\"}}}\" > /kaniko/.docker/config.json; /kaniko/executor --context=. --dockerfile=Dockerfile --destination=\$HOST/\$LOWER_USER/my-docs:latest --destination=\$HOST/\$LOWER_USER/my-docs:${{ github.sha }} --insecure --skip-tls-verify --cache=true + " + + # --- 第二部分:部署到 K8s (新增部分) --- + deploy: + needs: build # 只有 build 成功才执行 + runs-on: ubuntu-latest + steps: + - name: Deploy to Kubernetes + # 使用包含 kubectl 的轻量级镜像 + uses: docker://bitnami/kubectl:latest + env: + # 需要在 Gitea Secrets 中配置 KUBE_CONFIG + KUBECONFIG_CONTENT: ${{ secrets.KUBE_CONFIG }} + with: + entrypoint: /bin/sh + args: >- + -c + " + # 1. 配置 Kubeconfig + echo \"$KUBECONFIG_CONTENT\" > /tmp/kubeconfig; + export KUBECONFIG=/tmp/kubeconfig; + + # 2. 准备变量 (保持与 Build 阶段一致的逻辑) + HOST='gitea-http.gitea.svc.cluster.local:3000'; + RAW_USER='${{ gitea.actor }}'; + LOWER_USER=$(echo $RAW_USER | tr '[:upper:]' '[:lower:]'); + IMAGE_TAG='${{ github.sha }}'; + FULL_IMAGE=\"$HOST/$LOWER_USER/my-docs:$IMAGE_TAG\"; + + echo \"Deploying image: $FULL_IMAGE\"; + + # 3. 执行滚动更新 + # 注意:请将 'my-docs-deployment' 替换为你 K8s 里真实的 Deployment 名称 + # 注意:请将 'my-container' 替换为 Deployment yaml 里定义的容器名称 + kubectl set image deployment/my-docs-deployment my-container=$FULL_IMAGE -n default; + + # 4. 等待部署完成 + kubectl rollout status deployment/my-docs-deployment -n default; " \ No newline at end of file