更新 .github/workflows/buidl_and_push_ghcr.yml
All checks were successful
Build and Deploy (Kaniko + Kubectl) / build (push) Successful in 59s
Build and Deploy (Kaniko + Kubectl) / deploy (push) Successful in 10s

This commit is contained in:
2025-12-19 01:25:05 +00:00
parent 130877efe6
commit bd006bcbea

View File

@@ -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;
"