更新 .github/workflows/buidl_and_push_ghcr.yml
Some checks failed
Build and Deploy (Kaniko + Kubectl) / build (push) Successful in 1m4s
Build and Deploy (Kaniko + Kubectl) / deploy (push) Has been cancelled

This commit is contained in:
2025-12-19 04:17:54 +00:00
parent f4cc051698
commit f04993e0ca

View File

@@ -41,10 +41,8 @@ jobs:
- name: Deploy to Kubernetes - name: Deploy to Kubernetes
uses: docker://bitnami/kubectl:latest uses: docker://bitnami/kubectl:latest
env: env:
# --- 1. 将变量提到这里,避免 Shell 引号地狱 ---
KUBECONFIG_BASE64: ${{ secrets.KUBE_CONFIG_TEST }} KUBECONFIG_BASE64: ${{ secrets.KUBE_CONFIG_TEST }}
HOST: gitea-http.gitea.svc.cluster.local:3000 HOST: gitea-http.gitea.svc.cluster.local:3000
# Gitea 变量直接在这里注入
RAW_USER: ${{ gitea.actor }} RAW_USER: ${{ gitea.actor }}
IMAGE_TAG: ${{ github.sha }} IMAGE_TAG: ${{ github.sha }}
with: with:
@@ -52,39 +50,30 @@ jobs:
args: >- args: >-
-c -c
" "
# --- 2. 开启 Debug 模式 (关键!) --- set -xe;
# -x: 打印执行的每一行命令
# -e: 遇到错误立即退出,不再继续 echo '>>> Step 1: Checking Environment';
set -xe
echo '>>> Step 1: Checking Environment'
if [ -z \"$KUBECONFIG_BASE64\" ]; then if [ -z \"$KUBECONFIG_BASE64\" ]; then
echo 'Error: KUBECONFIG_BASE64 is empty! Check Gitea Secrets.' echo 'Error: KUBECONFIG_BASE64 is empty! Check Gitea Secrets.';
exit 1 exit 1;
fi fi;
echo '>>> Step 2: Decoding Kubeconfig' echo '>>> Step 2: Decoding Kubeconfig';
# 使用 base64 -d 解码,忽略可能存在的换行符 echo \"$KUBECONFIG_BASE64\" | base64 -d > /tmp/kubeconfig;
echo \"$KUBECONFIG_BASE64\" | base64 -d > /tmp/kubeconfig export KUBECONFIG=/tmp/kubeconfig;
export KUBECONFIG=/tmp/kubeconfig
# 测试 kubectl 是否能连接 (验证 config 是否有效)
echo '>>> Step 3: Verifying Cluster Connection'
kubectl cluster-info
kubectl get pods -n dev || echo 'Warning: Could not list pods, check permissions'
echo '>>> Step 4: Preparing Image Name' echo '>>> Step 3: Verifying Cluster Connection';
# 在 Shell 内部处理大小写转换 kubectl cluster-info;
LOWER_USER=$(echo $RAW_USER | tr '[:upper:]' '[:lower:]')
FULL_IMAGE=\"$HOST/$LOWER_USER/my-docs:$IMAGE_TAG\"
echo \"Target Image: $FULL_IMAGE\" echo '>>> Step 4: Preparing Image Name';
LOWER_USER=$(echo $RAW_USER | tr '[:upper:]' '[:lower:]');
FULL_IMAGE=\"$HOST/$LOWER_USER/my-docs:$IMAGE_TAG\";
echo \"Target Image: $FULL_IMAGE\";
echo '>>> Step 5: Executing Deployment' echo '>>> Step 5: Executing Deployment';
# 执行部署 kubectl set image deployment/my-docs nginx=$FULL_IMAGE -n dev;
kubectl set image deployment/my-docs nginx=$FULL_IMAGE -n dev
echo '>>> Step 6: Restarting and Waiting';
echo '>>> Step 6: Restarting and Waiting' kubectl rollout restart deployment/my-docs -n dev;
kubectl rollout restart deployment/my-docs -n dev kubectl rollout status deployment/my-docs -n dev;
kubectl rollout status deployment/my-docs -n dev
" "