thingsboard/build-tb-postgres-multiarch.sh

290 lines
8.8 KiB
Bash
Executable File
Raw Permalink 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
#
# Copyright © 2016-2025 The Thingsboard Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
# ThingsBoard PostgreSQL 单镜像多平台构建脚本
#
# 功能:
# 1. 构建 ThingsBoard PostgreSQL 单镜像
# 2. 支持多平台linux/amd64 (Intel/AMD) 和 linux/arm64 (Mac M1/M2)
# 3. 自动推送到私有仓库registry.t-aaron.com
#
# 使用方法:
# ./build-tb-postgres-multiarch.sh
#
# 作者Auto-generated
# 日期2026-01-19
################################################################################
set -e # 遇到错误立即退出
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 配置变量
REGISTRY="registry.t-aaron.com"
IMAGE_NAME="thingsboard/tb-postgres"
PLATFORMS="linux/amd64,linux/arm64"
BUILDER_NAME="tb-multiarch-builder"
# 打印带颜色的消息
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_step() {
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}$1${NC}"
echo -e "${GREEN}========================================${NC}"
}
# 检查命令是否存在
check_command() {
if ! command -v $1 &> /dev/null; then
print_error "$1 未安装,请先安装"
exit 1
fi
}
# 获取项目根目录
PROJECT_ROOT=$(pwd)
print_info "项目根目录: $PROJECT_ROOT"
################################################################################
# 步骤 0: 环境检查
################################################################################
print_step "步骤 0: 环境检查"
# 检查必要的命令
print_info "检查必要的命令..."
check_command "docker"
check_command "mvn"
check_command "java"
# 检查 Docker Buildx
if ! docker buildx version &> /dev/null; then
print_error "Docker Buildx 未安装或未启用"
print_info "请确保使用 Docker Desktop 或手动安装 Buildx"
exit 1
fi
print_success "Docker Buildx 已安装"
# 检查 Docker 是否运行
if ! docker info &> /dev/null; then
print_error "Docker 未运行,请启动 Docker Desktop"
exit 1
fi
print_success "Docker 正在运行"
################################################################################
# 步骤 1: 获取版本号
################################################################################
print_step "步骤 1: 获取版本号"
if [ ! -f "pom.xml" ]; then
print_error "未找到 pom.xml请确保在项目根目录执行此脚本"
exit 1
fi
VERSION=$(grep "<version>" pom.xml | head -1 | sed 's/.*<version>\(.*\)<\/version>.*/\1/')
if [ -z "$VERSION" ]; then
print_error "无法从 pom.xml 获取版本号"
exit 1
fi
print_success "当前版本: $VERSION"
# 构建完整的镜像标签
FULL_IMAGE_TAG="$REGISTRY/$IMAGE_NAME:$VERSION"
LATEST_IMAGE_TAG="$REGISTRY/$IMAGE_NAME:latest"
print_info "镜像标签: $FULL_IMAGE_TAG"
print_info "最新标签: $LATEST_IMAGE_TAG"
################################################################################
# 步骤 2: 拉取基础镜像
################################################################################
print_step "步骤 2: 拉取基础镜像"
print_info "拉取 registry.t-aaron.com/thingsboard/openjdk17:bookworm-slim..."
if docker pull registry.t-aaron.com/thingsboard/openjdk17:bookworm-slim; then
print_success "基础镜像拉取成功"
else
print_warning "基础镜像拉取失败,将尝试使用本地缓存"
fi
################################################################################
# 步骤 3: Maven 构建
################################################################################
print_step "步骤 3: Maven 构建代码和 DEB 包"
print_info "开始 Maven 构建(跳过测试)..."
print_info "这可能需要 5-15 分钟,请耐心等待..."
MAVEN_OPTS="-Xmx2048m" mvn clean install -DskipTests --projects msa/tb --also-make
if [ $? -ne 0 ]; then
print_error "Maven 构建失败"
exit 1
fi
print_success "Maven 构建完成"
################################################################################
# 步骤 4: 验证构建产物
################################################################################
print_step "步骤 4: 验证构建产物"
BUILD_DIR="msa/tb/target/docker-postgres"
if [ ! -d "$BUILD_DIR" ]; then
print_error "构建目录不存在: $BUILD_DIR"
exit 1
fi
print_info "检查构建产物..."
REQUIRED_FILES=("Dockerfile" "thingsboard.deb" "start-tb.sh" "start-db.sh" "stop-db.sh" "thingsboard.conf" "logback.xml")
for file in "${REQUIRED_FILES[@]}"; do
if [ ! -f "$BUILD_DIR/$file" ]; then
print_error "缺少必要文件: $file"
exit 1
fi
done
print_success "所有必要文件已就绪"
# 显示 DEB 包大小
DEB_SIZE=$(du -h "$BUILD_DIR/thingsboard.deb" | cut -f1)
print_info "DEB 包大小: $DEB_SIZE"
################################################################################
# 步骤 5: 配置 Docker Buildx
################################################################################
print_step "步骤 5: 配置 Docker Buildx"
# 检查 builder 是否存在
if docker buildx inspect $BUILDER_NAME &> /dev/null; then
print_info "使用现有的 builder: $BUILDER_NAME"
docker buildx use $BUILDER_NAME
else
print_info "创建新的 builder: $BUILDER_NAME"
docker buildx create --name $BUILDER_NAME --use --driver docker-container
fi
# 启动 builder
print_info "启动 builder..."
docker buildx inspect --bootstrap
print_success "Buildx 配置完成"
################################################################################
# 步骤 6: 构建多平台镜像
################################################################################
print_step "步骤 6: 构建多平台 Docker 镜像"
print_info "支持的平台: $PLATFORMS"
print_info "开始构建镜像,这可能需要 5-15 分钟..."
print_warning "注意: 构建 arm64 平台可能需要使用 QEMU 模拟,会比较慢"
cd "$BUILD_DIR"
# 构建并推送多平台镜像
docker buildx build \
--platform $PLATFORMS \
--tag $FULL_IMAGE_TAG \
--tag $LATEST_IMAGE_TAG \
--push \
.
if [ $? -ne 0 ]; then
print_error "Docker 镜像构建失败"
cd "$PROJECT_ROOT"
exit 1
fi
cd "$PROJECT_ROOT"
print_success "多平台镜像构建完成"
################################################################################
# 步骤 7: 验证镜像推送
################################################################################
print_step "步骤 7: 验证镜像推送"
print_info "验证镜像是否成功推送到仓库..."
# 使用 buildx imagetools 检查镜像
if docker buildx imagetools inspect $FULL_IMAGE_TAG &> /dev/null; then
print_success "镜像已成功推送到: $FULL_IMAGE_TAG"
# 显示镜像详细信息
print_info "镜像详细信息:"
docker buildx imagetools inspect $FULL_IMAGE_TAG
else
print_warning "无法验证镜像推送状态,但构建过程未报错"
fi
################################################################################
# 完成
################################################################################
print_step "构建完成!"
echo ""
print_success "镜像已成功构建并推送到私有仓库"
echo ""
echo -e "${GREEN}镜像信息:${NC}"
echo -e " 版本镜像: ${BLUE}$FULL_IMAGE_TAG${NC}"
echo -e " 最新镜像: ${BLUE}$LATEST_IMAGE_TAG${NC}"
echo -e " 支持平台: ${BLUE}$PLATFORMS${NC}"
echo ""
echo -e "${GREEN}使用方法:${NC}"
echo ""
echo -e "${YELLOW}1. 在 Mac (M1/M2) 或 Linux (ARM64) 上运行:${NC}"
echo -e " docker run -d -p 8080:9090 -p 1883:1883 \\"
echo -e " -v ~/.mytb-data:/data \\"
echo -e " --name mytb \\"
echo -e " $FULL_IMAGE_TAG"
echo ""
echo -e "${YELLOW}2. 在 Linux (x86_64/AMD64) 上运行:${NC}"
echo -e " docker run -d -p 8080:9090 -p 1883:1883 \\"
echo -e " -v ~/.mytb-data:/data \\"
echo -e " --name mytb \\"
echo -e " $FULL_IMAGE_TAG"
echo ""
echo -e "${YELLOW}3. 访问 ThingsBoard:${NC}"
echo -e " http://localhost:8080"
echo -e " 默认账号: ${BLUE}sysadmin@thingsboard.org${NC} / ${BLUE}sysadmin${NC}"
echo ""
print_success "脚本执行完成!"