Files
labelmain/build.sh
2026-03-28 17:34:59 +08:00

98 lines
2.6 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
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
HOME_PATH=$(cd ~;pwd)
# ./build.sh # stage + standalone
# ./build.sh stage # stage + standalone
# ./build.sh prod # prod + standalone
# ./build.sh -csr # stage + export
# ./build.sh prod -csr # prod + export
# ./build.sh stage -ssr # stage + standalone默认
# -------------------------
# 1. 默认环境 & 默认输出类型
# -------------------------
ENV=stage
OUTPUT_TYPE=standalone # 默认 SSRstandalone
# -------------------------
# 2. 解析参数
# -------------------------
for arg in "$@"; do
case "$arg" in
prod)
ENV=prod
;;
stage)
ENV=stage
;;
-csr)
OUTPUT_TYPE=export # 静态导出
;;
-ssr)
OUTPUT_TYPE=standalone # SSR 部署
;;
esac
done
# -------------------------
# 3. 计算 BUILD_FLAG && FLAG_PROD
# -------------------------
# build:{standalone|export}-{stage|prod}
BUILD_FLAG="build:${OUTPUT_TYPE}-${ENV}"
# {stage|prod}_latest
FLAG_PROD="${ENV}_latest"
cd $SHELL_FOLDER
# # 检查git状态
# echo -e "${GREEN}Checking git status...${NC}"
# git fetch
# git merge
# if [[ $? != 0 ]]; then echo -e "${RED}git pull error${NC}"; exit 100; fi
BRANCH_NAME=`git branch | grep "*"`
echo -e "${GREEN}Current branch: ${BRANCH_NAME/* /}${NC}"
# 安装依赖
echo -e "${GREEN}Installing dependencies...${NC}"
pnpm install
if [[ $? != 0 ]]; then echo -e "${RED}pnpm install failed${NC}"; exit 100; fi
# 执行构建
echo -e "${GREEN}Building project...${NC}"
pnpm run $BUILD_FLAG
if [[ $? != 0 ]]; then echo -e "${RED}build error${NC}"; exit 100; fi
# Docker构建和推送
MAIN_NAME=web_digitalspace_labelmain
DOCKER_FILE=$SHELL_FOLDER/Dockerfile
HARBOR_REPO=softrepo
if [[ $ENV == prod ]]; then
HARBOR_REPO=softpro
fi
if [[ $OUTPUT_TYPE == export ]]; then
DOCKER_FILE=$SHELL_FOLDER/docker/nginx.dockerfile
MAIN_NAME=web_digitalspace_labelmain_static
elif [[ $OUTPUT_TYPE == standalone ]]; then
DOCKER_FILE=$SHELL_FOLDER/docker/node.dockerfile
MAIN_NAME=web_digitalspace_labelmain_ssr
fi
echo -e "${GREEN}Building docker image...${NC}"
docker build -f $DOCKER_FILE -t $MAIN_NAME $SHELL_FOLDER
if [[ $? != 0 ]]; then echo -e "${RED}build docker with error${NC}"; exit 100; fi
echo -e "${GREEN}Tagging and pushing docker image...${NC}"
docker tag $MAIN_NAME harbor.cowarobot.cn/$HARBOR_REPO/$MAIN_NAME:$FLAG_PROD
docker push harbor.cowarobot.cn/$HARBOR_REPO/$MAIN_NAME:$FLAG_PROD
echo -e "${GREEN}Build completed successfully${NC}"