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

【新增】添加自动发行版本的 GitHub Actions 工作流

PJ568 1 рік тому
батько
коміт
62ff2854de
3 змінених файлів з 118 додано та 1 видалено
  1. 66 0
      .github/workflows/release.yml
  2. 1 1
      clean_trash.sh
  3. 51 0
      scripts/is-newer-version.bash

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

@@ -0,0 +1,66 @@
+name: 自动发行版本
+
+on:
+  push:
+    branches:
+      - master
+
+jobs:
+  release:
+    runs-on: ubuntu-latest
+    permissions:
+      contents: write
+      pages: write
+      id-token: write
+    name: 发行新版本
+    steps:
+    - name: 检出代码
+      uses: actions/checkout@v4
+      with:
+        fetch-depth: 0
+
+    - name: 检查版本
+      id: version-check
+      run: |
+        VERSION=$(./scripts/is-newer-version.bash)
+        echo "version=${VERSION}" >> $GITHUB_OUTPUT
+        if [ "$VERSION" != "0" ]; then
+          echo "检测到新版本: $VERSION"
+        else
+          echo "没有新版本需要发行"
+        fi
+
+    - name: 推送标签
+      if: steps.version-check.outputs.version != '0'
+      id: push-tag
+      run: |
+        tag="v${{ steps.version-check.outputs.version }}"
+        git config user.name "${GITHUB_ACTOR}"
+        git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
+        git tag -a "${tag}" -m "Release ${tag}"
+        git push origin "${tag}"
+        echo "tag_name=${tag}" >> $GITHUB_OUTPUT
+
+    - name: 准备发行文件
+      if: steps.version-check.outputs.version != '0'
+      run: |
+        mkdir -p ../release
+        cp -v clean_trash.sh ../release/clean_trash
+        # 确保脚本具有执行权限
+        chmod +x ../release/clean_trash
+
+    - name: 创建 GitHub Release
+      if: steps.version-check.outputs.version != '0'
+      uses: ncipollo/release-action@v1
+      with:
+        generateReleaseNotes: true
+        tag: "v${{ steps.version-check.outputs.version }}"
+        artifacts: "../release/clean_trash"
+
+    - name: 删除标签(如果失败)
+      if: (cancelled() || failure()) && steps.push-tag.outputs.tag_name != ''
+      run: |
+        git config user.name "${GITHUB_ACTOR}"
+        git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
+        git push --delete origin "${{ steps.push-tag.outputs.tag_name }}"
+        git tag -d "${{ steps.push-tag.outputs.tag_name }}"

+ 1 - 1
clean_trash.sh

@@ -3,7 +3,7 @@
 # 设置定量 | Quantities
 ## 初始化变量 | Initialize variables
 VERBOSE=0
-VERSION="1.0.0"
+VERSION="1.0.1"
 ## 当前语言 | Current language
 CURRENT_LANG=0 # 0: en-US, 1: zh-Hans-CN
 ## 定义回收站目录 | Define trash directory

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

@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+# 判断是否需要发行新版本 | Checking for a Newer Version
+
+# 设置定量 | 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=$(grep -E "^VERSION=" "$REPO_DIR/clean_trash.sh" | cut -d'"' -f2)
+if [ -z "$CURRENT_VERSION" ]; then
+  recho "错误:无法从 clean_trash.sh 获取版本号" "Error: Cannot get version from clean_trash.sh"
+  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