diff --git a/.env.versions b/.env.versions index 812c374..6391475 100644 --- a/.env.versions +++ b/.env.versions @@ -1,5 +1,5 @@ # 本项目版本 -PROJECT_VERSION=0.7.3 +PROJECT_VERSION=0.8.0 #=== start === gui-base images ========================== @@ -7,10 +7,10 @@ PROJECT_VERSION=0.7.3 GUI_BASE_VERSION=0.4.0 # `stainless403/mdcx-builtin-gui-base`镜像版本 -GUI_BASE_MDCX_BUILTIN_VERSION=120240220 +GUI_BASE_MDCX_BUILTIN_VERSION=d20240527 # `stainless403/mdcx-src-gui-base`镜像版本 -GUI_BASE_MDCX_SRC_VERSION=0.2.1 +GUI_BASE_MDCX_SRC_VERSION=0.3.0 #=== end === gui-base images ========================== @@ -21,10 +21,10 @@ GUI_BASE_MDCX_SRC_VERSION=0.2.1 WEBTOP_BASE_VERSION=0.1.2 # `stainless403/mdcx-builtin-webtop-base`镜像版本 -WEBTOP_BASE_MDCX_BUILTIN_VERSION=120240220 +WEBTOP_BASE_MDCX_BUILTIN_VERSION=d20240527 # `stainless403/mdcx-src-webtop-base`镜像版本 -WEBTOP_BASE_MDCX_SRC_VERSION=0.2.1 +WEBTOP_BASE_MDCX_SRC_VERSION=0.3.0 #=== end === webtop-base images ========================== @@ -35,10 +35,10 @@ WEBTOP_BASE_MDCX_SRC_VERSION=0.2.1 BUILD_MDCX_BASE_VERSION=0.4.0 # `stainless403/build-mdcx`镜像版本 -BUILD_MDCX_VERSION=120240220 +BUILD_MDCX_VERSION=d20240527 #=== end === build-mdcx images ========================== # MDCx最新版本 -MDCX_APP_VERSION=120240220 \ No newline at end of file +MDCX_APP_VERSION=d20240527 \ No newline at end of file diff --git a/.github/workflows/watch-mdcx.yml b/.github/workflows/watch-mdcx.yml index 1a5c687..93f62ed 100644 --- a/.github/workflows/watch-mdcx.yml +++ b/.github/workflows/watch-mdcx.yml @@ -58,50 +58,33 @@ jobs: run: | source scripts/base.sh source scripts/github.sh + source scripts/release-utils.sh VAR_VERSION="MDCX_LATEST_VERSION" VAR_TIME="MDCX_LATEST_TIME" - # 获取最新版本信息,返回值为数组,第一个元素为版本号,第二个元素为更新时间 - getNewInfo() { - local _content=$(curl -s "${{ env.RELEASE_URL }}") - - # tag名称,作为版本号 - local tagName=$(printf '%s' $_content | jq -r ".tag_name") - local archiveVersion=$(echo $tagName | sed 's/v//g') - - if [[ -z "$archiveVersion" || "$archiveVersion" == "null" ]]; then - echo "❌ 获取版本号失败!" - return 1 - fi - - # 源码压缩包(tar格式)链接 - local archiveUrl=$(printf '%s' $_content | jq -r ".tarball_url") + REPO="sqzw-x/mdcx" + TAG_NAME="daily_release" - if [[ -z "$archiveUrl" ]]; then - echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" - echo "🔘 请求链接:$_url" - echo "🔘 请求结果:$_content" - exit 1 - fi + REPO="sqzw-x/mdcx" + TAG_NAME="daily_release" - # https://gitbook.curiouser.top/origin/linux-jq.html#%E4%B8%89%E3%80%81jq%E5%91%BD%E4%BB%A4%E5%8F%82%E6%95%B0 - # -r 如果过滤的结果是一个字符串,那么直接写到标准输出(去掉字符串的引号); - local updatedAt=$(printf '%s' $_content | jq -r '.updated_at') - # 如果没有`updated_at`,则取`published_at` - if [[ -z "$updatedAt" || "$updatedAt" == "null" ]]; then - updatedAt=$(printf '%s' $_content | jq -r '.published_at') - fi + info=$(get_release_info "$REPO" "$TAG_NAME") + if [[ $? -ne 0 ]]; then + echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!" + exit 1 + else + echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!" + fi + echo $info | jq - if [[ -z "$updatedAt" || "$updatedAt" == "null" ]]; then - echo "❌ 获取更新时间失败!" - return 1 - fi + # 发布时间 + published_at=$(printf '%s' $info | jq -r ".published_at") + echo "📅 发布时间: $published_at" - local info=("$archiveVersion" "$updatedAt") - echo "${info[@]}" - return 0 - } + # 版本号 + release_version=$(printf '%s' $info | jq -r ".release_version") + echo "🔢 版本号: $release_version" newVersion="" newTime="" @@ -109,8 +92,8 @@ jobs: # 获取最新版本信息 info=($(getNewInfo)) - newVersion=${info[0]} - newTime=${info[1]} + newVersion="$release_version" + newTime="$published_at" # 获取已记录的版本信息 latestVersion=$(getVariable $VAR_VERSION) diff --git a/README.md b/README.md index e83638c..2bbcf98 100644 --- a/README.md +++ b/README.md @@ -132,4 +132,9 @@ docker run -d --name mdcx \ ## FAQ -请查看 [FAQ](https://github.com/northsea4/mdcx-docker/blob/main/FAQ.md) \ No newline at end of file +请查看 [FAQ](https://github.com/northsea4/mdcx-docker/blob/main/FAQ.md) + + +## TODO +- [x] 上游源码采用了daily release自动发布,看能不能跟随 +- [ ] 一键脚本github镜像加速 \ No newline at end of file diff --git a/gui-base/update-src.sh b/gui-base/update-src.sh index adf6cc3..6cd0c6f 100755 --- a/gui-base/update-src.sh +++ b/gui-base/update-src.sh @@ -1,5 +1,14 @@ #!/bin/bash +# 脚本说明: 当使用源码部署时,使用该脚本自动完成更新源码的处理。 + +# DEV: +# - gui-base/update-src.sh +# - webtop-base/update-src.sh +# - webtop-base/rootfs-src/app-assets/scripts/update-src.sh +# 除了.env提示不同外,其余部分基本相同。 +# webtop-base/rootfs-src/app-assets/scripts/update-src.sh 中 appPath=/app; 没有restart容器处理。 + if [ ! -f ".env" ]; then echo "⚠️ 当前目录缺少文件 .env。示例文件:https://github.com/northsea4/mdcx-docker/blob/main/gui-base/.env.sample" # exit 1 @@ -40,10 +49,6 @@ do shift shift ;; - --force) - force=1 - shift - ;; --dry) dry=1 shift @@ -66,184 +71,207 @@ done if [ -n "$help" ]; then echo "脚本功能:更新自部署的应用源码" echo "" - echo "示例-检查并更新: ./update-src.sh" + echo "示例-检查并更新: $0" echo "" echo "参数说明:" echo "--restart 更新后重启容器,默认false。可选参数值: 1, 0; true, false" - echo "--force 强制更新。默认情况下当已发布版本较新于本地版本时才会更新。" echo "--dry 只检查,不更新" echo "-h, --help 显示帮助信息" exit 0 fi -compareVersion () { - if [[ $1 == $2 ]] - then - return 0 - fi - local IFS=. - local i ver1=($1) ver2=($2) - # fill empty fields in ver1 with zeros - for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) - do - ver1[i]=0 - done - for ((i=0; i<${#ver1[@]}; i++)) - do - if [[ -z ${ver2[i]} ]] - then - # fill empty fields in ver2 with zeros - ver2[i]=0 - fi - if ((10#${ver1[i]} > 10#${ver2[i]})) - then - return 1 +generate_app_version() { + local published_at="$1" + + # 去除非数字字符 + published_at=$(echo "$published_at" | tr -dc '0-9') + + # 取前8位数字作为年月日,前缀为d + echo "d${published_at:0:8}" +} + +find_release_by_tag_name() { + local repo=$1 + local target_tag_name=$2 + + local url="https://api.github.com/repos/${repo}/releases" + + # echo "URL: $url" + + local target_release="" + + let found=false + local page=1 + while true; do + local response=$(curl -s "${url}?per_page=100&page=${page}") + if [[ -z "$response" ]]; then + break fi - if ((10#${ver1[i]} < 10#${ver2[i]})) - then - return 2 + + local releases=$(printf '%s' $response | jq -c '.[]') + for release in $releases; do + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ "$tag_name" == "$target_tag_name" ]]; then + found=true + echo $release + break + fi + done + + if [[ $found ]]; then + break fi + + page=$((page + 1)) done - return 0 } -# 从`appPath/config.ini.default`获取应用版本 -# [modified_time] -# modified_time = 2023-12-19 23:53:41 -# version = 120231219 -getAppVersionFromConfig () { - local configPath="$1" - if [[ -f "$configPath" ]]; then - local version=$(cat $configPath | grep -oi 'version\s*=\s*[0-9]\+' | grep -oi '[0-9]\+$') - echo $version - else - echo 0 +# 获取指定仓库和tag_name的release,并解析得到release信息 +# 返回json对象: +# { +# "tag_name": "v1.0.0", +# "published_at": "2022-01-01T00:00:00Z", +# "release_version": "120220101", +# "tar_url": "https://api.github.com/repos/sqzw-x/mdcx/tarball/daily_release", +# "zip_url": "https://api.github.com/repos/sqzw-x/mdcx/zipball/daily_release" +# } +get_release_info() { + local repo="$1" + local tag_name="$2" + + # echo "⏳ 正在获取仓库 ${repo} 中 tag_name=${tag_name} 的release..." + local release=$(find_release_by_tag_name "$repo" "$tag_name") + + if [[ -z "$release" ]]; then + echo "❌ 找不到 tag_name=${tag_name} 的release!" + return 1 + fi + + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ -z "$tag_name" ]]; then + echo "❌ 找不到 tag_name!" + return 1 + fi + + published_at=$(printf '%s' $release | jq -r '.published_at') + if [[ -z "$published_at" ]]; then + echo "❌ 找不到 published_at!" + return 1 + fi + + release_version=$(generate_app_version "$published_at") + + tar_url=$(printf '%s' $release | jq -r '.tarball_url') + if [[ -z "$tar_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 + fi + + zip_url=$(printf '%s' $release | jq -r '.zipball_url') + if [[ -z "$zip_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 fi + + # 构建一个json对象 + local data="{ + \"tag_name\": \"${tag_name}\", + \"published_at\": \"${published_at}\", + \"release_version\": \"${release_version}\", + \"tar_url\": \"${tar_url}\", + \"zip_url\": \"${zip_url}\" + }" + echo $data + return 0 } appPath=$(echo "$appPath" | sed 's:/*$::') -isEmpty=0 if [[ -n "${appPath}" ]]; then if [[ ! -d "${appPath}" ]]; then echo "⚠️ $appPath 不存在,现在创建" mkdir -p $appPath else - appConfigPath="$appPath/config.ini.default" - appVersion=$(getAppVersionFromConfig "$appConfigPath") - if [[ $appVersion == 0 ]]; then - isEmpty=1 - echo "ℹ️ 本地应用版本: $appVersion" - else - echo "ℹ️ 从 $appConfigPath 检测到应用版本为 $appVersion" - fi + echo "✅ $appPath 已经存在" fi else - echo "❌ 应用源码目录不能为空!" + echo "❌ 应用源码目录参数不能为空!" exit 1 fi -_url="https://api.github.com/repos/sqzw-x/mdcx/releases/latest" -_content=$(curl -s "$_url") +REPO="sqzw-x/mdcx" +TAG_NAME="daily_release" -# TODO github workflow里竟然会有比较大的概率获取失败 -if [[ -z "$_content" ]]; then - echo "❌ 请求 $_url 失败!" +info=$(get_release_info "$REPO" "$TAG_NAME") +if [[ $? -ne 0 ]]; then + echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!" exit 1 +else + echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!" fi +echo $info | jq +# exit 0 -# tag名称,作为版本号 -tagName=$(printf '%s' $_content | jq -r ".tag_name") -archiveVersion=$(echo $tagName | sed 's/v//g') - -# 源码压缩包(tar格式)链接 -archiveUrl=$(printf '%s' $_content | jq -r ".tarball_url") - -if [[ -z "$archiveUrl" ]]; then - echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" - echo "🔘 请求链接:$_url" - exit 1 -fi +# 发布时间 +published_at=$(printf '%s' $info | jq -r ".published_at") +echo "📅 发布时间: $published_at" -if [[ -n "$verbose" ]]; then - echo "ℹ️ TAG名称: $tagName" - echo "🔗 下载链接: $archiveUrl" -fi -echo "ℹ️ 已发布版本: $archiveVersion" +# 版本号 +release_version=$(printf '%s' $info | jq -r ".release_version") +echo "🔢 版本号: $release_version" -# exit +# 源码链接 +file_url=$(printf '%s' $info | jq -r ".tar_url") +echo "🔗 下载链接: $file_url" -compareVersion $archiveVersion $appVersion -case $? in - 0) op='=';; - 1) op='>';; - 2) op='<';; -esac -shouldUpdate= -if [[ $op == '>' ]]; then - echo "🆕 已发布的最新版本 较新于 本地版本" - shouldUpdate=1 +if [[ -z "$file_url" ]]; then + echo "❌ 从请求结果获取下载链接失败!" + exit 1 fi -if [[ -n "$force" ]]; then - echo "ℹ️ 强制更新" - shouldUpdate=1 +if [[ -n "$dry" ]]; then + exit 0 fi -if [[ -n "$shouldUpdate" ]]; then - - if [[ -n "$dry" ]]; then - exit 0 - fi +file_path="$release_version.tar.gz" - archivePath="$archiveVersion.tar.gz" - - if [[ -n "$verbose" ]]; then - curl -o $archivePath $archiveUrl -L - else - curl -so $archivePath $archiveUrl -L - fi +if [[ -n "$verbose" ]]; then + curl -o $file_path $file_url -L +else + curl -so $file_path $file_url -L +fi - echo "✅ 下载成功" - echo "⏳ 开始解压..." +echo "✅ 下载成功" +echo "⏳ 开始解压..." - # 解压新的源码到app目录 - tar -zxvf $archivePath -C $appPath --strip-components 1 - # 删除压缩包 - rm -f $archivePath - echo "✅ 源码已覆盖到 $appPath" +# 解压新的源码到app目录 +tar -zxvf $file_path -C $appPath --strip-components 1 +# 删除压缩包 +rm -f $file_path +echo "✅ 源码已覆盖到 $appPath" - if [ -f ".env.versions" ]; then - echo "✅ 更新 .env.versions MDCX_APP_VERSION=$archiveVersion" - sed -i -e "s/MDCX_APP_VERSION=[0-9.]\+/MDCX_APP_VERSION=$archiveVersion/" .env.versions - fi +if [ -f ".env.versions" ]; then + echo "✅ 更新 .env.versions MDCX_APP_VERSION=$release_version" + sed -i -e "s/MDCX_APP_VERSION=[0-9.]\+/MDCX_APP_VERSION=$release_version/" .env.versions +fi - if [ -f ".env" ]; then - echo "✅ 更新 .env APP_VERSION=$archiveVersion" - sed -i -e "s/APP_VERSION=[0-9.]\+/APP_VERSION=$archiveVersion/" .env - fi +if [ -f ".env" ]; then + echo "✅ 更新 .env APP_VERSION=$release_version" + sed -i -e "s/APP_VERSION=[0-9.]\+/APP_VERSION=$release_version/" .env +fi - echo "ℹ️ 删除标记文件 $appPath/$FILE_INITIALIZED" - rm -f "$appPath/$FILE_INITIALIZED" +echo "ℹ️ 删除标记文件 $appPath/$FILE_INITIALIZED" +rm -f "$appPath/$FILE_INITIALIZED" - if [[ -n "MDCX_SRC_CONTAINER_NAME" ]]; then - if [[ "$restart" == "1" || "$restart" == "true" ]]; then - echo "⏳ 重启容器..." - docker restart $MDCX_SRC_CONTAINER_NAME - else - echo "ℹ️ 如果已经部署过容器,执行以下命令重启容器" - echo "docker restart $MDCX_SRC_CONTAINER_NAME" - fi - fi -else - if [[ $op == '<' ]]; then - echo "ℹ️ 本地版本 较新于 已发布的最新版本" +if [[ -n "MDCX_SRC_CONTAINER_NAME" ]]; then + if [[ "$restart" == "1" || "$restart" == "true" ]]; then + echo "⏳ 重启容器..." + docker restart $MDCX_SRC_CONTAINER_NAME else - echo "ℹ️ 本地版本 已是最新版本" + echo "ℹ️ 如果已经部署过容器,执行以下命令重启容器" + echo "docker restart $MDCX_SRC_CONTAINER_NAME" fi fi -if [ -n "$GITHUB_ACTIONS" ]; then - echo "APP_VERSION=$archiveVersion" >> $GITHUB_OUTPUT -fi \ No newline at end of file +echo "🎉 Enjoy~" \ No newline at end of file diff --git a/install.sh b/install.sh index 98cd3f1..81c7d51 100755 --- a/install.sh +++ b/install.sh @@ -297,39 +297,143 @@ else echo "❗ 你没有指定映射影片目录,你可以之后在docker-compose.yml中手动添加。" fi -downloadSrc() { - local _url="https://api.github.com/repos/sqzw-x/mdcx/releases/latest" - local _content=$(curl -s "$_url") +generate_app_version() { + local published_at="$1" - # TODO github workflow里竟然会有比较大的概率获取失败 - if [[ -z "$_content" ]]; then - echo "❌ 请求 $_url 失败!" - exit 1 + # 去除非数字字符 + published_at=$(echo "$published_at" | tr -dc '0-9') + + # 取前8位数字作为年月日,前缀为d + echo "d${published_at:0:8}" +} + +find_release_by_tag_name() { + local repo=$1 + local target_tag_name=$2 + + local url="https://api.github.com/repos/${repo}/releases" + + # echo "URL: $url" + + local target_release="" + + let found=false + local page=1 + while true; do + local response=$(curl -s "${url}?per_page=100&page=${page}") + if [[ -z "$response" ]]; then + break + fi + + local releases=$(printf '%s' $response | jq -c '.[]') + for release in $releases; do + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ "$tag_name" == "$target_tag_name" ]]; then + found=true + echo $release + break + fi + done + + if [[ $found ]]; then + break + fi + + page=$((page + 1)) + done +} + +# 获取指定仓库和tag_name的release,并解析得到release信息 +# 返回json对象: +# { +# "tag_name": "v1.0.0", +# "published_at": "2022-01-01T00:00:00Z", +# "release_version": "120220101", +# "tar_url": "https://api.github.com/repos/sqzw-x/mdcx/tarball/daily_release", +# "zip_url": "https://api.github.com/repos/sqzw-x/mdcx/zipball/daily_release" +# } +get_release_info() { + local repo="$1" + local tag_name="$2" + + # echo "⏳ 正在获取仓库 ${repo} 中 tag_name=${tag_name} 的release..." + local release=$(find_release_by_tag_name "$repo" "$tag_name") + + if [[ -z "$release" ]]; then + echo "❌ 找不到 tag_name=${tag_name} 的release!" + return 1 fi - # tag名称,作为版本号 - tagName=$(printf '%s' $_content | jq -r ".tag_name") - archiveVersion=$(echo $tagName | sed 's/v//g') + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ -z "$tag_name" ]]; then + echo "❌ 找不到 tag_name!" + return 1 + fi - # 源码压缩包(tar格式)链接 - archiveUrl=$(printf '%s' $_content | jq -r ".tarball_url") + published_at=$(printf '%s' $release | jq -r '.published_at') + if [[ -z "$published_at" ]]; then + echo "❌ 找不到 published_at!" + return 1 + fi + + release_version=$(generate_app_version "$published_at") + + tar_url=$(printf '%s' $release | jq -r '.tarball_url') + if [[ -z "$tar_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 + fi - if [[ -z "$archiveUrl" ]]; then + zip_url=$(printf '%s' $release | jq -r '.zipball_url') + if [[ -z "$zip_url" ]]; then echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" - echo "🔘 请求链接:$_url" - - on_error "${DIR_FULL_PATH}" + return 1 fi - if [[ -n "$verbose" ]]; then - echo "ℹ️ TAG名称: $tagName" - echo "🔗 下载链接: $archiveUrl" + # 构建一个json对象 + local data="{ + \"tag_name\": \"${tag_name}\", + \"published_at\": \"${published_at}\", + \"release_version\": \"${release_version}\", + \"tar_url\": \"${tar_url}\", + \"zip_url\": \"${zip_url}\" + }" + echo $data + return 0 +} + +download_src() { + local REPO="sqzw-x/mdcx" + local TAG_NAME="daily_release" + + local info=$(get_release_info "$REPO" "$TAG_NAME") + if [[ $? -ne 0 ]]; then + echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!" + exit 1 + else + echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!" fi - echo "ℹ️ 已发布版本: $archiveVersion" + echo $info | jq + + # 发布时间 + local published_at=$(printf '%s' $info | jq -r ".published_at") + echo "📅 发布时间: $published_at" + + # 版本号 + local release_version=$(printf '%s' $info | jq -r ".release_version") + echo "🔢 版本号: $release_version" - archivePath="$archiveVersion.tar.gz" + # 源码链接 + local file_url=$(printf '%s' $info | jq -r ".tar_url") + echo "🔗 下载链接: $file_url" - curl -o $archivePath $archiveUrl -L + local file_path="$release_version.tar.gz" + + curl -o $file_path $file_url -L + if [[ $? -ne 0 ]]; then + echo "❌ 下载文件失败!" + exit 1 + fi echo "✅ 下载成功" echo "⏳ 开始解压..." @@ -338,9 +442,10 @@ downloadSrc() { mkdir -p $appPath # 解压 - tar -zxvf $archivePath -C $appPath --strip-components 1 + tar -zxvf $file_path -C $appPath --strip-components=1 + # 删除压缩包 - rm -f $archivePath + rm -f $file_path echo "✅ 源码已覆盖到 $appPath" echo "🔘 删除标记文件 $appPath/$FILE_INITIALIZED" @@ -353,7 +458,7 @@ downloadSrc() { if [[ "$TYPE" == "src" ]]; then echo "" echo "⏳ 下载源码..." - downloadSrc + download_src fi # 询问输入容器名称 diff --git a/prepare-src.sh b/prepare-src.sh index 6d8ab82..dac85fc 100755 --- a/prepare-src.sh +++ b/prepare-src.sh @@ -3,6 +3,7 @@ # 脚本说明:下载应用源码并解压到指定的目录(通过`context`指定)下的`.mdcx_src`目录 # 一般只用于构建镜像流程,普通用户可以忽略。 # UPDATE 2023-12-24 17:08:03 使用新的源码仓库:https://github.com/sqzw-x/mdcx +# UPDATE 2024-05-28 21:28:01 sqzw-x/mdcx目前基本只进行daily_release构建 # 检查是否有jq命令 if ! command -v jq &> /dev/null @@ -24,6 +25,10 @@ do verbose=1 shift ;; + --dry) + dry=1 + shift + ;; -h|--help) help=1 shift @@ -47,51 +52,163 @@ fi cd $context -echo "ℹ️ 将从发布仓库下载源码进行构建" - -_url="https://api.github.com/repos/sqzw-x/mdcx/releases/latest" -_content=$(curl -s "$_url") - -# TODO github workflow里竟然会有比较大的概率获取失败 -if [[ -z "$_content" ]]; then - echo "❌ 请求 $_url 失败!" +echo "○ 将从发布仓库下载源码进行构建" + + +generate_app_version() { + local published_at="$1" + + # 去除非数字字符 + published_at=$(echo "$published_at" | tr -dc '0-9') + + # 取前8位数字作为年月日,前缀为d + echo "d${published_at:0:8}" +} + +find_release_by_tag_name() { + local repo=$1 + local target_tag_name=$2 + + local url="https://api.github.com/repos/${repo}/releases" + + # echo "URL: $url" + + local target_release="" + + let found=false + local page=1 + while true; do + local response=$(curl -s "${url}?per_page=100&page=${page}") + if [[ -z "$response" ]]; then + break + fi + + local releases=$(printf '%s' $response | jq -c '.[]') + for release in $releases; do + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ "$tag_name" == "$target_tag_name" ]]; then + found=true + echo $release + break + fi + done + + if [[ $found ]]; then + break + fi + + page=$((page + 1)) + done +} + +# 获取指定仓库和tag_name的release,并解析得到release信息 +# 返回json对象: +# { +# "tag_name": "v1.0.0", +# "published_at": "2022-01-01T00:00:00Z", +# "release_version": "120220101", +# "tar_url": "https://api.github.com/repos/sqzw-x/mdcx/tarball/daily_release", +# "zip_url": "https://api.github.com/repos/sqzw-x/mdcx/zipball/daily_release" +# } +get_release_info() { + local repo="$1" + local tag_name="$2" + + # echo "⏳ 正在获取仓库 ${repo} 中 tag_name=${tag_name} 的release..." + local release=$(find_release_by_tag_name "$repo" "$tag_name") + + if [[ -z "$release" ]]; then + echo "❌ 找不到 tag_name=${tag_name} 的release!" + return 1 + fi + + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ -z "$tag_name" ]]; then + echo "❌ 找不到 tag_name!" + return 1 + fi + + published_at=$(printf '%s' $release | jq -r '.published_at') + if [[ -z "$published_at" ]]; then + echo "❌ 找不到 published_at!" + return 1 + fi + + release_version=$(generate_app_version "$published_at") + + tar_url=$(printf '%s' $release | jq -r '.tarball_url') + if [[ -z "$tar_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 + fi + + zip_url=$(printf '%s' $release | jq -r '.zipball_url') + if [[ -z "$zip_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 + fi + + # 构建一个json对象 + local data="{ + \"tag_name\": \"${tag_name}\", + \"published_at\": \"${published_at}\", + \"release_version\": \"${release_version}\", + \"tar_url\": \"${tar_url}\", + \"zip_url\": \"${zip_url}\" + }" + echo $data + return 0 +} + +REPO="sqzw-x/mdcx" +TAG_NAME="daily_release" + +info=$(get_release_info "$REPO" "$TAG_NAME") +if [[ $? -ne 0 ]]; then + echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!" exit 1 +else + echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!" fi +echo $info | jq +# exit 0 -# tag名称,作为版本号 -tagName=$(printf '%s' $_content | jq -r ".tag_name") -archiveVersion=$(echo $tagName | sed 's/v//g') +# 发布时间 +published_at=$(printf '%s' $info | jq -r ".published_at") +echo "📅 发布时间: $published_at" -# 源码压缩包(tar格式)链接 -archiveUrl=$(printf '%s' $_content | jq -r ".tarball_url") +# 版本号 +release_version=$(printf '%s' $info | jq -r ".release_version") +echo "🔢 版本号: $release_version" -if [[ -z "$archiveUrl" ]]; then - echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" - echo "🔘 请求链接:$_url" - echo "🔘 请求结果:$_content" - exit 1 -fi +# 源码链接 +file_url=$(printf '%s' $info | jq -r ".tar_url") +echo "🔗 下载链接: $file_url" -if [[ -n "$verbose" ]]; then - echo "ℹ️ TAG名称: $tagName" - echo "🔗 下载链接: $archiveUrl" -fi -echo "ℹ️ 已发布版本: $archiveVersion" -if [[ -z "$archiveUrl" ]]; then +if [[ -z "$file_url" ]]; then echo "❌ 从请求结果获取下载链接失败!" exit 1 fi +if [[ -n "$dry" ]]; then + exit 0 +fi + echo "⏳ 下载文件..." -archivePath="$archiveVersion.tar.gz" +tar_path="$release_version.tar.gz" srcDir=".mdcx_src" if [[ -n "$verbose" ]]; then - curl -o $archivePath $archiveUrl -L + curl -o $tar_path $file_url -L else - curl -so $archivePath $archiveUrl -L + curl -so $tar_path $file_url -L +fi + +if [[ $? -ne 0 ]]; then + echo "❌ 下载文件失败!" + exit 1 fi echo "✅ 下载成功" @@ -100,10 +217,10 @@ echo "⏳ 开始解压..." # 使用tar命令解压 rm -rf $srcDir mkdir -p $srcDir -tar -zxvf $archivePath -C $srcDir --strip-components 1 -rm -f $archivePath +tar -zxvf $tar_path -C $srcDir --strip-components 1 +rm -f $tar_path echo "✅ 源码已解压到 $srcDir" if [ -n "$GITHUB_ACTIONS" ]; then - echo "APP_VERSION=$archiveVersion" >> $GITHUB_OUTPUT + echo "APP_VERSION=$release_version" >> $GITHUB_OUTPUT fi diff --git a/scripts/release-utils.sh b/scripts/release-utils.sh new file mode 100755 index 0000000..47c850f --- /dev/null +++ b/scripts/release-utils.sh @@ -0,0 +1,130 @@ +#!/bin/sh + +# 脚本说明:检查新版本 + +# 检查是否有jq命令 +if ! command -v jq &> /dev/null +then + echo "❌ 请先安装jq命令!参考:https://command-not-found.com/jq" + exit 1 +fi + +while [[ $# -gt 0 ]] +do + key="$1" + case $key in + --verbose) + verbose=1 + shift + ;; + -h|--help) + help=1 + shift + ;; + *) + shift + ;; + esac +done + +generate_app_version() { + local published_at="$1" + + # 去除非数字字符 + published_at=$(echo "$published_at" | tr -dc '0-9') + + # 取前8位数字作为年月日,前缀为1 + echo "1${published_at:0:8}" +} + +find_release_by_tag_name() { + local repo=$1 + local local target_tag_name=$2 + + local url="https://api.github.com/repos/${repo}/releases" + + local target_release="" + + let found=false + local page=1 + while true; do + local response=$(curl -s "${url}?per_page=100&page=${page}") + if [[ -z "$response" ]]; then + break + fi + + local releases=$(printf '%s' $response | jq -c '.[]') + for release in $releases; do + local tag_name=$(printf '%s' $release | jq -r '.tag_name') + local if [[ "$tag_name" == "$target_tag_name" ]]; then + found=true + echo $release + break + fi + done + + if [[ $found ]]; then + break + fi + + page=$((page + 1)) + done +} + +# 获取指定仓库和tag_name的release,并解析得到release信息 +# 返回json对象: +# { +# "tag_name": "v1.0.0", +# "published_at": "2022-01-01T00:00:00Z", +# "release_version": "120220101", +# "tar_url": "https://api.github.com/repos/sqzw-x/mdcx/tarball/daily_release", +# "zip_url": "https://api.github.com/repos/sqzw-x/mdcx/zipball/daily_release" +# } +get_release_info() { + local repo="$1" + local tag_name="$2" + + local echo "⏳ 正在获取仓库 ${repo} 中 tag_name=${tag_name} 的release..." + local release=$(find_release_by_tag_name "$repo" "$tag_name") + + if [[ -z "$release" ]]; then + local echo "❌ 找不到 tag_name=${tag_name} 的release!" + return 1 + + local tag_name=(printf '%s' $release | jq -r '.tag_name') + local if [[ -z "$tag_name" ]]; then + local echo "❌ 找不到 tag_name!" + return 1 + fi + + published_at=$(printf '%s' $release | jq -r '.published_at') + if [[ -z "$published_at" ]]; then + local echo "❌ 找不到 published_at!" + return 1 + fi + + release_version=$(generate_app_version "$published_at") + + tar_url=$(printf '%s' $release | jq -r '.tarball_url') + if [[ -z "$tar_url" ]]; then + local echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 + fi + + zip_url=$(printf '%s' $release | jq -r '.zipball_url') + if [[ -z "$zip_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 + fi + + # 构建一个json对象 + local data="{ + \"tag_name\": \"${tag_name}\", + \"published_at\": \"${published_at}\", + \"release_version\": \"${release_version}\", + \"tar_url\": \"${tar_url}\", + \"zip_url\": \"${zip_url}\" + }" + echo $data + return 0 +} \ No newline at end of file diff --git a/webtop-base/rootfs-src/app-assets/scripts/update-src.sh b/webtop-base/rootfs-src/app-assets/scripts/update-src.sh index aba3be5..9d54a9b 100755 --- a/webtop-base/rootfs-src/app-assets/scripts/update-src.sh +++ b/webtop-base/rootfs-src/app-assets/scripts/update-src.sh @@ -1,6 +1,20 @@ #!/bin/bash -export LC_ALL=zh_CN.UTF-8 +# 脚本说明: 当使用源码部署时,使用该脚本自动完成更新源码的处理。 + +# DEV: +# - gui-base/update-src.sh +# - webtop-base/update-src.sh +# - webtop-base/rootfs-src/app-assets/scripts/update-src.sh +# 除了.env提示不同外,其余部分基本相同。 +# webtop-base/rootfs-src/app-assets/scripts/update-src.sh 中 appPath=/app; 没有restart容器处理。 + +if [ ! -f ".env" ]; then + echo "⚠️ 当前目录缺少文件 .env。示例文件:https://github.com/northsea4/mdcx-docker/blob/main/webtop-base/.env.sample" + # exit 1 +else + . .env +fi # 检查是否有jq命令 if ! command -v jq &> /dev/null @@ -9,37 +23,22 @@ then exit 1 fi + FILE_INITIALIZED=".mdcx_initialized" # 应用版本 appVersion=0 -# 详细日志 -verbose=1 - -# 默认强制更新 -force=1 - # 源码存放目录 appPath="/app" -if [[ ! -d "$appPath" ]]; then - echo "❌ 应用源码不存在!" - exit 1 -fi - - while [[ $# -gt 0 ]] do key="$1" case $key in - --restart) - restart="$2" - shift - shift - ;; - --force) - force=1 + -p|--path|--src) + appPath="$2" + shift 2 shift ;; --dry) @@ -67,151 +66,198 @@ if [ -n "$help" ]; then echo "示例-检查并更新: $0" echo "" echo "参数说明:" - echo "--restart 更新后重启容器,默认true。可选参数值: 1, 0; true, false" - echo "--force 强制更新。默认情况下当已发布版本较新于本地版本时才会更新。" echo "--dry 只检查,不更新" echo "-h, --help 显示帮助信息" exit 0 fi -compareVersion () { - if [[ $1 == $2 ]] - then - return 0 - fi - local IFS=. - local i ver1=($1) ver2=($2) - # fill empty fields in ver1 with zeros - for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) - do - ver1[i]=0 - done - for ((i=0; i<${#ver1[@]}; i++)) - do - if [[ -z ${ver2[i]} ]] - then - # fill empty fields in ver2 with zeros - ver2[i]=0 - fi - if ((10#${ver1[i]} > 10#${ver2[i]})) - then - return 1 +generate_app_version() { + local published_at="$1" + + # 去除非数字字符 + published_at=$(echo "$published_at" | tr -dc '0-9') + + # 取前8位数字作为年月日,前缀为d + echo "d${published_at:0:8}" +} + +find_release_by_tag_name() { + local repo=$1 + local target_tag_name=$2 + + local url="https://api.github.com/repos/${repo}/releases" + + # echo "URL: $url" + + local target_release="" + + let found=false + local page=1 + while true; do + local response=$(curl -s "${url}?per_page=100&page=${page}") + if [[ -z "$response" ]]; then + break fi - if ((10#${ver1[i]} < 10#${ver2[i]})) - then - return 2 + + local releases=$(printf '%s' $response | jq -c '.[]') + for release in $releases; do + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ "$tag_name" == "$target_tag_name" ]]; then + found=true + echo $release + break + fi + done + + if [[ $found ]]; then + break fi + + page=$((page + 1)) done - return 0 } -# 从`appPath/config.ini.default`获取应用版本 -# [modified_time] -# modified_time = 2023-12-19 23:53:41 -# version = 120231219 -getAppVersionFromConfig () { - local configPath="$1" - if [[ -f "$configPath" ]]; then - local version=$(cat $configPath | grep -oi 'version\s*=\s*[0-9]\+' | grep -oi '[0-9]\+$') - echo $version - else - echo 0 +# 获取指定仓库和tag_name的release,并解析得到release信息 +# 返回json对象: +# { +# "tag_name": "v1.0.0", +# "published_at": "2022-01-01T00:00:00Z", +# "release_version": "120220101", +# "tar_url": "https://api.github.com/repos/sqzw-x/mdcx/tarball/daily_release", +# "zip_url": "https://api.github.com/repos/sqzw-x/mdcx/zipball/daily_release" +# } +get_release_info() { + local repo="$1" + local tag_name="$2" + + # echo "⏳ 正在获取仓库 ${repo} 中 tag_name=${tag_name} 的release..." + local release=$(find_release_by_tag_name "$repo" "$tag_name") + + if [[ -z "$release" ]]; then + echo "❌ 找不到 tag_name=${tag_name} 的release!" + return 1 + fi + + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ -z "$tag_name" ]]; then + echo "❌ 找不到 tag_name!" + return 1 + fi + + published_at=$(printf '%s' $release | jq -r '.published_at') + if [[ -z "$published_at" ]]; then + echo "❌ 找不到 published_at!" + return 1 + fi + + release_version=$(generate_app_version "$published_at") + + tar_url=$(printf '%s' $release | jq -r '.tarball_url') + if [[ -z "$tar_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 + fi + + zip_url=$(printf '%s' $release | jq -r '.zipball_url') + if [[ -z "$zip_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 fi + + # 构建一个json对象 + local data="{ + \"tag_name\": \"${tag_name}\", + \"published_at\": \"${published_at}\", + \"release_version\": \"${release_version}\", + \"tar_url\": \"${tar_url}\", + \"zip_url\": \"${zip_url}\" + }" + echo $data + return 0 } appPath=$(echo "$appPath" | sed 's:/*$::') - -appConfigPath="$appPath/config.ini.default" -appVersion=$(getAppVersionFromConfig "$appConfigPath") -if [[ $appVersion == 0 ]]; then - isEmpty=1 - echo "ℹ️ 本地应用版本: $appVersion" +if [[ -n "${appPath}" ]]; then + if [[ ! -d "${appPath}" ]]; then + echo "⚠️ $appPath 不存在,现在创建" + mkdir -p $appPath + else + echo "✅ $appPath 已经存在" + fi else - echo "ℹ️ 从 $appConfigPath 检测到应用版本为 $appVersion" + echo "❌ 应用源码目录参数不能为空!" + exit 1 fi -_url="https://api.github.com/repos/sqzw-x/mdcx/releases/latest" -_content=$(curl -s "$_url") +REPO="sqzw-x/mdcx" +TAG_NAME="daily_release" -# TODO github workflow里竟然会有比较大的概率获取失败 -if [[ -z "$_content" ]]; then - echo "❌ 请求 $_url 失败!" +info=$(get_release_info "$REPO" "$TAG_NAME") +if [[ $? -ne 0 ]]; then + echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!" exit 1 +else + echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!" fi +echo $info | jq +# exit 0 + +# 发布时间 +published_at=$(printf '%s' $info | jq -r ".published_at") +echo "📅 发布时间: $published_at" -# tag名称,作为版本号 -tagName=$(printf '%s' $_content | jq -r ".tag_name") -archiveVersion=$(echo $tagName | sed 's/v//g') +# 版本号 +release_version=$(printf '%s' $info | jq -r ".release_version") +echo "🔢 版本号: $release_version" -# 源码压缩包(tar格式)链接 -archiveUrl=$(printf '%s' $_content | jq -r ".tarball_url") +# 源码链接 +file_url=$(printf '%s' $info | jq -r ".tar_url") +echo "🔗 下载链接: $file_url" -if [[ -z "$archiveUrl" ]]; then - echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" - echo "🔘 请求链接:$_url" + +if [[ -z "$file_url" ]]; then + echo "❌ 从请求结果获取下载链接失败!" exit 1 fi -if [[ -n "$verbose" ]]; then - echo "ℹ️ TAG名称: $tagName" - echo "🔗 下载链接: $archiveUrl" +if [[ -n "$dry" ]]; then + exit 0 fi -echo "ℹ️ 已发布版本: $archiveVersion" - -compareVersion $archiveVersion $appVersion -case $? in - 0) op='=';; - 1) op='>';; - 2) op='<';; -esac +tar_path="$release_version.tar.gz" -shouldUpdate= -if [[ $op == '>' ]]; then - echo " 已发布的最新版本 较新于 本地版本" - shouldUpdate=1 +if [[ -n "$verbose" ]]; then + curl -o $tar_path $file_url -L +else + curl -so $tar_path $file_url -L fi -if [[ -n "$force" ]]; then - echo "ℹ️ 强制更新" - shouldUpdate=1 +if [[ $? -ne 0 ]]; then + echo "❌ 下载源码压缩包失败!" + exit 1 fi -if [[ -n "$shouldUpdate" ]]; then - - if [[ -n "$dry" ]]; then - exit 0 - fi - - archivePath="$archiveVersion.tar.gz" - - if [[ -n "$verbose" ]]; then - curl -o $archivePath $archiveUrl -L - else - curl -so $archivePath $archiveUrl -L - fi - - echo "✅ 下载成功" - echo "⏳ 开始解压..." +echo "✅ 下载成功" +echo "⏳ 开始解压..." - # 解压新的源码到app目录 - tar -zxvf $archivePath -C $appPath --strip-components 1 - # 删除压缩包 - rm -f $archivePath - echo "✅ 源码已覆盖到 $appPath" +# 解压新的源码到app目录 +tar -zxvf $tar_path -C $appPath --strip-components 1 +# 删除压缩包 +rm -f $tar_path +echo "✅ 源码已覆盖到 $appPath" - echo "ℹ️ 删除标记文件 $appPath/$FILE_INITIALIZED" - rm -f "$appPath/$FILE_INITIALIZED" +if [ -f ".env.versions" ]; then + echo "✅ 更新 .env.versions MDCX_APP_VERSION=$release_version" + sed -i -e "s/MDCX_APP_VERSION=[0-9.]\+/MDCX_APP_VERSION=$release_version/" .env.versions +fi - echo "✅ 源码已更新成功!版本:$archiveVersion" - -else - if [[ $op == '<' ]]; then - echo "ℹ️ 本地版本 较新于 已发布的最新版本" - else - echo "ℹ️ 本地版本 已是最新版本" - fi +if [ -f ".env" ]; then + echo "✅ 更新 .env APP_VERSION=$release_version" + sed -i -e "s/APP_VERSION=[0-9.]\+/APP_VERSION=$release_version/" .env fi -echo "ℹ️ 按下任意键关闭窗口。" \ No newline at end of file +echo "ℹ️ 删除标记文件 $appPath/$FILE_INITIALIZED" +rm -f "$appPath/$FILE_INITIALIZED" + +echo "🎉 Enjoy~" \ No newline at end of file diff --git a/webtop-base/update-src.sh b/webtop-base/update-src.sh index 4cac9f7..89260f9 100755 --- a/webtop-base/update-src.sh +++ b/webtop-base/update-src.sh @@ -1,5 +1,14 @@ #!/bin/bash +# 脚本说明: 当使用源码部署时,使用该脚本自动完成更新源码的处理。 + +# DEV: +# - gui-base/update-src.sh +# - webtop-base/update-src.sh +# - webtop-base/rootfs-src/app-assets/scripts/update-src.sh +# 除了.env提示不同外,其余部分基本相同。 +# webtop-base/rootfs-src/app-assets/scripts/update-src.sh 中 appPath=/app; 没有restart容器处理。 + if [ ! -f ".env" ]; then echo "⚠️ 当前目录缺少文件 .env。示例文件:https://github.com/northsea4/mdcx-docker/blob/main/webtop-base/.env.sample" # exit 1 @@ -40,10 +49,6 @@ do shift shift ;; - --force) - force=1 - shift - ;; --dry) dry=1 shift @@ -66,184 +71,212 @@ done if [ -n "$help" ]; then echo "脚本功能:更新自部署的应用源码" echo "" - echo "示例-检查并更新: ./update-src.sh" + echo "示例-检查并更新: $0" echo "" echo "参数说明:" echo "--restart 更新后重启容器,默认false。可选参数值: 1, 0; true, false" - echo "--force 强制更新。默认情况下当已发布版本较新于本地版本时才会更新。" echo "--dry 只检查,不更新" echo "-h, --help 显示帮助信息" exit 0 fi -compareVersion () { - if [[ $1 == $2 ]] - then - return 0 - fi - local IFS=. - local i ver1=($1) ver2=($2) - # fill empty fields in ver1 with zeros - for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) - do - ver1[i]=0 - done - for ((i=0; i<${#ver1[@]}; i++)) - do - if [[ -z ${ver2[i]} ]] - then - # fill empty fields in ver2 with zeros - ver2[i]=0 - fi - if ((10#${ver1[i]} > 10#${ver2[i]})) - then - return 1 +generate_app_version() { + local published_at="$1" + + # 去除非数字字符 + published_at=$(echo "$published_at" | tr -dc '0-9') + + # 取前8位数字作为年月日,前缀为d + echo "d${published_at:0:8}" +} + +find_release_by_tag_name() { + local repo=$1 + local target_tag_name=$2 + + local url="https://api.github.com/repos/${repo}/releases" + + # echo "URL: $url" + + local target_release="" + + let found=false + local page=1 + while true; do + local response=$(curl -s "${url}?per_page=100&page=${page}") + if [[ -z "$response" ]]; then + break fi - if ((10#${ver1[i]} < 10#${ver2[i]})) - then - return 2 + + local releases=$(printf '%s' $response | jq -c '.[]') + for release in $releases; do + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ "$tag_name" == "$target_tag_name" ]]; then + found=true + echo $release + break + fi + done + + if [[ $found ]]; then + break fi + + page=$((page + 1)) done - return 0 } -# 从`appPath/config.ini.default`获取应用版本 -# [modified_time] -# modified_time = 2023-12-19 23:53:41 -# version = 120231219 -getAppVersionFromConfig () { - local configPath="$1" - if [[ -f "$configPath" ]]; then - local version=$(cat $configPath | grep -oi 'version\s*=\s*[0-9]\+' | grep -oi '[0-9]\+$') - echo $version - else - echo 0 +# 获取指定仓库和tag_name的release,并解析得到release信息 +# 返回json对象: +# { +# "tag_name": "v1.0.0", +# "published_at": "2022-01-01T00:00:00Z", +# "release_version": "120220101", +# "tar_url": "https://api.github.com/repos/sqzw-x/mdcx/tarball/daily_release", +# "zip_url": "https://api.github.com/repos/sqzw-x/mdcx/zipball/daily_release" +# } +get_release_info() { + local repo="$1" + local tag_name="$2" + + # echo "⏳ 正在获取仓库 ${repo} 中 tag_name=${tag_name} 的release..." + local release=$(find_release_by_tag_name "$repo" "$tag_name") + + if [[ -z "$release" ]]; then + echo "❌ 找不到 tag_name=${tag_name} 的release!" + return 1 + fi + + tag_name=$(printf '%s' $release | jq -r '.tag_name') + if [[ -z "$tag_name" ]]; then + echo "❌ 找不到 tag_name!" + return 1 + fi + + published_at=$(printf '%s' $release | jq -r '.published_at') + if [[ -z "$published_at" ]]; then + echo "❌ 找不到 published_at!" + return 1 + fi + + release_version=$(generate_app_version "$published_at") + + tar_url=$(printf '%s' $release | jq -r '.tarball_url') + if [[ -z "$tar_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 + fi + + zip_url=$(printf '%s' $release | jq -r '.zipball_url') + if [[ -z "$zip_url" ]]; then + echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" + return 1 fi + + # 构建一个json对象 + local data="{ + \"tag_name\": \"${tag_name}\", + \"published_at\": \"${published_at}\", + \"release_version\": \"${release_version}\", + \"tar_url\": \"${tar_url}\", + \"zip_url\": \"${zip_url}\" + }" + echo $data + return 0 } appPath=$(echo "$appPath" | sed 's:/*$::') -isEmpty=0 if [[ -n "${appPath}" ]]; then if [[ ! -d "${appPath}" ]]; then echo "⚠️ $appPath 不存在,现在创建" mkdir -p $appPath else - appConfigPath="$appPath/config.ini.default" - appVersion=$(getAppVersionFromConfig "$appConfigPath") - if [[ $appVersion == 0 ]]; then - isEmpty=1 - echo "ℹ️ 本地应用版本: $appVersion" - else - echo "ℹ️ 从 $appConfigPath 检测到应用版本为 $appVersion" - fi + echo "✅ $appPath 已经存在" fi else - echo "❌ 应用源码目录不能为空!" + echo "❌ 应用源码目录参数不能为空!" exit 1 fi -_url="https://api.github.com/repos/sqzw-x/mdcx/releases/latest" -_content=$(curl -s "$_url") +REPO="sqzw-x/mdcx" +TAG_NAME="daily_release" -# TODO github workflow里竟然会有比较大的概率获取失败 -if [[ -z "$_content" ]]; then - echo "❌ 请求 $_url 失败!" +info=$(get_release_info "$REPO" "$TAG_NAME") +if [[ $? -ne 0 ]]; then + echo "❌ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息失败!" exit 1 +else + echo "✅ 获取仓库 ${REPO} 中 tag_name=${TAG_NAME} 的release信息成功!" fi +echo $info | jq +# exit 0 -# tag名称,作为版本号 -tagName=$(printf '%s' $_content | jq -r ".tag_name") -archiveVersion=$(echo $tagName | sed 's/v//g') +# 发布时间 +published_at=$(printf '%s' $info | jq -r ".published_at") +echo "📅 发布时间: $published_at" -# 源码压缩包(tar格式)链接 -archiveUrl=$(printf '%s' $_content | jq -r ".tarball_url") +# 版本号 +release_version=$(printf '%s' $info | jq -r ".release_version") +echo "🔢 版本号: $release_version" -if [[ -z "$archiveUrl" ]]; then - echo "❌ 从请求结果获取源码压缩包文件下载链接失败!" - echo "🔘 请求链接:$_url" +# 源码链接 +file_url=$(printf '%s' $info | jq -r ".tar_url") +echo "🔗 下载链接: $file_url" + + +if [[ -z "$file_url" ]]; then + echo "❌ 从请求结果获取下载链接失败!" exit 1 fi -if [[ -n "$verbose" ]]; then - echo "ℹ️ TAG名称: $tagName" - echo "🔗 下载链接: $archiveUrl" +if [[ -n "$dry" ]]; then + exit 0 fi -echo "ℹ️ 已发布版本: $archiveVersion" - -# exit -compareVersion $archiveVersion $appVersion -case $? in - 0) op='=';; - 1) op='>';; - 2) op='<';; -esac +file_path="$release_version.tar.gz" -shouldUpdate= -if [[ $op == '>' ]]; then - echo "🆕 已发布的最新版本 较新于 本地版本" - shouldUpdate=1 +if [[ -n "$verbose" ]]; then + curl -o $file_path $file_url -L +else + curl -so $file_path $file_url -L fi -if [[ -n "$force" ]]; then - echo "ℹ️ 强制更新" - shouldUpdate=1 +if [[ $? -ne 0 ]]; then + echo "❌ 下载源码压缩包失败!" + exit 1 fi -if [[ -n "$shouldUpdate" ]]; then - - if [[ -n "$dry" ]]; then - exit 0 - fi - - archivePath="$archiveVersion.tar.gz" +echo "✅ 下载成功" +echo "⏳ 开始解压..." - if [[ -n "$verbose" ]]; then - curl -o $archivePath $archiveUrl -L - else - curl -so $archivePath $archiveUrl -L - fi +# 解压新的源码到app目录 +tar -zxvf $file_path -C $appPath --strip-components 1 +# 删除压缩包 +rm -f $file_path +echo "✅ 源码已覆盖到 $appPath" - echo "✅ 下载成功" - echo "⏳ 开始解压..." - - # 解压新的源码到app目录 - tar -zxvf $archivePath -C $appPath --strip-components 1 - # 删除压缩包 - rm -f $archivePath - echo "✅ 源码已覆盖到 $appPath" - - if [ -f ".env.versions" ]; then - echo "✅ 更新 .env.versions MDCX_APP_VERSION=$archiveVersion" - sed -i -e "s/MDCX_APP_VERSION=[0-9.]\+/MDCX_APP_VERSION=$archiveVersion/" .env.versions - fi +if [ -f ".env.versions" ]; then + echo "✅ 更新 .env.versions MDCX_APP_VERSION=$release_version" + sed -i -e "s/MDCX_APP_VERSION=[0-9.]\+/MDCX_APP_VERSION=$release_version/" .env.versions +fi - if [ -f ".env" ]; then - echo "✅ 更新 .env APP_VERSION=$archiveVersion" - sed -i -e "s/APP_VERSION=[0-9.]\+/APP_VERSION=$archiveVersion/" .env - fi +if [ -f ".env" ]; then + echo "✅ 更新 .env APP_VERSION=$release_version" + sed -i -e "s/APP_VERSION=[0-9.]\+/APP_VERSION=$release_version/" .env +fi - echo "ℹ️ 删除标记文件 $appPath/$FILE_INITIALIZED" - rm -f "$appPath/$FILE_INITIALIZED" +echo "ℹ️ 删除标记文件 $appPath/$FILE_INITIALIZED" +rm -f "$appPath/$FILE_INITIALIZED" - if [[ -n "MDCX_SRC_CONTAINER_NAME" ]]; then - if [[ "$restart" == "1" || "$restart" == "true" ]]; then - echo "⏳ 重启容器..." - docker restart $MDCX_SRC_CONTAINER_NAME - else - echo "ℹ️ 如果已经部署过容器,执行以下命令重启容器" - echo "docker restart $MDCX_SRC_CONTAINER_NAME" - fi - fi -else - if [[ $op == '<' ]]; then - echo "ℹ️ 本地版本 较新于 已发布的最新版本" +if [[ -n "MDCX_SRC_CONTAINER_NAME" ]]; then + if [[ "$restart" == "1" || "$restart" == "true" ]]; then + echo "⏳ 重启容器..." + docker restart $MDCX_SRC_CONTAINER_NAME else - echo "ℹ️ 本地版本 已是最新版本" + echo "ℹ️ 如果已经部署过容器,执行以下命令重启容器" + echo "docker restart $MDCX_SRC_CONTAINER_NAME" fi fi -if [ -n "$GITHUB_ACTIONS" ]; then - echo "APP_VERSION=$archiveVersion" >> $GITHUB_OUTPUT -fi \ No newline at end of file +echo "🎉 Enjoy~" \ No newline at end of file