2022-06-02 17:41:11 +08:00
|
|
|
|
#bin/bash
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------
|
|
|
|
|
|
# 前后端打包编译至指定目录,即快速制作发行版
|
|
|
|
|
|
#----------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
project_path=`pwd`
|
|
|
|
|
|
# 构建后的二进制执行文件名
|
|
|
|
|
|
exec_file_name="mayfly-go"
|
|
|
|
|
|
# web项目目录
|
2024-10-31 17:24:56 +08:00
|
|
|
|
web_folder="${project_path}/frontend"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
# server目录
|
|
|
|
|
|
server_folder="${project_path}/server"
|
|
|
|
|
|
|
|
|
|
|
|
function echo_red() {
|
|
|
|
|
|
echo -e "\033[1;31m$1\033[0m"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function echo_green() {
|
|
|
|
|
|
echo -e "\033[1;32m$1\033[0m"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function echo_yellow() {
|
|
|
|
|
|
echo -e "\033[1;33m$1\033[0m"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildWeb() {
|
|
|
|
|
|
cd ${web_folder}
|
2022-06-08 10:21:02 +08:00
|
|
|
|
copy2Server=$1
|
|
|
|
|
|
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_yellow "-------------------Start bundling frontends-------------------"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
yarn run build
|
2022-08-24 20:55:42 +08:00
|
|
|
|
if [ "${copy2Server}" == "2" ] ; then
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green 'Copy the packaged static files to server/static/static'
|
2022-08-24 20:55:42 +08:00
|
|
|
|
rm -rf ${server_folder}/static/static && mkdir -p ${server_folder}/static/static && cp -r ${web_folder}/dist/* ${server_folder}/static/static
|
2022-06-08 10:21:02 +08:00
|
|
|
|
fi
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_yellow ">>>>>>>>>>>>>>>>>>>End of packaging frontend<<<<<<<<<<<<<<<<<<<<\n"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function build() {
|
|
|
|
|
|
cd ${project_path}
|
|
|
|
|
|
|
|
|
|
|
|
# 打包产物的输出目录
|
|
|
|
|
|
toFolder=$1
|
|
|
|
|
|
os=$2
|
|
|
|
|
|
arch=$3
|
2022-12-16 23:14:00 +08:00
|
|
|
|
copyDocScript=$4
|
2022-06-02 17:41:11 +08:00
|
|
|
|
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_yellow "-------------------Start a bundle build - ${os}-${arch}-------------------"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
|
|
|
|
|
|
cd ${server_folder}
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green "Package build executables..."
|
2022-06-08 10:21:02 +08:00
|
|
|
|
|
|
|
|
|
|
execFileName=${exec_file_name}
|
|
|
|
|
|
# 如果是windows系统,可执行文件需要添加.exe结尾
|
|
|
|
|
|
if [ "${os}" == "windows" ];then
|
|
|
|
|
|
execFileName="${execFileName}.exe"
|
|
|
|
|
|
fi
|
2024-01-16 20:04:04 +08:00
|
|
|
|
CGO_ENABLE=0 GOOS=${os} GOARCH=${arch} go build -ldflags=-w -o ${execFileName} main.go
|
2022-06-02 17:41:11 +08:00
|
|
|
|
|
|
|
|
|
|
if [ -d ${toFolder} ] ; then
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green "The desired folder already exists. Clear the folder"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
sudo rm -rf ${toFolder}
|
|
|
|
|
|
fi
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green "Create '${toFolder}' Directory"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
mkdir ${toFolder}
|
|
|
|
|
|
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green "Move binary to '${toFolder}'"
|
2022-06-08 10:21:02 +08:00
|
|
|
|
mv ${server_folder}/${execFileName} ${toFolder}
|
2022-06-02 17:41:11 +08:00
|
|
|
|
|
2022-12-16 23:14:00 +08:00
|
|
|
|
# if [ "${copy2Server}" == "1" ] ; then
|
|
|
|
|
|
# echo_green "拷贝前端静态页面至'${toFolder}/static'"
|
|
|
|
|
|
# mkdir -p ${toFolder}/static && cp -r ${web_folder}/dist/* ${toFolder}/static
|
|
|
|
|
|
# fi
|
|
|
|
|
|
|
|
|
|
|
|
if [ "${copyDocScript}" == "1" ] ; then
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green "Copy resources such as scripts [config.yml.example、mayfly-go.sql、mayfly-go.sqlite、readme.txt、startup.sh、shutdown.sh]"
|
2023-08-16 17:37:33 +08:00
|
|
|
|
cp ${server_folder}/config.yml.example ${toFolder}
|
2022-12-16 23:14:00 +08:00
|
|
|
|
cp ${server_folder}/readme.txt ${toFolder}
|
2024-11-21 19:02:15 +08:00
|
|
|
|
cp ${server_folder}/readme_cn.txt ${toFolder}
|
2023-10-12 21:50:55 +08:00
|
|
|
|
cp ${server_folder}/resources/script/startup.sh ${toFolder}
|
|
|
|
|
|
cp ${server_folder}/resources/script/shutdown.sh ${toFolder}
|
|
|
|
|
|
cp ${server_folder}/resources/script/sql/mayfly-go.sql ${toFolder}
|
2023-10-14 16:00:16 +08:00
|
|
|
|
cp ${server_folder}/resources/data/mayfly-go.sqlite ${toFolder}
|
2022-08-24 20:55:42 +08:00
|
|
|
|
fi
|
2022-06-02 17:41:11 +08:00
|
|
|
|
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_yellow ">>>>>>>>>>>>>>>>>>> ${os}-${arch} - Bundle build complete <<<<<<<<<<<<<<<<<<<<\n"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildLinuxAmd64() {
|
2022-08-24 20:55:42 +08:00
|
|
|
|
build "$1/mayfly-go-linux-amd64" "linux" "amd64" $2
|
2022-06-02 17:41:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildLinuxArm64() {
|
2022-08-24 20:55:42 +08:00
|
|
|
|
build "$1/mayfly-go-linux-arm64" "linux" "arm64" $2
|
2022-06-02 17:41:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildWindows() {
|
2022-08-24 20:55:42 +08:00
|
|
|
|
build "$1/mayfly-go-windows" "windows" "amd64" $2
|
2022-06-02 17:41:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-07 11:18:47 +08:00
|
|
|
|
function buildMac() {
|
|
|
|
|
|
build "$1/mayfly-go-mac" "darwin" "amd64" $2
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-16 23:14:00 +08:00
|
|
|
|
function buildDocker() {
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_yellow "-------------------Start building the docker image-------------------"
|
2022-12-16 23:14:00 +08:00
|
|
|
|
imageVersion=$1
|
2024-10-24 11:56:22 +08:00
|
|
|
|
imageName="mayfly/mayfly-go:${imageVersion}"
|
2024-10-28 12:13:41 +08:00
|
|
|
|
docker build --no-cache --platform linux/amd64 --build-arg MAYFLY_GO_VERSION="${imageVersion}" -t "${imageName}" .
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green "The docker image is built -> [${imageName}]"
|
|
|
|
|
|
echo_yellow "-------------------Finished building the docker image-------------------"
|
2022-12-16 23:14:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-15 21:28:01 +08:00
|
|
|
|
function buildxDocker() {
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_yellow "-------------------The docker buildx build image starts-------------------"
|
2023-02-15 21:28:01 +08:00
|
|
|
|
imageVersion=$1
|
2023-06-11 19:59:35 +08:00
|
|
|
|
imageName="ccr.ccs.tencentyun.com/mayfly/mayfly-go:${imageVersion}"
|
2024-10-28 12:13:41 +08:00
|
|
|
|
docker buildx build --no-cache --push --platform linux/amd64,linux/arm64 --build-arg MAYFLY_GO_VERSION="${imageVersion}" -t "${imageName}" .
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green "The docker multi-architecture version image is built -> [${imageName}]"
|
|
|
|
|
|
echo_yellow "-------------------The docker buildx image is finished-------------------"
|
2023-02-15 21:28:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-02 17:41:11 +08:00
|
|
|
|
function runBuild() {
|
2024-11-21 19:02:15 +08:00
|
|
|
|
read -p "Select build version [0 | Other->Other than docker image 1->linux-amd64 2->linux-arm64 3->windows 4->mac 5->docker 6->docker buildx]: " buildType
|
2022-12-16 23:14:00 +08:00
|
|
|
|
|
|
|
|
|
|
toPath="."
|
|
|
|
|
|
imageVersion="latest"
|
|
|
|
|
|
copyDocScript="1"
|
|
|
|
|
|
|
2023-02-15 21:28:01 +08:00
|
|
|
|
if [[ "${buildType}" != "5" ]] && [[ "${buildType}" != "6" ]] ; then
|
2022-12-16 23:14:00 +08:00
|
|
|
|
# 构建结果的目的路径
|
2024-11-21 19:02:15 +08:00
|
|
|
|
read -p "Please enter the build product output directory [default current path]: " toPath
|
2022-12-16 23:14:00 +08:00
|
|
|
|
if [ ! -d ${toPath} ] ; then
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_red "Build product output directory does not exist!"
|
2022-12-16 23:14:00 +08:00
|
|
|
|
exit;
|
|
|
|
|
|
fi
|
|
|
|
|
|
if [ "${toPath}" == "" ] ; then
|
|
|
|
|
|
toPath="."
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
2024-11-21 19:02:15 +08:00
|
|
|
|
read -p "Whether to copy documents & Scripts [0-> No 1-> Yes][Default yes]: " copyDocScript
|
2022-12-16 23:14:00 +08:00
|
|
|
|
if [ "${copyDocScript}" == "" ] ; then
|
|
|
|
|
|
copyDocScript="1"
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# 进入目标路径,并赋值全路径
|
|
|
|
|
|
cd ${toPath}
|
|
|
|
|
|
toPath=`pwd`
|
2023-08-04 12:22:21 +08:00
|
|
|
|
|
|
|
|
|
|
# read -p "是否构建前端[0|其他->否 1->是 2->构建并拷贝至server/static/static]: " runBuildWeb
|
|
|
|
|
|
runBuildWeb="2"
|
|
|
|
|
|
# 编译web前端
|
|
|
|
|
|
buildWeb ${runBuildWeb}
|
2022-06-02 17:41:11 +08:00
|
|
|
|
fi
|
2022-12-16 23:14:00 +08:00
|
|
|
|
|
2023-02-15 21:32:51 +08:00
|
|
|
|
if [[ "${buildType}" == "5" ]] || [[ "${buildType}" == "6" ]] ; then
|
2024-11-21 19:02:15 +08:00
|
|
|
|
read -p "Please enter the docker image version (default latest) : " imageVersion
|
2022-12-16 23:14:00 +08:00
|
|
|
|
|
|
|
|
|
|
if [ "${imageVersion}" == "" ] ; then
|
|
|
|
|
|
imageVersion="latest"
|
|
|
|
|
|
fi
|
2022-08-26 20:15:36 +08:00
|
|
|
|
fi
|
2022-06-02 17:41:11 +08:00
|
|
|
|
|
2022-08-24 21:36:16 +08:00
|
|
|
|
case ${buildType} in
|
|
|
|
|
|
"1")
|
2022-12-16 23:14:00 +08:00
|
|
|
|
buildLinuxAmd64 ${toPath} ${copyDocScript}
|
2022-08-24 21:36:16 +08:00
|
|
|
|
;;
|
|
|
|
|
|
"2")
|
2022-12-16 23:14:00 +08:00
|
|
|
|
buildLinuxArm64 ${toPath} ${copyDocScript}
|
2022-08-24 21:36:16 +08:00
|
|
|
|
;;
|
|
|
|
|
|
"3")
|
2022-12-16 23:14:00 +08:00
|
|
|
|
buildWindows ${toPath} ${copyDocScript}
|
2022-08-24 21:36:16 +08:00
|
|
|
|
;;
|
2022-09-07 11:18:47 +08:00
|
|
|
|
"4")
|
2022-12-16 23:14:00 +08:00
|
|
|
|
buildMac ${toPath} ${copyDocScript}
|
|
|
|
|
|
;;
|
|
|
|
|
|
"5")
|
|
|
|
|
|
buildDocker ${imageVersion}
|
2022-09-07 11:18:47 +08:00
|
|
|
|
;;
|
2023-02-15 21:28:01 +08:00
|
|
|
|
"6")
|
|
|
|
|
|
buildxDocker ${imageVersion}
|
|
|
|
|
|
;;
|
2022-08-24 21:36:16 +08:00
|
|
|
|
*)
|
2022-12-16 23:14:00 +08:00
|
|
|
|
buildLinuxAmd64 ${toPath} ${copyDocScript}
|
|
|
|
|
|
buildLinuxArm64 ${toPath} ${copyDocScript}
|
|
|
|
|
|
buildWindows ${toPath} ${copyDocScript}
|
|
|
|
|
|
buildMac ${toPath} ${copyDocScript}
|
2022-08-24 21:36:16 +08:00
|
|
|
|
;;
|
|
|
|
|
|
esac
|
2022-12-16 23:14:00 +08:00
|
|
|
|
|
2023-08-04 12:22:21 +08:00
|
|
|
|
if [[ "${buildType}" != "5" ]] && [[ "${buildType}" != "6" ]] ; then
|
2024-11-21 19:02:15 +08:00
|
|
|
|
echo_green "Delete static assets under ['${server_folder}/static/static']."
|
2023-08-04 12:22:21 +08:00
|
|
|
|
# 删除静态资源文件,保留一个favicon.ico,否则后端启动会报错
|
|
|
|
|
|
rm -rf ${server_folder}/static/static/assets
|
|
|
|
|
|
rm -rf ${server_folder}/static/static/config.js
|
|
|
|
|
|
rm -rf ${server_folder}/static/static/index.html
|
|
|
|
|
|
fi
|
2022-06-02 17:41:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-20 20:34:05 +08:00
|
|
|
|
runBuild
|