Files
i-tools/build-and-push.sh
yfan 964451c383
Some checks failed
Build and Push Docker Image / build (push) Has been cancelled
Sync to CNB / sync (push) Has been cancelled
Delete old workflow runs / del_runs (push) Has been cancelled
Upstream Sync / Sync latest commits from upstream repo (push) Has been cancelled
修改docker脚本
2026-02-02 17:27:18 +08:00

349 lines
10 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 创智未来信奥工具箱 - Docker镜像构建和发布脚本
# 发布到阿里云镜像仓库: registry.cn-hangzhou.aliyuncs.com/nick-x86
set -e # 遇到错误立即退出
# 配置变量
REGISTRY="registry.cn-hangzhou.aliyuncs.com"
NAMESPACE="nick-x86"
IMAGE_NAME="i-tools"
# 目标平台配置服务器是x86架构
PLATFORM="linux/amd64"
CURRENT_ARCH=$(uname -m)
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 日志函数
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 显示横幅
show_banner() {
echo -e "${BLUE}"
echo "================================================="
echo " 创智未来信奥工具箱 Docker 构建发布脚本"
echo "================================================="
echo -e "${NC}"
}
# 获取版本号
get_version() {
if [ -f "package.json" ]; then
VERSION=$(node -p "require('./package.json').version" 2>/dev/null || echo "latest")
else
VERSION="latest"
fi
# 如果传入了版本参数,使用传入的版本
if [ ! -z "$1" ]; then
VERSION="$1"
log_info "使用指定版本: $VERSION"
else
log_info "从 package.json 读取版本: $VERSION"
fi
}
# 检查Docker是否安装
check_docker() {
if ! command -v docker &> /dev/null; then
log_error "Docker 未安装或不在 PATH 中"
exit 1
fi
if ! docker info &> /dev/null; then
log_error "Docker 守护进程未运行"
exit 1
fi
log_success "Docker 环境检查通过"
}
# 检查是否已登录阿里云镜像仓库
check_registry_login() {
log_info "检查阿里云镜像仓库登录状态..."
if ! docker system info | grep -q "Registry:"; then
log_warning "建议先登录阿里云镜像仓库"
echo "请运行以下命令登录:"
echo "docker login --username=yf6230065@163.com $REGISTRY"
read -p "是否已经登录?(y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_error "请先登录阿里云镜像仓库后再运行此脚本"
exit 1
fi
fi
log_success "镜像仓库登录状态检查通过"
}
# 构建镜像
build_image() {
local full_image_name="$REGISTRY/$NAMESPACE/$IMAGE_NAME:$VERSION"
local latest_image_name="$REGISTRY/$NAMESPACE/$IMAGE_NAME:latest"
log_info "开始构建 Docker 镜像..."
log_info "镜像名称: $full_image_name"
log_info "当前架构: $CURRENT_ARCH"
log_info "目标平台: $PLATFORM (服务器x86架构)"
# 构建x86架构镜像可用 NODE_IMAGE 或 --node-image 指定本地 Node 基础镜像)
local build_opts=()
if [ "$NO_CACHE" = true ]; then
build_opts+=(--no-cache)
fi
if [ -n "$NODE_IMAGE" ]; then
log_info "使用 Node 基础镜像: $NODE_IMAGE"
build_opts+=(--build-arg "NODE_IMAGE=$NODE_IMAGE")
fi
if [ -n "$NPM_REGISTRY" ]; then
log_info "使用 npm 源: $NPM_REGISTRY"
build_opts+=(--build-arg "NPM_REGISTRY=$NPM_REGISTRY")
fi
if docker build \
"${build_opts[@]}" \
--platform "$PLATFORM" \
--tag "$full_image_name" \
--tag "$latest_image_name" \
--build-arg BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
--build-arg VERSION="$VERSION" \
. ; then
log_success "x86架构镜像构建成功"
echo " - $full_image_name"
echo " - $latest_image_name"
if [ "$CURRENT_ARCH" = "arm64" ]; then
log_warning "注意: 在ARM64机器上构建的x86镜像适用于x86服务器部署"
fi
else
log_error "镜像构建失败"
exit 1
fi
}
# 推送镜像
push_image() {
local full_image_name="$REGISTRY/$NAMESPACE/$IMAGE_NAME:$VERSION"
local latest_image_name="$REGISTRY/$NAMESPACE/$IMAGE_NAME:latest"
log_info "开始推送镜像到阿里云镜像仓库..."
# 推送带版本号的镜像
log_info "推送版本镜像: $full_image_name"
if docker push "$full_image_name"; then
log_success "版本镜像推送成功"
else
log_error "版本镜像推送失败"
exit 1
fi
# 推送latest镜像
log_info "推送latest镜像: $latest_image_name"
if docker push "$latest_image_name"; then
log_success "latest镜像推送成功"
else
log_error "latest镜像推送失败"
exit 1
fi
}
# 清理本地镜像(可选)
cleanup_local() {
read -p "是否清理本地构建的镜像?(y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
local full_image_name="$REGISTRY/$NAMESPACE/$IMAGE_NAME:$VERSION"
local latest_image_name="$REGISTRY/$NAMESPACE/$IMAGE_NAME:latest"
log_info "清理本地镜像..."
docker rmi "$full_image_name" "$latest_image_name" 2>/dev/null || true
log_success "本地镜像清理完成"
fi
}
# 显示部署信息
show_deployment_info() {
local full_image_name="$REGISTRY/$NAMESPACE/$IMAGE_NAME:$VERSION"
echo -e "${GREEN}"
echo "================================================="
echo " 镜像发布成功!"
echo "================================================="
echo -e "${NC}"
echo "镜像地址:"
echo " 版本镜像: $full_image_name"
echo " 最新镜像: $REGISTRY/$NAMESPACE/$IMAGE_NAME:latest"
echo ""
echo "使用方法:"
echo " docker pull $full_image_name"
echo " docker run -p 3000:3000 $full_image_name"
echo ""
echo "启用测试点生成-大模型(可选,需宿主机有 .env"
echo " docker run -p 3000:3000 --env-file .env $full_image_name"
echo " 或: docker run -p 3000:3000 -e ENABLE_AI=true -e DASHSCOPE_API_KEY=sk-xxx $full_image_name"
echo ""
echo "Docker Compose 示例:"
echo " image: $full_image_name"
echo ""
}
# 解析命令行参数
parse_args() {
BUILD_ONLY=false
NO_CACHE=false
VERSION_ARG=""
while [[ $# -gt 0 ]]; do
case $1 in
-v|--version)
if [ -z "$2" ]; then
log_error "-v/--version 参数需要指定版本号"
exit 1
fi
VERSION_ARG="$2"
shift 2
;;
--node-image)
if [ -z "$2" ]; then
log_error "--node-image 参数需要指定镜像名(如 node:20-alpine"
exit 1
fi
NODE_IMAGE="$2"
shift 2
;;
--npm-registry)
if [ -z "$2" ]; then
log_error "--npm-registry 参数需要指定地址(如 https://registry.npmmirror.com"
exit 1
fi
NPM_REGISTRY="$2"
shift 2
;;
--build-only)
BUILD_ONLY=true
shift
;;
--no-cache)
NO_CACHE=true
shift
;;
-h|--help)
show_help
exit 0
;;
*)
# 如果没有使用 -v 参数,第一个参数作为版本号
if [ -z "$VERSION_ARG" ]; then
VERSION_ARG="$1"
else
log_error "未知参数: $1"
show_help
exit 1
fi
shift
;;
esac
done
}
# 主函数
main() {
show_banner
# 解析命令行参数
parse_args "$@"
# 获取版本号
get_version "$VERSION_ARG"
# 环境检查
check_docker
check_registry_login
# 构建镜像
if [ "$NO_CACHE" = true ]; then
# 如果支持 --no-cache需要修改 build_image 函数
log_info "使用 --no-cache 选项构建(当前版本暂不支持,将正常构建)"
fi
build_image
# 如果不是仅构建,则推送镜像
if [ "$BUILD_ONLY" = false ]; then
push_image
# 显示结果
show_deployment_info
else
log_info "仅构建模式,跳过推送步骤"
log_success "镜像构建完成!"
fi
# 可选清理
cleanup_local
log_success "所有操作完成!"
}
# 帮助信息
show_help() {
echo "创智未来信奥工具箱 Docker 构建发布脚本"
echo ""
echo "用法:"
echo " $0 [选项] [版本号]"
echo ""
echo "选项:"
echo " -v, --version <版本号> 指定要构建的版本号"
echo " --node-image <镜像> 指定 Node 基础镜像(默认 node:20-alpine可用本地镜像"
echo " --npm-registry <地址> 指定 npm 源(卡在 npm ci 时可用 https://registry.npmmirror.com"
echo " --build-only 仅构建镜像,不推送"
echo " --no-cache 不使用缓存构建(暂未实现)"
echo " -h, --help 显示此帮助信息"
echo ""
echo "参数:"
echo " 版本号 可选指定要构建的版本号默认从package.json读取"
echo ""
echo "示例:"
echo " $0 # 使用package.json中的版本"
echo " $0 1.0.4 # 使用指定版本1.0.4"
echo " $0 -v 1.0.4 # 使用 -v 参数指定版本1.0.4"
echo " $0 --version 1.0.4 # 使用 --version 参数指定版本1.0.4"
echo " $0 --build-only # 仅构建,不推送"
echo " $0 -v 1.0.4 --build-only # 指定版本并仅构建"
echo " $0 --node-image node:20-alpine # 使用本地/指定 Node 镜像"
echo " $0 --npm-registry https://registry.npmmirror.com # 国内构建用镜像源"
echo " NODE_IMAGE=node:20-alpine $0 # 通过环境变量指定 Node 镜像"
echo " NPM_REGISTRY=https://registry.npmmirror.com $0 # 通过环境变量指定 npm 源"
echo ""
echo "环境要求:"
echo " - Docker已安装并运行"
echo " - 已登录阿里云镜像仓库"
echo ""
echo "登录命令:"
echo " docker login --username=<用户名> $REGISTRY"
}
# 脚本入口
# 运行主函数,传入所有参数
main "$@"