Переглянути джерело

【维护】添加发布流程和版本检查脚本

- 新增 Release.yml 工作流,用于创建发布
- 添加 check-version-and-push-tag.yml 工作流,用于检查版本并推送标签
- 新增 get-version.bash 脚本,用于提取版本号
- 新增 is-newer-version.bash 脚本,用于比较版本号
- 新增 release.bash 脚本,用于生成版本号并推送标签
PJ568 1 рік тому
батько
коміт
439646f586

+ 35 - 0
.github/workflows/check-version-and-push-tag.yml

@@ -0,0 +1,35 @@
+name: check version and push tag
+
+on:
+  workflow_dispatch:
+  push:
+    branches:
+      - main
+
+jobs:
+  check:
+    runs-on: ubuntu-latest
+    permissions: write-all
+    steps:
+
+    - name: Checkout
+      uses: actions/checkout@v4
+    
+    - name: Check version
+      id: version-check
+      run: |
+        chmod +x ./scripts/is-newer-version.bash
+        chmod +x ./scripts/get-version.bash
+        VERSION=$(./scripts/is-newer-version.bash)
+        echo "version=${VERSION}" >> $GITHUB_OUTPUT
+        if [ "$VERSION" != "0" ]; then
+          echo "New version detected: $VERSION"
+        else
+          echo "No new version to release"
+        fi
+
+    - name: Push tag
+      if: steps.version-check.outputs.version != '0'
+      uses: actions-ecosystem/action-push-tag@v1
+      with:
+        tag: "v${{ steps.version-check.outputs.version }}"

+ 24 - 0
.github/workflows/release.yml

@@ -0,0 +1,24 @@
+name: release
+
+on: 
+  push:
+    tags:
+      - "v*"
+
+jobs:
+  release:
+    runs-on: ubuntu-latest
+    permissions: write-all
+    name: Export
+    steps:
+
+    - name: Checkout
+      uses: actions/checkout@v4
+
+    - name: Create release
+      uses: ncipollo/release-action@v1
+      with:
+        token: ${{ secrets.TOKEN }}
+        generateReleaseNotes: true
+        tag: ${{ github.ref_name }}
+        artifacts: "index.html,index.min.html"

+ 41 - 0
scripts/get-version.bash

@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+# 从 ../index.html 中获取版本号 | Get version number from ../index.html
+
+# 设置定量 | Quantities
+## 当前脚本所在目录 | Current Script Directory
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+## 仓库目录 | Repository Directory
+REPO_DIR="$(dirname "$SCRIPT_DIR")"
+## 文件路径 | File Path
+FILE_PATH="$REPO_DIR/index.html"
+## 当前语言 | Current Language
+CURRENT_LANG=0 ### 0: en-US, 1: zh-Hans-CN
+
+# 语言检测 | Language Detection
+if [ $(echo ${LANG/_/-} | grep -Ei "\\b(zh|cn)\\b") ]; then CURRENT_LANG=1;  fi
+
+# 本地化 | Localization
+recho() {
+  if [ $CURRENT_LANG == 1 ]; then
+    ## zh-Hans-CN
+    echo $1;
+  else
+    ## en-US
+    echo $2;
+  fi
+}
+
+# 提取版本号并打印 | Extract version number and print
+if [ ! -f "$FILE_PATH" ]; then
+  recho "错误:文件 $FILE_PATH 不存在" "Error: File $FILE_PATH does not exist"
+  exit 1
+fi
+
+## 从 <markdown-html> 标签中提取第一个 version 属性的值
+VERSION=$(grep -oP '<markdown-html\s+version="\K[^"]+' "$FILE_PATH" | head -1)
+if [ -z "$VERSION" ]; then
+  recho "错误:未找到版本号" "Error: Version not found"
+  exit 1
+fi
+
+echo "$VERSION"

+ 50 - 0
scripts/is-newer-version.bash

@@ -0,0 +1,50 @@
+#!/usr/bin/env bash
+# 从 ../index.html 中获取版本号 | Get version number from ../index.html
+
+# 设置定量 | Quantities
+## 当前脚本所在目录 | Current Script Directory
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+## 仓库目录 | Repository Directory
+REPO_DIR="$(dirname "$SCRIPT_DIR")"
+## 当前语言 | Current Language
+CURRENT_LANG=0 ### 0: en-US, 1: zh-Hans-CN
+
+# 语言检测 | Language Detection
+if [ $(echo ${LANG/_/-} | grep -Ei "\\b(zh|cn)\\b") ]; then CURRENT_LANG=1;  fi
+
+# 本地化 | Localization
+recho() {
+  if [ $CURRENT_LANG == 1 ]; then
+    ## zh-Hans-CN
+    echo $1;
+  else
+    ## en-US
+    echo $2;
+  fi
+}
+
+# 获取当前版本号 | Get current version number
+CURRENT_VERSION=$("$SCRIPT_DIR/get-version.bash")
+if [ $? -ne 0 ]; then
+  exit 1
+fi
+
+# 获取最新的 git 标签版本 | Get the latest git tag version
+LATEST_TAG=$(git -C "$REPO_DIR" tag -l 'v*' --sort=-v:refname | head -1)
+if [ -z "$LATEST_TAG" ]; then
+  # 没有标签时默认为需要发布 | No tags, assume it needs to be released
+  echo "$CURRENT_VERSION"
+  exit 0
+fi
+
+# 去除标签中的 v 前缀 | Remove the v prefix from the tag
+LATEST_VERSION=${LATEST_TAG#v}
+
+# 比较版本号 | Compare version numbers
+if [ "$(printf "%s\n%s" "$CURRENT_VERSION" "$LATEST_VERSION" | sort -V | head -n1)" != "$CURRENT_VERSION" ]; then
+  # 当前版本较新,需要发布 | The current version is newer, needs to be released
+  echo "$CURRENT_VERSION"
+else
+  # 已经是最新版本 | Already the latest version
+  echo "0"
+fi

+ 80 - 0
scripts/release.bash

@@ -0,0 +1,80 @@
+#!/usr/bin/env bash
+# 根据参数生成版本号,并推送标签到远程仓库 | Automatically generate version number and push tags to remote repository
+
+# 设置定量 | Quantities
+## 当前脚本所在目录 | Current Script Directory
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+## 仓库目录 | Repository Directory
+REPO_DIR="$(dirname "$SCRIPT_DIR")"
+## 当前语言 | Current Language
+CURRENT_LANG=0 ### 0: en-US, 1: zh-Hans-CN
+
+# 语言检测 | Language Detection
+if [ $(echo ${LANG/_/-} | grep -Ei "\\b(zh|cn)\\b") ]; then CURRENT_LANG=1;  fi
+
+# 本地化 | Localization
+recho() {
+  if [ $CURRENT_LANG == 1 ]; then
+    ## zh-Hans-CN
+    echo $1;
+  else
+    ## en-US
+    echo $2;
+  fi
+}
+
+# 参数解析 | Argument Parsing
+version=""
+while [[ $# -gt 0 ]]; do
+  case "$1" in
+    -v|--version)
+      version="$2"
+      shift 2
+      ;;
+    *)
+      recho "错误: 未知参数 $1" "Error: Unknown argument $1"
+      exit 1
+      ;;
+  esac
+done
+
+# 版本号验证函数 | Version Validation Function
+validate_version() {
+  if [[ ! "$1" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
+    recho "错误: 版本号格式不正确" "Error: Invalid version format"
+    exit 1
+  fi
+}
+
+# 如果未通过参数指定版本号,则交互式输入 | Interactive input if version not specified
+if [ -z "$version" ]; then
+  while true; do
+    recho "请输入版本号 (格式: X.X.X): " "Please enter version number (format: X.X.X): "
+    read -r version
+    validate_version "$version" && break
+  done
+else
+  validate_version "$version"
+fi
+
+# 切换到仓库目录 | Change to repository directory
+recho "切换到仓库目录..." "Changing to repository directory..."
+cd "$REPO_DIR" || {
+  recho "错误: 无法切换到仓库目录" "Error: Failed to change to repository directory"
+  exit 1
+}
+
+# 创建并推送标签 | Create and push tag
+recho "创建标签 v$version..." "Creating tag v$version..."
+git tag "v$version" || {
+  recho "错误: 创建标签失败" "Error: Failed to create tag"
+  exit 1
+}
+
+recho "推送标签到远程仓库..." "Pushing tag to remote repository..."
+git push origin "v$version" || {
+  recho "错误: 推送标签失败" "Error: Failed to push tag"
+  exit 1
+}
+
+recho "版本 $version 发布成功!" "Version $version released successfully!"