mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 23:03:37 +08:00
feat: 新增配置构建环境的 GitHub Action,支持 Git 和 kubectl 配置验证
This commit is contained in:
59
config-env/scripts/configure-git.sh
Normal file
59
config-env/scripts/configure-git.sh
Normal file
@@ -0,0 +1,59 @@
|
||||
#!/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}"
|
||||
}
|
||||
|
||||
main() {
|
||||
if ! command -v git >/dev/null 2>&1; then
|
||||
log_error "未检测到 git 命令"
|
||||
exit 1
|
||||
else
|
||||
log_info "已检测到 git: $(git --version)"
|
||||
fi
|
||||
|
||||
local name="${GIT_USER_NAME:-}"
|
||||
local email="${GIT_USER_EMAIL:-}"
|
||||
|
||||
if [[ -z "$name" && -z "$email" ]]; then
|
||||
log_warning "未提供 Git 用户名和邮箱,跳过配置"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ -n "$name" ]]; then
|
||||
git config --global user.name "$name"
|
||||
log_success "已配置 Git 用户名: $name"
|
||||
else
|
||||
log_warning "未提供 Git 用户名"
|
||||
fi
|
||||
|
||||
if [[ -n "$email" ]]; then
|
||||
git config --global user.email "$email"
|
||||
log_success "已配置 Git 邮箱: $email"
|
||||
else
|
||||
log_warning "未提供 Git 邮箱"
|
||||
fi
|
||||
}
|
||||
|
||||
trap 'log_error "Git 配置失败,退出码: $?"' ERR
|
||||
|
||||
main "$@"
|
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 "$@"
|
70
config-env/scripts/validate-tools.sh
Normal file
70
config-env/scripts/validate-tools.sh
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/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}"
|
||||
}
|
||||
|
||||
validate_binary() {
|
||||
local name="$1"
|
||||
local required="$2"
|
||||
local version_cmd="$3"
|
||||
local output_var="$4"
|
||||
|
||||
if command -v "$name" >/dev/null 2>&1; then
|
||||
log_success "检测到 $name: $($version_cmd)"
|
||||
printf '%s=%s\n' "$output_var" "$($version_cmd)" >> "$GITHUB_OUTPUT"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ "$required" == "true" ]]; then
|
||||
log_error "未检测到必需的命令: $name"
|
||||
exit 1
|
||||
else
|
||||
log_warning "未检测到可选命令: $name"
|
||||
printf '%s=%s\n' "$output_var" "not-found" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
}
|
||||
|
||||
main() {
|
||||
: "${GITHUB_OUTPUT:?GITHUB_OUTPUT 未设置}" >/dev/null
|
||||
|
||||
local check_docker="${CHECK_DOCKER:-true}"
|
||||
local check_kubectl="${CHECK_KUBECTL:-true}"
|
||||
|
||||
if [[ "$check_docker" != "true" ]]; then
|
||||
log_info "跳过 Docker 校验"
|
||||
printf 'docker-version=%s\n' "skipped" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
validate_binary "docker" "true" "docker --version" "docker-version"
|
||||
fi
|
||||
|
||||
if [[ "$check_kubectl" != "true" ]]; then
|
||||
log_info "跳过 kubectl 校验"
|
||||
printf 'kubectl-version=%s\n' "skipped" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
validate_binary "kubectl" "true" "kubectl version --client --short 2>/dev/null || kubectl version --client" "kubectl-version"
|
||||
fi
|
||||
}
|
||||
|
||||
trap 'log_error "工具校验失败,退出码: $?"' ERR
|
||||
|
||||
main "$@"
|
60
config-env/scripts/verify-kubectl.sh
Normal file
60
config-env/scripts/verify-kubectl.sh
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/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}"
|
||||
}
|
||||
|
||||
: "${GITHUB_OUTPUT:?GITHUB_OUTPUT 未设置}" >/dev/null
|
||||
|
||||
verify_cluster() {
|
||||
local timeout=30
|
||||
|
||||
if timeout "$timeout" kubectl cluster-info >/dev/null 2>&1; then
|
||||
log_success "kubectl 集群连接验证通过"
|
||||
log_info "集群信息:"
|
||||
kubectl cluster-info
|
||||
|
||||
log_info "尝试获取节点信息"
|
||||
if kubectl get nodes >/dev/null 2>&1; then
|
||||
kubectl get nodes
|
||||
else
|
||||
log_warning "无法获取节点信息(可能权限不足)"
|
||||
fi
|
||||
|
||||
local current_context
|
||||
current_context=$(kubectl config current-context 2>/dev/null || echo "unknown")
|
||||
log_info "当前上下文: $current_context"
|
||||
printf 'current-context=%s\n' "$current_context" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
log_error "kubectl 集群连接验证失败"
|
||||
kubectl cluster-info || true
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
trap 'log_error "kubectl 验证失败,退出码: $?"' ERR
|
||||
|
||||
main() {
|
||||
verify_cluster
|
||||
}
|
||||
|
||||
main "$@"
|
Reference in New Issue
Block a user