Files
membank/auto-check/docker/stress-test/build.sh

79 lines
2.9 KiB
Bash
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.
#!/usr/bin/env bash
# build.sh - 构建压测镜像并以时间戳打 tag
#
# 目的:每次构建都生成唯一 tag如 stress-test:20260824-153045
# 便于在 docker images 中区分新旧版本,避免 latest 重复构建后无法追溯。
#
# 用法:
# ./build.sh # 自动使用当前时间戳 (YYYYMMDD-HHMMSS)
# ./build.sh 20260824-153045 # 手动指定时间戳(用于复现/重打)
#
# 产物:
# - 镜像: stress-test:<timestamp> (仅时间戳 tag不再保留 latest
# - Tar : stress-test-<timestamp>.tar (带时间戳归档)
# + stress-test.tar auto-check 默认引用,同步覆盖)
# - auto-check.yaml 的 docker_image 字段会被自动同步为新时间戳 tag
#
# 注意:
# - 脚本会自动 cd 到自身所在目录,可从任意位置调用
# - 需要本地有 Docker 可用
# - 每次构建会覆盖上一份 stress-test.tar时间戳 tar 保留可作历史归档
# - 旧时间戳镜像可执行 docker rmi stress-test:<old-ts> 清理
set -euo pipefail
# 切到脚本所在目录Dockerfile 在这里)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# 时间戳:支持外部传入,否则取当前时间
TS="${1:-$(date +%Y%m%d-%H%M%S)}"
IMAGE_NAME="stress-test"
TAG_TS="${IMAGE_NAME}:${TS}"
TAR_TS="${IMAGE_NAME}-${TS}.tar"
TAR_LATEST="${IMAGE_NAME}.tar"
# auto-check.yaml 路径(相对本脚本 ../..
YAML_FILE="$SCRIPT_DIR/../../auto-check.yaml"
echo "==> 时间戳: ${TS}"
echo "==> 构建镜像: ${TAG_TS}"
docker build -t "${TAG_TS}" .
echo "==> 导出 Tar: ${TAR_TS}"
docker save -o "${TAR_TS}" "${TAG_TS}"
echo "==> 同步 Latest Tar: ${TAR_LATEST}"
cp -f "${TAR_TS}" "${TAR_LATEST}"
# 同步 auto-check.yaml 中的 docker_image 为新时间戳 tag
if [ -f "$YAML_FILE" ]; then
if grep -q '^[[:space:]]*docker_image:' "$YAML_FILE"; then
if ! grep -q "${TAG_TS}" "$YAML_FILE"; then
# -i.bak 同时生成备份(兼容 GNU sed / BSD sed改完再删
sed -i.bak -E "s|(docker_image:[[:space:]]*\")[^\"]+(\")|\1${TAG_TS}\2|" "$YAML_FILE"
rm -f "${YAML_FILE}.bak"
echo "==> 已更新 ${YAML_FILE##*/}: docker_image -> ${TAG_TS}"
else
echo "==> ${YAML_FILE##*/} 中 docker_image 已是 ${TAG_TS},跳过"
fi
else
echo "==> 未在 ${YAML_FILE##*/} 中找到 docker_image 字段,请手动更新"
fi
else
echo "==> 未找到 ${YAML_FILE},请手动确保 docker_image 与新 tag 一致"
fi
echo ""
echo "=========================================="
echo " 构建完成"
echo "=========================================="
echo " 镜像 Tag: ${TAG_TS}"
echo " Tar 文件: ${TAR_TS}"
echo " ${TAR_LATEST} (auto-check 默认引用)"
echo ""
echo "==> 本机 ${IMAGE_NAME} 镜像列表:"
docker images "${IMAGE_NAME}" --format "table {{.Repository}}\t{{.Tag}}\t{{.CreatedAt}}\t{{.Size}}"