build.sh 7.44 KB
#!/bin/bash

# 功能说明:把jar包打成tar.gz包并上传到阿里云存储

# 远程打包的配置
PASSWORD="vion2025"

# 定义模块配置:目录名->webhook URL
declare -A MODULE_CONFIG
MODULE_CONFIG["matchPerson"]="http://192.168.1.64:9505/api/apis/deploy/26/branch/"
#MODULE_CONFIG["VVAS-DataCenter-process"]="http://192.168.1.64:9505/api/apis/deploy/9/branch/"
#MODULE_CONFIG["VVAS-DataCenter-dbSave"]="http://192.168.1.64:9505/api/apis/deploy/10/branch/"

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 打印带颜色的文本
print_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 显示菜单
show_menu() {
    echo "=================================="
    echo "本工具使用当前目录git的分支及已提交的最新commitId来打包!"
    echo "如果需要根据历史版本打包,请访问界面手动选择commitID打包: http://192.168.1.64:9505/deploy/request "
    echo "请选择打包方式:"
    echo "1) 远程打包(默认),使用192.168.1.64服务器打包,速度更快"
    echo "2) 本地打包"
    echo "=================================="
    read -p "请输入选项 (1 或 2, 默认为 1): " choice
    choice=${choice:-1}
}

# 获取当前分支名
get_current_branch() {
    git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown"
}

# 获取最新提交ID
get_commit_id() {
    git rev-parse HEAD 2>/dev/null | cut -c1-8 || echo "unknown"
}

# 获取模块列表
get_module_list() {
    local modules=()
    for module in "${!MODULE_CONFIG[@]}"; do
        modules+=("$module")
    done
    echo "${modules[@]}"
}

# 获取 Git 信息
BRANCH_NAME=$(get_current_branch)
COMMIT_ID=$(get_commit_id)

# 本地打包指定模块
local_build_module() {
    local module_dir=$1
    local webhook_url=${MODULE_CONFIG[$module_dir]}

    # 检查模块是否在配置中
    if [ -z "$webhook_url" ]; then
        print_error "未知模块: $module_dir"
        return 1
    fi

    print_info "开始本地打包模块: $module_dir"

    # 切换到模块目录
    #local module_path="$SCRIPT_DIR$module_dir"
    local module_path="$(dirname "$SCRIPT_DIR")"
    if [ ! -d "$module_path" ]; then
        print_error "模块目录不存在: $module_path"
        return 1
    fi

    cd "$module_path" || return 1
    print_info "当前目录: $(pwd)"

    # 复制 ../build/bin 目录到当前目录
    print_info "复制 ../build/bin 目录到当前目录..."
    if [ -d "../build/bin" ]; then
        cp -r ../build/bin ./build/
        print_info "复制完成"
    else
        print_error "../build/bin 目录不存在"
        cd "$SCRIPT_DIR" || return 1
        return 1
    fi
    
    if [ -f "../build/package.sh" ]; then
        print_info "执行 ../build/package.sh..."
        chmod +x ../build/package.sh
        ../build/package.sh
        if [ $? -eq 0 ]; then
            print_info "模块 $module_dir 本地打包完成"
            rm -fr build/bin
        else
            print_error "模块 $module_dir 本地打包失败"+
            rm -fr build/bin
            cd "$SCRIPT_DIR" || return 1
            return 1
        fi
    else
        print_error "../build/package.sh 文件不存在"
        rm -fr build/bin
        cd "$SCRIPT_DIR" || return 1
        return 1
    fi
    
    # 返回脚本所在目录
    cd "$SCRIPT_DIR" || return 1
}

# 远程打包指定模块
remote_build_module() {
    local module_dir=$1
    local webhook_url=${MODULE_CONFIG[$module_dir]}
    
    # 检查模块是否在配置中
    if [ -z "$webhook_url" ]; then
        print_error "未知模块: $module_dir"
        return 1
    fi
    
    print_info "开始远程打包模块: $module_dir"
    
    # 切换到模块目录
    #local module_path="$SCRIPT_DIR/$module_dir"
    local module_path="$(dirname "$SCRIPT_DIR")"
    if [ ! -d "$module_path" ]; then
        print_error "模块目录不存在: $module_path"
        return 1
    fi
    
    cd "$module_path" || return 1
    print_info "当前目录: $(pwd)"
    
    # Webhook URL 和参数
    WEBHOOK_URL="${webhook_url}?name=${BRANCH_NAME}&token=${PASSWORD}"

    print_info "当前分支: $BRANCH_NAME"
    print_info "提交ID: $COMMIT_ID"

    # 模拟 GitLab webhook 数据
    TIMESTAMP=$(date -Iseconds)
    PAYLOAD=$(cat <<EOF
{
  "object_kind": "push",
  "ref": "refs/heads/$BRANCH_NAME",
  "before": "",
  "after": "$COMMIT_ID",
  "repository": {
    "name": "$module_dir",
    "url": "",
    "description": "",
    "homepage": "",
    "git_http_url": "",
    "git_ssh_url": "",
    "visibility_level": 0
  },
  "commits": [
    {
      "id": "$COMMIT_ID",
      "message": "autoBuild",
      "timestamp": "$TIMESTAMP",
      "url": "",
      "author": {
        "name": "build-script",
        "email": "build@script.local"
      }
    }
  ],
  "total_commits_count": 1
}
EOF
)
    
    # 发送请求
    print_info "调用 webhook: $WEBHOOK_URL"
    RESPONSE=$(curl -s -w "%{http_code}" -X POST \
        -H "Content-Type: application/json" \
        -d "$PAYLOAD" \
        "$WEBHOOK_URL")
    
    HTTP_CODE=${RESPONSE: -3}

    if [ "$HTTP_CODE" -eq 202 ]; then
        print_info "模块 $module_dir 远程打包触发成功,正在打包,请稍等,预计30秒后打包成功!"
    elif [ "$HTTP_CODE" -eq 200 ] ; then
        print_warn "响应内容: ${RESPONSE%???}"
        print_warn "模块 $module_dir 打包返回内容,可能有异常: $HTTP_CODE"
    else
        print_error "模块 $module_dir 远程打包触发失败,HTTP状态码: $HTTP_CODE"
        print_error "响应内容: ${RESPONSE%???}"
        cd "$SCRIPT_DIR" || return 1
        return 1
    fi
    print_info "模块 $module_dir 打包上传成功!";
    
    # 返回脚本所在目录
    cd "$SCRIPT_DIR" || return 1
}

# 本地打包所有模块
local_build() {
    print_info "开始本地打包所有模块..."
    
    # 获取模块列表
    local modules
    read -ra modules <<< "$(get_module_list)"
    
    for module in "${modules[@]}"; do
        if ! local_build_module "$module"; then
            print_error "模块 $module 本地打包失败"
            return 1
        fi
        echo ""
    done

    print_info "所有模块打包上传成功,可访问地址进行部署发布: xxxx";
    print_info "部署版本号:${BRANCH_NAME}-${COMMIT_ID:0:6}"
}

# 远程打包所有模块
remote_build() {
    print_info "开始远程打包所有模块..."
    
    # 获取模块列表
    local modules
    read -ra modules <<< "$(get_module_list)"
    
    for module in "${modules[@]}"; do
        if ! remote_build_module "$module"; then
            print_error "模块 $module 远程打包失败"
            return 1
        fi
        echo ""
    done
    
    print_info "所有模块正在打包,预计30秒后打包成功,可访问地址进行部署发布: xxxx";
    print_info "查看打包进度:http://192.168.1.64:9505/deploy/request";
    print_info "部署版本号:${BRANCH_NAME}-${COMMIT_ID:0:6}"
}

# 主程序
main() {
    print_info "构建脚本,目录${SCRIPT_DIR}"
    
    show_menu
    
    case $choice in
        1)
            remote_build
            ;;
        2)
            local_build
            ;;
        *)
            print_warn "无效选项,使用默认远程打包"
            remote_build
            ;;
    esac
}

# 执行主程序
main "$@"