This commit is contained in:
2025-12-03 21:40:23 +08:00
parent 904bc54248
commit 238589b82e

View File

@@ -711,12 +711,27 @@ download_binary_from_releases() {
# 解析 release 对应的 commit hashtarget_commitish
local release_commit=$(echo "$release_detail" | grep -o '"target_commitish":"[^"]*"' | head -1 | cut -d'"' -f4)
# 如果 release_detail 中没有 target_commitish尝试通过 tag 获取 commit hash
if [ -z "$release_commit" ]; then
# 如果 target_commitish 是分支名(如 "main"),需要通过 API 获取该分支的 commit hash
if [ -n "$release_commit" ] && [ "${#release_commit}" -lt 40 ]; then
# 可能是分支名,尝试获取分支的最新 commit hash
local branch_info=$(curl -s -X GET "${repo_api}/git/commits/${release_commit}" 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$branch_info" ]; then
local branch_commit=$(echo "$branch_info" | grep -o '"sha":"[^"]*"' | head -1 | cut -d'"' -f4)
if [ -n "$branch_commit" ] && [ "${#branch_commit}" -eq 40 ]; then
release_commit="$branch_commit"
fi
fi
fi
# 如果 release_detail 中没有 target_commitish 或获取失败,尝试通过 tag 获取 commit hash
if [ -z "$release_commit" ] || [ "${#release_commit}" -lt 40 ]; then
# 通过 tag API 获取 commit hash
local tag_info=$(curl -s -X GET "${repo_api}/git/tags/${latest_tag}" 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$tag_info" ]; then
release_commit=$(echo "$tag_info" | grep -o '"sha":"[^"]*"' | head -1 | cut -d'"' -f4)
local tag_commit=$(echo "$tag_info" | grep -o '"sha":"[^"]*"' | head -1 | cut -d'"' -f4)
if [ -n "$tag_commit" ] && [ "${#tag_commit}" -eq 40 ]; then
release_commit="$tag_commit"
fi
fi
fi
@@ -878,30 +893,54 @@ download_binary_from_releases() {
# 对比 Git commit hash
if [ -n "$release_commit" ]; then
echo -e "${BLUE}验证 Git commit 版本...${NC}"
cd "$SOURCE_DIR"
# 获取当前分支的最新 commit hash
local current_commit=$(git rev-parse HEAD 2>/dev/null || echo "")
# 切换到源码目录(如果存在)
if [ -d "$SOURCE_DIR" ] && [ -d "$SOURCE_DIR/.git" ]; then
cd "$SOURCE_DIR" || {
echo -e "${YELLOW}⚠ 无法切换到源码目录,跳过验证${NC}"
cd /tmp || true
}
if [ -n "$current_commit" ]; then
# 截取短 commit hash 用于显示前7位
local release_commit_short=$(echo "$release_commit" | cut -c1-7)
local current_commit_short=$(echo "$current_commit" | cut -c1-7)
# 获取当前分支的最新 commit hash
local current_commit=$(git rev-parse HEAD 2>/dev/null || echo "")
echo -e "${BLUE} Release commit: ${release_commit_short}${NC}"
echo -e "${BLUE} 当前代码 commit: ${current_commit_short}${NC}"
if [ -n "$current_commit" ]; then
# 截取短 commit hash 用于显示前7位
local release_commit_short=""
local current_commit_short=$(echo "$current_commit" | cut -c1-7)
# 对比 commit hash
if [ "$release_commit" != "$current_commit" ]; then
echo -e "${YELLOW}⚠ Commit hash 不匹配,二进制文件可能不是最新代码编译的${NC}"
echo -e "${YELLOW} Release 基于较旧的代码,将使用源码编译最新版本${NC}"
rm -rf "$temp_dir"
return 1
if [ "${#release_commit}" -eq 40 ]; then
release_commit_short=$(echo "$release_commit" | cut -c1-7)
else
release_commit_short="$release_commit"
fi
echo -e "${BLUE} Release commit: ${release_commit_short}${NC}"
echo -e "${BLUE} 当前代码 commit: ${current_commit_short}${NC}"
# 对比 commit hash只有当 release_commit 是完整的 commit hash 时才对比)
if [ "${#release_commit}" -eq 40 ] && [ "${#current_commit}" -eq 40 ]; then
if [ "$release_commit" != "$current_commit" ]; then
echo -e "${YELLOW}⚠ Commit hash 不匹配,二进制文件可能不是最新代码编译的${NC}"
echo -e "${YELLOW} Release 基于较旧的代码,将使用源码编译最新版本${NC}"
# 保留已克隆的仓库目录,供 build_from_source 复用
cd /tmp || true
rm -rf "$temp_dir"
return 1
else
echo -e "${GREEN}✓ Commit hash 匹配,二进制文件是最新代码编译的${NC}"
fi
else
echo -e "${YELLOW}⚠ 无法获取有效的 commit hash 进行对比,跳过验证${NC}"
fi
else
echo -e "${GREEN}✓ Commit hash 匹配,二进制文件是最新代码编译的${NC}"
echo -e "${YELLOW}⚠ 无法获取当前代码的 commit hash跳过验证${NC}"
fi
# 返回临时目录
cd /tmp || true
else
echo -e "${YELLOW}无法获取当前代码的 commit hash,跳过验证${NC}"
echo -e "${YELLOW}源码目录不存在,跳过验证${NC}"
fi
else
echo -e "${YELLOW}⚠ 无法获取 release 的 commit hash跳过验证${NC}"
@@ -976,19 +1015,30 @@ build_from_source() {
exit 1
fi
# 如果源码目录已存在,删除(卸载函数应该已经删除,这里作为保险
if [ -d "$SOURCE_DIR" ]; then
echo -e "${YELLOW}清理旧的源码目录...${NC}"
sudo rm -rf "$SOURCE_DIR"
fi
# 检查源码目录是否已存在(可能由 download_binary_from_releases 已克隆
if [ -d "$SOURCE_DIR" ] && [ -d "$SOURCE_DIR/.git" ]; then
echo -e "${BLUE}检测到已存在的仓库目录,复用现有目录...${NC}"
cd "$SOURCE_DIR"
# 确保是最新代码
echo -e "${BLUE}更新代码到最新版本...${NC}"
sudo git fetch origin 2>/dev/null || true
sudo git checkout "${GITHUB_BRANCH}" 2>/dev/null || true
sudo git pull origin "${GITHUB_BRANCH}" 2>/dev/null || true
else
# 如果源码目录已存在但不是 git 仓库,删除它
if [ -d "$SOURCE_DIR" ]; then
echo -e "${YELLOW}清理旧的源码目录...${NC}"
sudo rm -rf "$SOURCE_DIR"
fi
# 克隆仓库到源码目录
echo -e "${BLUE}克隆仓库到 ${SOURCE_DIR}...${NC}"
if ! sudo git clone --branch "${GITHUB_BRANCH}" "https://gitee.nas.cpolar.cn/${GITHUB_REPO}.git" "$SOURCE_DIR" 2>&1; then
echo -e "${RED}克隆仓库失败,请检查网络连接和仓库地址${NC}"
echo -e "${YELLOW}仓库地址: https://gitee.nas.cpolar.cn/${GITHUB_REPO}.git${NC}"
show_build_alternatives
exit 1
# 克隆仓库到源码目录
echo -e "${BLUE}克隆仓库到 ${SOURCE_DIR}...${NC}"
if ! sudo git clone --branch "${GITHUB_BRANCH}" "https://gitee.nas.cpolar.cn/${GITHUB_REPO}.git" "$SOURCE_DIR" 2>&1; then
echo -e "${RED}克隆仓库失败,请检查网络连接和仓库地址${NC}"
echo -e "${YELLOW}仓库地址: https://gitee.nas.cpolar.cn/${GITHUB_REPO}.git${NC}"
show_build_alternatives
exit 1
fi
fi
# 设置目录权限(允许当前用户访问,但服务运行时是 root