unis_crm/deploy/build-and-push.sh

72 lines
2.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# 构建 unis_scc 镜像(linux/amd64)并推送到 Harbor。
#
# ./deploy/build-and-push.sh # 默认:all-in-one 单镜像 unis_scc
# ./deploy/build-and-push.sh app # 同上,显式指定
# ./deploy/build-and-push.sh backend frontend # 分别打后端/前端镜像(管理端已并入前端 /admin/*)
# TAG=202610291500 ./deploy/build-and-push.sh
# PUSH=0 ./deploy/build-and-push.sh # 只构建不推送
set -euo pipefail
# 默认仓库与 deploy/README.md、k3s 清单里的 image 前缀保持一致(docker.llm 已停用,解析得到但连不上)。
REGISTRY="${REGISTRY:-docker.oa.unissense.tech}"
PROJECT="${PROJECT:-public}"
BASE_NAME="${BASE_NAME:-unis_scc}"
TAG="${TAG:-$(date +%Y%m%d%H%M)}"
PLATFORM="${PLATFORM:-linux/amd64}"
PUSH="${PUSH:-1}"
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BACKEND_JAR="$ROOT_DIR/backend/target/unis-crm-backend-1.0.0-SNAPSHOT.jar"
# 组件 -> "构建上下文|Dockerfile|镜像后缀"
spec() {
case "$1" in
app) echo "$ROOT_DIR|$ROOT_DIR/deploy/all-in-one/Dockerfile|" ;;
backend) echo "$ROOT_DIR/backend|$ROOT_DIR/backend/Dockerfile|-backend" ;;
frontend) echo "$ROOT_DIR/frontend|$ROOT_DIR/frontend/Dockerfile|-frontend" ;;
*) return 1 ;;
esac
}
targets=("$@")
if [ ${#targets[@]} -eq 0 ]; then
targets=(app)
fi
need_jar=0
for t in "${targets[@]}"; do
case "$t" in app|backend) need_jar=1 ;; esac
done
if [ "$need_jar" = 1 ]; then
echo "==> 重新打包后端 jar(JDK 17)"
JAVA_HOME="${JAVA_HOME_17:-$(/usr/libexec/java_home -v 17)}" \
mvn -B -q -DskipTests -f "$ROOT_DIR/backend/pom.xml" clean package
[ -f "$BACKEND_JAR" ] || { echo "缺少 $BACKEND_JAR"; exit 1; }
fi
for comp in "${targets[@]}"; do
IFS='|' read -r ctx dockerfile suffix < <(spec "$comp") || { echo "未知组件: $comp"; exit 1; }
image="$REGISTRY/$PROJECT/$BASE_NAME$suffix"
echo "==> 构建 $image:$TAG ($PLATFORM)"
docker buildx build --platform "$PLATFORM" \
-f "$dockerfile" \
-t "$image:$TAG" -t "$image:latest" \
--load "$ctx"
done
if [ "$PUSH" != 1 ]; then
echo "==> PUSH=0,跳过推送"; exit 0
fi
for comp in "${targets[@]}"; do
IFS='|' read -r _ _ suffix < <(spec "$comp")
image="$REGISTRY/$PROJECT/$BASE_NAME$suffix"
echo "==> 推送 $image"
docker push "$image:$TAG"
docker push "$image:latest"
done
echo "==> 完成,TAG=$TAG"