mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 06:33:37 +08:00
77 lines
1.9 KiB
Bash
77 lines
1.9 KiB
Bash
#!/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"
|
||
local version_output=""
|
||
|
||
if command -v "$name" >/dev/null 2>&1; then
|
||
if [[ "$name" == "kubectl" ]]; then
|
||
if version_output=$(kubectl version --client --short 2>/dev/null | head -n 1); then
|
||
:
|
||
else
|
||
version_output=$(kubectl version --client 2>/dev/null | head -n 1)
|
||
fi
|
||
else
|
||
version_output=$(bash -c "$version_cmd")
|
||
fi
|
||
|
||
log_success "检测到 $name: $version_output"
|
||
printf '%s=%s\n' "$output_var" "$version_output" >> "$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 enable_validation="${ENABLE_VALIDATION:-true}"
|
||
|
||
if [[ "$enable_validation" != "true" ]]; then
|
||
log_info "已通过统一开关禁用环境校验"
|
||
printf 'docker-version=%s\n' "skipped" >> "$GITHUB_OUTPUT"
|
||
printf 'kubectl-version=%s\n' "skipped" >> "$GITHUB_OUTPUT"
|
||
return 0
|
||
fi
|
||
|
||
validate_binary "docker" "true" "docker --version" "docker-version"
|
||
validate_binary "kubectl" "true" "kubectl version --client --short 2>/dev/null || kubectl version --client" "kubectl-version"
|
||
}
|
||
|
||
trap 'log_error "工具校验失败,退出码: $?"' ERR
|
||
|
||
main "$@"
|