Sfoglia il codice sorgente

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

- 新增 check 和 release 两个 GitHub Actions 工作流
- 实现版本号自动提取和比较功能
- 添加新版本发布和 GitHub Pages 部署流程
- 更新 README.md 版本号至 v0.1.5
PJ568 1 anno fa
parent
commit
4c2ef08e3a

+ 27 - 0
.github/workflows/check.yml

@@ -0,0 +1,27 @@
+name: check
+
+on: [push, pull_request, merge_group, workflow_dispatch]
+
+jobs:
+  check:
+    runs-on: ubuntu-latest
+    name: Check
+    steps:
+
+    - name: Checkout
+      uses: actions/checkout@v4
+      with:
+        fetch-depth: 0
+    
+    - 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

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

@@ -0,0 +1,78 @@
+name: release
+
+on:
+  workflow_dispatch:
+  push:
+    branches:
+      - master
+
+jobs:
+  release:
+    runs-on: ubuntu-latest
+    permissions: write-all
+    name: Release
+    steps:
+
+    - name: Checkout
+      uses: actions/checkout@v4
+      with:
+        fetch-depth: 0
+    
+    - 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'
+      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: Checkout
+      if: steps.version-check.outputs.version != '0'
+      uses: actions/checkout@v4
+
+    - name: Create release
+      if: steps.version-check.outputs.version != '0'
+      uses: ncipollo/release-action@v1
+      with:
+        generateReleaseNotes: true
+        tag: "v${{ steps.version-check.outputs.version }}"
+        artifacts: "./README.md"
+
+    - name: Drop markdown.html
+      uses: PJ-568/[email protected]
+
+    - name: Setup Pages
+      uses: actions/configure-pages@v4
+
+    - name: Upload artifact
+      uses: actions/upload-pages-artifact@v3
+      with:
+        path: './'
+
+    - name: Deploy to GitHub Pages
+      id: deployment
+      uses: actions/deploy-pages@v4
+
+    - name: Delete tag
+      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
README.md

@@ -1,4 +1,4 @@
-# PJ568 提交说明规范 `v 0.1.4`
+# PJ568 提交说明规范 `v0.1.5`
 
 本规范旨在格式化提交说明,以在方便阅读理解的同时,保证其能被程序一致地解析。
 

+ 45 - 0
scripts/get-version.bash

@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+# 从 ../README.md 中获取版本号 | Get version number from ../README.md
+
+# 设置定量 | Quantities
+## 当前脚本所在目录 | Current Script Directory
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+## 仓库目录 | Repository Directory
+REPO_DIR="$(dirname "$SCRIPT_DIR")"
+## 文件路径 | File Path
+if [ -n "$1" ]; then
+  FILE_PATH="$1"
+else
+  FILE_PATH="$REPO_DIR/README.md"
+fi
+## 当前语言 | 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
+
+## 从 README.md 标题中提取版本数值(X.X.X)| Extract version number (X.X.X) from README.md title
+VERSION=$(grep -E '^# .*v[0-9]+\.[0-9]+\.[0-9]+' "$FILE_PATH" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
+if [ -z "$VERSION" ]; then
+  recho "错误:未找到版本号" "Error: Version not found"
+  exit 1
+fi
+
+echo "$VERSION"

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

@@ -0,0 +1,34 @@
+#!/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")"
+
+# 获取当前版本号 | 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