feat: 新增配置构建环境的 GitHub Action,支持 Git 和 kubectl 配置验证

This commit is contained in:
Lyda
2025-10-11 18:21:10 +08:00
parent 2fe7c6809c
commit f618608667
7 changed files with 448 additions and 0 deletions

View 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 "$@"