mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 14:23:37 +08:00
feat: 新增配置构建环境的 GitHub Action,支持 Git 和 kubectl 配置验证
This commit is contained in:
65
config-env/scripts/configure-kubectl.sh
Normal file
65
config-env/scripts/configure-kubectl.sh
Normal file
@@ -0,0 +1,65 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
log_info() {
|
||||
echo -e "${BLUE}ℹ️ $1${NC}"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}✅ $1${NC}"
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
echo -e "${YELLOW}⚠️ $1${NC}"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}❌ $1${NC}"
|
||||
}
|
||||
|
||||
ensure_kubectl_available() {
|
||||
if ! command -v kubectl >/dev/null 2>&1; then
|
||||
log_error "kubectl 未安装或不可用"
|
||||
exit 1
|
||||
fi
|
||||
log_info "kubectl 版本: $(kubectl version --client --short 2>/dev/null || kubectl version --client)"
|
||||
}
|
||||
|
||||
write_kube_config() {
|
||||
local encoded="${KUBE_CONFIG_BASE64:-}"
|
||||
if [[ -z "$encoded" ]]; then
|
||||
log_error "KUBE_CONFIG_BASE64 环境变量为空"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$HOME/.kube"
|
||||
local config_path="$HOME/.kube/config"
|
||||
|
||||
echo "$encoded" | base64 -d > "$config_path"
|
||||
chmod 600 "$config_path"
|
||||
log_success "已写入 kubectl 配置: $config_path"
|
||||
}
|
||||
|
||||
validate_kube_config() {
|
||||
if ! kubectl config view --minify >/dev/null 2>&1; then
|
||||
log_error "kubectl 配置文件无效或权限不足"
|
||||
exit 1
|
||||
fi
|
||||
log_success "kubectl 配置文件格式验证通过"
|
||||
}
|
||||
|
||||
trap 'log_error "kubectl 配置失败,退出码: $?"' ERR
|
||||
|
||||
main() {
|
||||
ensure_kubectl_available
|
||||
write_kube_config
|
||||
validate_kube_config
|
||||
}
|
||||
|
||||
main "$@"
|
Reference in New Issue
Block a user