From 8915023593fd686d8138e700c61f35422d127ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=BB=E7=B5=B1=E7=AE=A1=E7=90=86=E5=93=A1?= <1+admin@noreply.localhost> Date: Sat, 11 Jul 2026 03:39:39 +0000 Subject: [PATCH 1/8] =?UTF-8?q?=E5=88=AA=E9=99=A4=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 375 ------------------------------------------------------ 1 file changed, 375 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 505de76..0000000 --- a/README.md +++ /dev/null @@ -1,375 +0,0 @@ -# Gitea Docker Container Action 範本 - -Docker container(容器)action 讓你把整個執行環境打包成一個 Docker image:action 在你指定的容器內執行,環境、相依套件、工具版本全部固定,跨機器結果一致。適合需要特定系統套件、編譯環境或非 JavaScript 語言撰寫的 action。 - -本文件整理 Docker container action `action.yml` 中**所有可用參數、說明與限制**,包含 `Dockerfile` 撰寫注意事項,並特別標出 **Gitea 與 GitHub Actions 的差異**。 - -> 語法基準:Gitea Actions 以相容 GitHub Actions metadata 語法為目標,但兩者有明確差異(見「Gitea vs GitHub」章節)。Gitea 端的行為亦受底層 [`act`](https://gitea.com/gitea/act) runner 版本影響,實作前建議以測試機驗證。 -> -> ⚠️ **平台限制**:Docker container action **只能在 Linux runner 上執行**,且該 runner 必須安裝 Docker。Windows / macOS runner 不支援。 - ---- - -## 目錄 - -- [完整結構總覽](#完整結構總覽) -- [頂層參數](#頂層參數) -- [`inputs`(輸入參數)](#inputs輸入參數) -- [`outputs`(輸出)](#outputs輸出) -- [`runs`(執行設定)](#runs執行設定) -- [`Dockerfile` 撰寫注意事項](#dockerfile-撰寫注意事項) -- [Docker action 的限制與注意事項](#docker-action-的限制與注意事項) -- [Gitea vs GitHub Actions 差異](#gitea-vs-github-actions-差異) -- [本 repo 範例對照](#本-repo-範例對照) -- [參考來源](#參考來源) - ---- - -## 完整結構總覽 - -```yaml -name: 'Gitea Docker Template' # 必填 -description: 'Gitea Docker 範本' # 必填 -author: 'Jeffery' # 選填 - -inputs: # 選填,定義輸入參數 - message: - description: '輸入訊息' - required: false - default: 'Hello, World!' - -outputs: # 選填,定義輸出(docker action 只宣告,不用 value) - message: - description: '輸出訊息' - -runs: # 必填 - using: 'docker' # 必填,固定為 docker - image: 'Dockerfile' # 必填,本機 Dockerfile 或 docker://image - # entrypoint: '/entrypoint.sh' # 選填,覆寫 Dockerfile 的 ENTRYPOINT - # pre-entrypoint: '/setup.sh' # 選填,主程式前執行(另開容器) - # post-entrypoint: '/cleanup.sh' # 選填,主程式後執行(另開容器) - env: # 選填,容器內環境變數 - GREETING: ${{ inputs.message }} - args: # 選填,傳給 ENTRYPOINT 的參數(取代 CMD) - - ${{ inputs.message }} - -branding: # 選填(Marketplace 用,Gitea 內部可省略) - icon: 'activity' - color: 'blue' -``` - -> 📌 `action.yml`(或 `action.yaml`)放在 action repo 根目錄;`image: 'Dockerfile'` 時,`Dockerfile` 也放在同一目錄。 - ---- - -## 頂層參數 - -| 參數 | 必填 | 說明 | -|------|------|------| -| `name` | ✅ | Action 名稱。 | -| `description` | ✅ | Action 簡短說明。 | -| `author` | ❌ | 作者名稱。 | -| `inputs` | ❌ | 輸入參數定義(見下)。 | -| `outputs` | ❌ | 輸出定義(見下)。 | -| `runs` | ✅ | 執行設定;docker 固定用 `using: 'docker'` + `image`。 | -| `branding` | ❌ | Marketplace 顯示用的 `icon` 與 `color`(`color` 限 `white`/`black`/`yellow`/`blue`/`green`/`orange`/`red`/`purple`/`gray-dark`;`icon` 為 Feather 圖示名稱)。 | - ---- - -## `inputs`(輸入參數) - -每個 input 是 `inputs.` 底下的一組設定: - -| 欄位 | 必填 | 說明 | -|------|------|------| -| `description` | ✅ | 參數說明。 | -| `required` | ❌ | 是否必填,布林值,預設 `false`。 | -| `default` | ❌ | 預設值;呼叫端沒傳時採用。**只能是字串**。 | -| `deprecationMessage` | ❌ | 標記此 input 已棄用,使用時發出警告訊息。 | - -### Docker action 怎麼取用 input - -這是 Docker action **與 composite action 最大的差異**。GitHub / Gitea 會把每個 input 轉成環境變數 `INPUT_`: - -- 名稱轉大寫、空白換成底線。例:input `octocat-eye-color` → 環境變數 `INPUT_OCTOCAT_EYE_COLOR`。 -- input `message` → `INPUT_MESSAGE`。 - -```yaml -inputs: - message: - description: '輸入訊息' - required: false - default: 'Hello, World!' -``` - -容器內就能直接讀: - -```sh -echo "$INPUT_MESSAGE" -``` - -> ⚠️ **關鍵限制**:`INPUT_*` 環境變數**只在 GitHub 官方 runner 保證自動注入**。若要跨環境(尤其 Gitea/`act`)可靠取值,官方建議**用 `args` 明確把 input 傳進容器**,或在 `runs.env` 自行對應一次(見下方 `runs.env`)。不要單靠 `INPUT_*` 而不驗證。 - -**呼叫端傳值**(用 `with`): - -```yaml -- uses: ./ - with: - message: 'Hi there' -``` - -> input 值一律是**字串**;數字、布林傳進來也會變字串(例如 `"true"`),比較時要留意。 - ---- - -## `outputs`(輸出) - -Docker action 的 output **只需宣告 `description`**,**不用**(也不能)像 composite 那樣寫 `value`: - -| 欄位 | 必填 | 說明 | -|------|------|------| -| `description` | ✅ | 輸出說明。 | - -**在容器內設定 output** → 寫入 `$GITHUB_OUTPUT` 檔案(該檔案路徑由 runner 掛載進容器): - -```yaml -outputs: - message: - description: '輸出訊息' -``` - -`entrypoint.sh` 內: - -```sh -echo "message=Hello from docker" >> "$GITHUB_OUTPUT" -``` - -**呼叫端取用 output**: - -```yaml -- id: docker-template - uses: ./ -- run: echo "${{ steps.docker-template.outputs.message }}" -``` - -> 📦 output 大小限制:單一 job 的 output 上限 **1 MB**,整個 workflow run 所有 output 合計上限 **50 MB**。 - ---- - -## `runs`(執行設定) - -Docker action 的核心。`using` 固定為 `docker`,其餘欄位: - -| 欄位 | 必填 | 說明 | -|------|------|------| -| `using` | ✅ | 固定為 `'docker'`。 | -| `image` | ✅ | 要跑的 image:本機 `Dockerfile`(檔名必須正好是 `Dockerfile`),或遠端 image 用 `docker://` 前綴(如 `docker://alpine:3.20`、`docker://gcr.io/...`)。 | -| `entrypoint` | ❌ | 覆寫 Dockerfile 的 `ENTRYPOINT`;Dockerfile 沒設時等於補上。建議用絕對路徑(如 `/entrypoint.sh`)。 | -| `pre-entrypoint` | ❌ | 在主 `entrypoint` **之前**執行的前置腳本。**會另開一個新容器**(同 base image),runtime 狀態與主容器不同——需保留的狀態要放進 workspace、`HOME`,或用 `STATE_` 變數傳遞。 | -| `post-entrypoint` | ❌ | 主 `entrypoint` 完成後執行的清理腳本,行為同 `pre-entrypoint`(另開容器)。 | -| `pre-if` | ❌ | 條件式,控制 `pre-entrypoint` 是否執行;預設一定跑。 | -| `post-if` | ❌ | 條件式,控制 `post-entrypoint` 是否執行;預設一定跑。 | -| `args` | ❌ | 字串陣列,啟動時傳給容器 `ENTRYPOINT` 的參數,**取代 Dockerfile 的 `CMD`**。 | -| `env` | ❌ | key/value map,容器啟動時設定的環境變數。 | - -### `image`:Dockerfile vs 遠端 image - -```yaml -# 用本機 Dockerfile(每次執行前會 build) -runs: - using: 'docker' - image: 'Dockerfile' - -# 直接拉遠端 image(不用自帶 Dockerfile,啟動快) -runs: - using: 'docker' - image: 'docker://alpine:3.20' -``` - -### `args`:怎麼把值送進容器 - -`args` 取代 `CMD`,會被當作參數接在 `ENTRYPOINT` 後面: - -```yaml -runs: - using: 'docker' - image: 'Dockerfile' - args: - - ${{ inputs.message }} # → entrypoint.sh 的 $1 - - 'foo' # → $2 - - 'bar' # → $3 -``` - -> ⚠️ 若 `args` 裡放的是**環境變數字串**(如 `- $GREETING`),在 exec-form 的 `ENTRYPOINT` **不會被展開**。要展開變數,讓 entrypoint 走一層 shell(`sh -c`),或改用 `runs.env` 傳值(見下)。 - -### `env`:明確傳環境變數(推薦) - -比起依賴 `INPUT_*` 自動注入,用 `runs.env` 把 input 對應成自訂環境變數,跨環境最穩: - -```yaml -runs: - using: 'docker' - image: 'Dockerfile' - env: - GREETING: ${{ inputs.message }} -``` - -容器內直接 `echo "$GREETING"`。 - ---- - -## `Dockerfile` 撰寫注意事項 - -Docker action 的 `Dockerfile` 有幾條**強制或強烈建議**的規則,踩到會直接失敗或讀不到檔案: - -1. **`FROM` 必須是第一行** - 建議用官方 image + 明確版本標籤(如 `python:3.12-slim`),別用 `latest`;Debian/Alpine 系列較穩。 - -2. **不要用 `USER`** - Docker action **必須以預設的 root 執行**。加了 `USER` 會導致**無法存取 `GITHUB_WORKSPACE`**(掛載進來的 repo 目錄)。 - -3. **不要用 `WORKDIR` 指定 entrypoint 位置** - runner 會自動把 `GITHUB_WORKSPACE` 掛載上來並設為工作目錄(路徑放在 `$GITHUB_WORKSPACE` 環境變數)。`entrypoint`/腳本一律用**絕對路徑**(如 `/entrypoint.sh`),不要依賴 `WORKDIR`。 - -4. **`ENTRYPOINT` 用 exec form(JSON 陣列)** - Docker 官方建議寫 `ENTRYPOINT ["/entrypoint.sh"]`。 - - **exec form**:`args` 能正確以獨立參數傳入,但**不做環境變數展開**(`ENTRYPOINT ["echo", "$GITHUB_SHA"]` 印出的是字面字串)。 - - **shell form**:`ENTRYPOINT /entrypoint.sh` 會走 shell,可展開變數,但 `args` 傳遞行為不同。 - - 需要在 entrypoint 展開變數時,用 `ENTRYPOINT ["sh", "-c", "echo $GITHUB_SHA"]`,或寫一支 `entrypoint.sh` 腳本自行處理。 - -5. **`CMD` 會被 `args` 蓋掉** - `action.yml` 的 `args` 取代 `CMD`。若 action 允許不帶 `args` 也能跑,就在 `Dockerfile` 的 `CMD` 提供預設值,並在 README 說明必要參數。 - -6. **`entrypoint.sh` 腳本規範** - - 開頭要有 shebang:`#!/bin/sh`(或 `#!/bin/bash`,視 base image 而定)。 - - 要可執行:`chmod +x entrypoint.sh`(並在 git 中保留執行權限)。 - - 腳本會收到 `action.yml` 的 `args` 作為位置參數(`$1`, `$2`, …)。 - ---- - -## Docker action 的限制與注意事項 - -以下是實務上最容易踩雷的地方: - -1. **只能跑在 Linux runner,且要有 Docker** - Windows / macOS runner 一律不支援 Docker container action。 - -2. **`INPUT_*` 不保證可靠,優先用 `args` / `env`** - GitHub 官方 runner 會自動注入 `INPUT_`,但這在自架 / Gitea 環境未必成立。要穩定取 input,用 `args` 傳位置參數,或 `runs.env` 對應成自訂環境變數。 - -3. **`pre-entrypoint` / `post-entrypoint` 是「另開容器」** - 它們**不共用主 entrypoint 容器的 runtime 狀態**(不是同一個容器內的前後腳本)。要跨階段保留狀態,寫進 `GITHUB_WORKSPACE`、`HOME`,或用 `STATE_` 變數。 - -4. **本機 `image: 'Dockerfile'` 每次會 build** - 用本機 Dockerfile 時,執行前會先 build image,較慢;想加速可改用 `docker://` 拉預先建好的 image。 - -5. **只有 `GITHUB_WORKSPACE` 是持久且共用的** - 容器內對檔案系統的改動,通常只有掛載進來的 `GITHUB_WORKSPACE` 會被後續 step 看到;其他路徑(如 `/tmp`)在跨 step / 跨 action 時不保證保留。 - -6. **exec-form `ENTRYPOINT` 不展開變數** - 如上「Dockerfile 注意事項」第 4 點,需要展開就走 `sh -c` 或 entrypoint 腳本。 - -7. **`args` 是字串陣列,順序即位置參數** - `args` 的順序對應 entrypoint 的 `$1`, `$2`…;輸入值全是字串。 - -8. **不支援 `runs.steps`** - 那是 composite action 專屬。Docker action 只有一個容器進入點(`entrypoint`)+選配的 pre/post。 - ---- - -## Gitea vs GitHub Actions 差異 - -Gitea Actions **不是** GitHub Actions 的 100% 複製品。撰寫 Docker action 時特別注意: - -| 項目 | Gitea 行為 | -|------|-----------| -| **runner 需求** | 執行 Docker action 的 `act_runner` 主機必須裝 Docker,且 runner label 對應到支援容器的環境。`runs-on` 只接受 `runs-on: xyz` 或 `runs-on: [xyz]`,不支援複雜表達式。 | -| **`pre-entrypoint` / `post-entrypoint`** | ⚠️ 早期 `act` 版本**完全不執行** pre/post-entrypoint(見 [`nektos/act#2363`](https://github.com/nektos/act/issues/2363),已於 PR #2394 修復)。Gitea 內建的 `act` 版本若較舊可能仍無效——**使用前務必在測試機驗證**。 | -| **`INPUT_*` 注入** | 是否自動注入 `INPUT_` 取決於 runner 版本,別假設一定有;優先用 `args` / `env` 明確傳值。 | -| **表達式函式** | 依官方比較文件,**僅保證支援 `always()`**;`success()` / `failure()` / `cancelled()` / `hashFiles()` 等視 `act` runner 版本而定,寫 `if:`(含 `pre-if` / `post-if`)前先在測試機驗證。 | -| **`uses` 支援絕對 URL** | 可寫 `uses: https://github.com/owner/repo@v1` 或 `uses: http://your_gitea/owner/repo@branch`,不限同站 action。 | -| **Go actions** | Gitea 額外支援 `using: 'go'` 寫 Go action(GitHub 沒有);`docker` / `node` / `composite` 皆支援。 | -| **context 檢查較寬鬆** | Gitea 不檢查 context 可用性,`env` context 可用在比 GitHub 更多的位置(但不代表可攜,跨到 GitHub 會失敗)。 | -| **被忽略的 job 欄位** | `jobs..timeout-minutes`、`jobs..continue-on-error`、`jobs..environment` 會被忽略。 | -| **annotations / problem matchers** | 不支援,會被忽略。 | -| **`permissions` scope** | 支援 `permissions`,但沒有 GitHub 專屬的 `statuses` / `checks` / `deployments` / `id-token` / `security-events` / `pages`;Gitea 有自己的 `code` / `releases` / `wiki` / `projects`。 | - -> 上表以 Gitea 官方文件為準;`act` runner 持續更新,部分限制(尤其表達式函式與 pre/post-entrypoint)可能隨版本放寬,仍以你環境的實測為準。 - ---- - -## 本 repo 範例對照 - -一個典型 Docker container action 由三個檔案組成,放在 repo 根目錄: - -**1. `action.yml`** — action 定義 - -```yaml -name: 'Gitea Docker Template' -description: 'Gitea Docker 範本' -author: 'Jeffery' -inputs: - message: - description: '輸入訊息' - required: false - default: 'Hello, World!' -outputs: - message: - description: '輸出訊息' -runs: - using: 'docker' - image: 'Dockerfile' - args: - - ${{ inputs.message }} -``` - -**2. `Dockerfile`** — 執行環境 - -```dockerfile -FROM alpine:3.20 - -COPY entrypoint.sh /entrypoint.sh -RUN chmod +x /entrypoint.sh - -ENTRYPOINT ["/entrypoint.sh"] -``` - -**3. `entrypoint.sh`** — 主程式 - -```sh -#!/bin/sh -set -e - -# $1 來自 action.yml 的 args(呼叫端 with.message) -MESSAGE="$1" - -echo "Docker action 收到訊息:$MESSAGE" - -# 設定 output 供後續 step 使用 -echo "message=$MESSAGE" >> "$GITHUB_OUTPUT" -``` - -**呼叫端**(workflow)用法: - -```yaml -- name: 3. Testing - id: docker-template - uses: ./ - with: - message: 'Hi there' -- name: 4. Feedback - run: echo "${{ steps.docker-template.outputs.message }}" -``` - -> ℹ️ 本 repo 目前的 [`action.yml`](./action.yml) 仍為 composite 範本;要改為 Docker action,依上方三件套調整 `action.yml` 並新增 `Dockerfile`、`entrypoint.sh`。 - ---- - -## 參考來源 - -- [GitHub Actions — Metadata syntax for actions](https://docs.github.com/en/actions/reference/workflows-and-actions/metadata-syntax) -- [GitHub Actions — Dockerfile support for GitHub Actions](https://docs.github.com/en/actions/sharing-automations/creating-actions/dockerfile-support-for-github-actions) -- [GitHub Actions — Creating a Docker container action](https://docs.github.com/en/actions/tutorials/creating-a-docker-container-action) -- [Gitea — Compared to GitHub Actions](https://docs.gitea.com/usage/actions/comparison) -- [Gitea — Actions FAQ](https://docs.gitea.com/usage/actions/faq) -- [nektos/act#2363 — pre/post-entrypoint of Docker actions not executed](https://github.com/nektos/act/issues/2363) From 276cd96b7de21b45a87e666c6317330de37634cc Mon Sep 17 00:00:00 2001 From: Jeffery Date: Sat, 11 Jul 2026 04:06:31 +0000 Subject: [PATCH 2/8] =?UTF-8?q?feat(calculate-version):=20=E6=94=B9?= =?UTF-8?q?=E7=82=BA=E5=BE=9E=20release=20API=20=E8=A8=88=E7=AE=97?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=B8=A6=E6=9B=B4=E6=96=B0=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yaml | 22 ++-- Dockerfile | 3 +- action.yml | 26 ++-- entrypoint.sh | 8 +- src/index.js | 278 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 305 insertions(+), 32 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index e7c6a45..1e875cd 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -10,17 +10,23 @@ jobs: name: 1. BUILD runs-on: ubuntu env: - VERSION: "0.0.0-beta.${{ gitea.run_number }}" IS_BETA: ${{ gitea.base_ref == 'develop' }} outputs: - version: ${{ env.VERSION }} + version: ${{ steps.calculate-version.outputs.version }} is_beta: ${{ env.IS_BETA }} steps: + - name: Source Code Checkout + uses: actions/checkout@${{ vars.ACTION_CHECKOUT_VERSION }} + - name: Run Calculate Version + id: calculate-version + uses: ./ + with: + is_beta: ${{ env.IS_BETA }} - name: Publishing Release uses: akkuman/gitea-release-action@${{ vars.ACTION_GITEA_RELEASE_VERSION }} with: - name: "${{ gitea.event.repository.name }} v${{ env.VERSION }}" - tag_name: "v${{ env.VERSION }}" + name: "${{ gitea.event.repository.name }} v${{ steps.calculate-version.outputs.version }}" + tag_name: "v${{ steps.calculate-version.outputs.version }}" target_commitish: ${{ gitea.sha }} prerelease: ${{ env.IS_BETA }} test: @@ -31,11 +37,11 @@ jobs: env: VERSION: ${{ needs.build.outputs.version }} outputs: - message: ${{ steps.docker-template.outputs.message }} + message: ${{ steps.calculate-version.outputs.version }} steps: - - name: Run Docker Template - id: docker-template - uses: https://gitea.jsc.idv.tw/actions/docker-template@v${{ env.VERSION }} + - name: Run Calculate Version + id: calculate-version + uses: https://gitea.jsc.idv.tw/actions/calculate-version@v${{ env.VERSION }} result: name: 3. RESULT runs-on: ubuntu diff --git a/Dockerfile b/Dockerfile index 6e02ce4..e4751be 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,5 @@ -ARG NODE_VERSION=alpine +FROM node:lts-alpine -FROM node:${NODE_VERSION} WORKDIR /action COPY src/ /action/src/ diff --git a/action.yml b/action.yml index 2a29e09..009edd3 100644 --- a/action.yml +++ b/action.yml @@ -1,14 +1,22 @@ -name: 'Gitea Docker Template' -description: 'Gitea Docker 範本' +name: 'Calculate Version' +description: '計算版本號' author: 'Jeffery' inputs: - message: - description: '輸入訊息' + is_beta: + description: '是否為 beta 版本' + default: 'false' + runner_token: + description: '用於讀取 release 的 token' required: false - default: 'Hello, World!' outputs: - message: - description: '輸出訊息' + version: + description: '計算出的版本號' runs: - using: docker - image: Dockerfile + using: 'docker' + image: 'Dockerfile' + env: + GITEA_SERVER_URL: ${{ gitea.server_url }} + GITEA_REPOSITORY: ${{ gitea.repository }} + GITEA_TOKEN: ${{ gitea.token }} + RUNNER_TOKEN: ${{ inputs.runner_token || secrets.GITEA_TOKEN || secrets.RUNNER_TOKEN }} + IS_BETA: ${{ inputs.is_beta }} diff --git a/entrypoint.sh b/entrypoint.sh index db76083..14d6d5a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,10 +1,4 @@ #!/bin/sh -set -e - -echo "================================================" -echo "Action : Gitea Docker Template" -echo "用途 : Gitea Docker 範本" -echo "更新時間: 2026/07/02 09:41:31" -echo "================================================" +set -eu exec node /action/src/index.js "$@" diff --git a/src/index.js b/src/index.js index 3727edc..87f9840 100644 --- a/src/index.js +++ b/src/index.js @@ -1,15 +1,281 @@ const fs = require('fs'); +const http = require('http'); +const https = require('https'); +const { URL } = require('url'); -function main() { - const message = process.env.INPUT_MESSAGE || ''; +const RELEASES_PER_PAGE = 10; + +function section(title) { + process.stdout.write(`\n==================================================\n${title}\n--------------------------------------------------\n`); +} + +function info(message) { + process.stdout.write(`[info] ${message}\n`); +} + +function fail(message) { + throw new Error(message); +} + +function requireEnv(name, value) { + if (!value || value === 'null') { + fail(`${name} 未設定`); + } + + return value; +} + +function normalizeBetaFlag(value = 'false') { + if (!value || value === 'null') { + return 'false'; + } + + return String(value); +} + +function writeOutput(version) { + const line = `version=${version}\n`; const outputPath = process.env.GITHUB_OUTPUT; - const line = `message=${message}\n`; if (outputPath) { fs.appendFileSync(outputPath, line); - } else { - process.stdout.write(line); + return; } + + process.stdout.write(line); } -main(); +function parseVersionParts(version) { + return String(version) + .split('.') + .map((part) => { + const value = Number.parseInt(part, 10); + return Number.isFinite(value) ? value : 0; + }); +} + +function compareVersionParts(left, right) { + const maxLength = Math.max(left.length, right.length); + + for (let index = 0; index < maxLength; index += 1) { + const a = left[index] ?? 0; + const b = right[index] ?? 0; + + if (a !== b) { + return a - b; + } + } + + return 0; +} + +function stableVersionFromTag(tagName) { + if (typeof tagName !== 'string' || tagName.includes('-beta.')) { + return null; + } + + const versionText = tagName.startsWith('v') ? tagName.slice(1) : tagName; + if (!/^[0-9]+(\.[0-9]+)*$/.test(versionText)) { + return null; + } + + return parseVersionParts(versionText); +} + +function latestStableVersion(releaseJson) { + if (!Array.isArray(releaseJson) || releaseJson.length === 0) { + return '0.0.0'; + } + + const stableVersions = releaseJson + .map((release) => stableVersionFromTag(release && release.tag_name)) + .filter(Boolean); + + if (stableVersions.length === 0) { + return '0.0.0'; + } + + stableVersions.sort(compareVersionParts); + const latest = stableVersions[stableVersions.length - 1]; + return `${latest[0] ?? 0}.${latest[1] ?? 0}.${latest[2] ?? 0}`; +} + +function nextReleaseVersion(latestVersion) { + const [majorRaw, minorRaw, patchRaw] = parseVersionParts(latestVersion); + let major = majorRaw ?? 0; + let minor = minorRaw ?? 0; + let patch = patchRaw ?? 0; + + patch += 1; + if (patch >= 10) { + patch = 0; + minor += 1; + } + + if (minor >= 10) { + minor = 0; + major += 1; + } + + return `${major}.${minor}.${patch}`; +} + +function nextBetaNumber(releaseJson, version) { + if (!Array.isArray(releaseJson) || releaseJson.length === 0) { + return 1; + } + + const prefix = `v${version}-beta.`; + const values = releaseJson + .map((release) => (release && typeof release.tag_name === 'string' ? release.tag_name : '')) + .filter((tagName) => tagName.startsWith(prefix)) + .map((tagName) => Number.parseInt(tagName.slice(prefix.length), 10)) + .filter((value) => Number.isInteger(value)); + + if (values.length === 0) { + return 1; + } + + return Math.max(...values) + 1; +} + +function calculateVersion(releaseJson, isBeta) { + if (!Array.isArray(releaseJson) || releaseJson.length === 0) { + return isBeta === 'true' + ? ['0.0.0', '0.0.1-beta.1'] + : ['0.0.0', '0.0.1']; + } + + const baseVersion = latestStableVersion(releaseJson); + const nextVersion = nextReleaseVersion(baseVersion); + + if (isBeta === 'true') { + const betaNumber = nextBetaNumber(releaseJson, nextVersion); + return [baseVersion, `${nextVersion}-beta.${betaNumber}`]; + } + + return [baseVersion, nextVersion]; +} + +function requestJson(url, token) { + return new Promise((resolve, reject) => { + const client = url.protocol === 'http:' ? http : https; + const headers = { + Accept: 'application/json', + }; + + if (token && token !== 'null') { + headers.Authorization = `token ${token}`; + } + + const request = client.request(url, { method: 'GET', headers }, (response) => { + let body = ''; + + response.setEncoding('utf8'); + response.on('data', (chunk) => { + body += chunk; + }); + response.on('end', () => { + if (response.statusCode < 200 || response.statusCode >= 300) { + reject(new Error(`release API 請求失敗 (${response.statusCode})`)); + return; + } + + if (body === '') { + resolve(''); + return; + } + + try { + resolve(JSON.parse(body)); + } catch (error) { + reject(new Error(`release API 回傳資料無法解析: ${error.message}`)); + } + }); + }); + + request.on('error', (error) => { + reject(error); + }); + + request.end(); + }); +} + +async function fetchReleases(baseUrl, repository, token) { + const combined = []; + let page = 1; + + while (true) { + const pageUrl = new URL(`/api/v1/repos/${repository}/releases`, baseUrl); + pageUrl.searchParams.set('limit', String(RELEASES_PER_PAGE)); + pageUrl.searchParams.set('page', String(page)); + + const pageJson = await requestJson(pageUrl, token); + + if (pageJson === '' || pageJson === null) { + break; + } + + if (!Array.isArray(pageJson)) { + throw new Error(`release API 回傳非陣列資料 (page=${page})`); + } + + info(`第 ${page} 頁取得 ${pageJson.length} 筆 release`); + combined.push(...pageJson); + + if (pageJson.length < RELEASES_PER_PAGE) { + break; + } + + page += 1; + } + + return combined; +} + +async function main() { + section('參數檢查'); + + const giteaServerUrl = requireEnv('GITEA_SERVER_URL', process.env.GITEA_SERVER_URL || ''); + const giteaRepository = requireEnv('GITEA_REPOSITORY', process.env.GITEA_REPOSITORY || ''); + const authToken = process.env.RUNNER_TOKEN || process.env.GITEA_TOKEN || ''; + const isBeta = normalizeBetaFlag(process.env.IS_BETA || 'false'); + + info(`RUNNER_TOKEN=${authToken && authToken !== 'null' ? '***' : '未提供'}`); + info(`IS_BETA=${isBeta}`); + + section('取得版本資料'); + + const releaseUrl = new URL(`/api/v1/repos/${giteaRepository}/releases`, `${giteaServerUrl.replace(/\/$/, '')}/`); + info(`RELEASE_URL=${releaseUrl.toString()}`); + + const releaseJson = await fetchReleases(giteaServerUrl, giteaRepository, authToken); + const [latestTag, newVersion] = calculateVersion(releaseJson, isBeta); + + info(`LATEST_VERSION=${latestTag}`); + + section('計算版本號'); + info(`NEW_VERSION=${newVersion}`); + writeOutput(newVersion); +} + +if (require.main === module) { + main().catch((error) => { + process.stderr.write(`[error] ${error.message}\n`); + process.exit(1); + }); +} + +module.exports = { + calculateVersion, + compareVersionParts, + fetchReleases, + latestStableVersion, + main, + nextBetaNumber, + nextReleaseVersion, + normalizeBetaFlag, + parseVersionParts, + stableVersionFromTag, +}; From 33e9548a6d6df8f82c817510dd244361c12074b8 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Sat, 11 Jul 2026 04:22:05 +0000 Subject: [PATCH 3/8] =?UTF-8?q?fix(CI=20=E5=B7=A5=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E7=A8=8B):=20=E4=BF=AE=E6=AD=A3=E7=89=88=E6=9C=AC=E8=BC=B8?= =?UTF-8?q?=E5=87=BA=E8=88=87=E6=B8=AC=E8=A9=A6=E6=AD=A5=E9=A9=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yaml | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 1e875cd..9ec621c 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -10,23 +10,17 @@ jobs: name: 1. BUILD runs-on: ubuntu env: + VERSION: "0.0.0-beta.${{ gitea.run_number }}" IS_BETA: ${{ gitea.base_ref == 'develop' }} outputs: - version: ${{ steps.calculate-version.outputs.version }} + version: ${{ env.VERSION }} is_beta: ${{ env.IS_BETA }} steps: - - name: Source Code Checkout - uses: actions/checkout@${{ vars.ACTION_CHECKOUT_VERSION }} - - name: Run Calculate Version - id: calculate-version - uses: ./ - with: - is_beta: ${{ env.IS_BETA }} - name: Publishing Release uses: akkuman/gitea-release-action@${{ vars.ACTION_GITEA_RELEASE_VERSION }} with: - name: "${{ gitea.event.repository.name }} v${{ steps.calculate-version.outputs.version }}" - tag_name: "v${{ steps.calculate-version.outputs.version }}" + name: "${{ gitea.event.repository.name }} v${{ env.VERSION }}" + tag_name: "v${{ env.VERSION }}" target_commitish: ${{ gitea.sha }} prerelease: ${{ env.IS_BETA }} test: @@ -37,17 +31,19 @@ jobs: env: VERSION: ${{ needs.build.outputs.version }} outputs: - message: ${{ steps.calculate-version.outputs.version }} + version: ${{ steps.calculate-version.outputs.version }} steps: - name: Run Calculate Version id: calculate-version uses: https://gitea.jsc.idv.tw/actions/calculate-version@v${{ env.VERSION }} + with: + is_beta: true result: name: 3. RESULT runs-on: ubuntu needs: [build,test] env: - MESSAGE: ${{ needs.test.outputs.message }} + VERSION: ${{ needs.test.outputs.version }} steps: - - name: Show Message - run: echo "$MESSAGE" + - name: Show Version + run: echo "$VERSION" From 517a517bc4e624f1527d037fc3a0b5ec1f4d3ecf Mon Sep 17 00:00:00 2001 From: Jeffery Date: Sat, 11 Jul 2026 05:17:44 +0000 Subject: [PATCH 4/8] =?UTF-8?q?fix(CI=20=E5=B7=A5=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E7=A8=8B):=20=E6=96=BC=E6=AD=A3=E5=BC=8F=E7=89=88=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E8=A3=9C=E4=B8=8A=E7=89=88=E6=9C=AC=E8=A8=88=E7=AE=97?= =?UTF-8?q?=E6=AD=A5=E9=A9=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 9ec621c..1f3523e 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -23,6 +23,10 @@ jobs: tag_name: "v${{ env.VERSION }}" target_commitish: ${{ gitea.sha }} prerelease: ${{ env.IS_BETA }} + - name: Run Calculate Version + id: calculate-version + if: ${{ env.IS_BETA == 'false' }} + uses: https://gitea.jsc.idv.tw/actions/calculate-version@v${{ env.VERSION }} test: name: 2. TEST runs-on: ubuntu From 2232f394aaec3c3b14f31207e460662057608056 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Sat, 11 Jul 2026 05:29:51 +0000 Subject: [PATCH 5/8] =?UTF-8?q?fix(CI=20=E5=B7=A5=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E7=A8=8B):=20=E5=B0=87=E6=AD=A3=E5=BC=8F=E7=89=88=E7=99=BC?= =?UTF-8?q?=E4=BD=88=E7=A7=BB=E5=88=B0=20result=20job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yaml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 1f3523e..cb1fa43 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -23,10 +23,6 @@ jobs: tag_name: "v${{ env.VERSION }}" target_commitish: ${{ gitea.sha }} prerelease: ${{ env.IS_BETA }} - - name: Run Calculate Version - id: calculate-version - if: ${{ env.IS_BETA == 'false' }} - uses: https://gitea.jsc.idv.tw/actions/calculate-version@v${{ env.VERSION }} test: name: 2. TEST runs-on: ubuntu @@ -45,9 +41,15 @@ jobs: result: name: 3. RESULT runs-on: ubuntu - needs: [build,test] + needs: [build] + if: ${{ needs.build.outputs.is_beta == 'false' }} env: - VERSION: ${{ needs.test.outputs.version }} + VERSION: ${{ needs.build.outputs.version }} steps: - - name: Show Version - run: echo "$VERSION" + - name: Publishing Release + uses: akkuman/gitea-release-action@${{ vars.ACTION_GITEA_RELEASE_VERSION }} + with: + name: "${{ gitea.event.repository.name }} v${{ env.VERSION }}" + tag_name: "v${{ env.VERSION }}" + target_commitish: ${{ gitea.sha }} + prerelease: false From fdfe09383ab9f00b07ef4c5217e94359c9fb719b Mon Sep 17 00:00:00 2001 From: Jeffery Date: Sat, 11 Jul 2026 05:45:06 +0000 Subject: [PATCH 6/8] =?UTF-8?q?fix(CI=20=E5=B7=A5=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E7=A8=8B):=20=E4=BF=AE=E6=AD=A3=E6=B8=AC=E8=A9=A6=E8=88=87?= =?UTF-8?q?=E7=99=BC=E4=BD=88=E6=B5=81=E7=A8=8B=E7=9A=84=20beta=20?= =?UTF-8?q?=E5=88=A4=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yaml | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index cb1fa43..15125b8 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -27,9 +27,9 @@ jobs: name: 2. TEST runs-on: ubuntu needs: [build] - if: ${{ needs.build.outputs.is_beta == 'true' }} env: - VERSION: ${{ needs.build.outputs.version }} + VERSION: ${{ steps.calculate-version.outputs.version }} + IS_BETA: ${{ needs.build.outputs.is_beta }} outputs: version: ${{ steps.calculate-version.outputs.version }} steps: @@ -37,14 +37,15 @@ jobs: id: calculate-version uses: https://gitea.jsc.idv.tw/actions/calculate-version@v${{ env.VERSION }} with: - is_beta: true + is_beta: ${{ env.IS_BETA == "true" }} result: name: 3. RESULT runs-on: ubuntu - needs: [build] - if: ${{ needs.build.outputs.is_beta == 'false' }} + needs: [build,test] + if: ${{ needs.test.outputs.is_beta == 'false' }} env: - VERSION: ${{ needs.build.outputs.version }} + VERSION: ${{ needs.test.outputs.version }} + IS_BETA: ${{ needs.build.outputs.is_beta }} steps: - name: Publishing Release uses: akkuman/gitea-release-action@${{ vars.ACTION_GITEA_RELEASE_VERSION }} @@ -52,4 +53,4 @@ jobs: name: "${{ gitea.event.repository.name }} v${{ env.VERSION }}" tag_name: "v${{ env.VERSION }}" target_commitish: ${{ gitea.sha }} - prerelease: false + prerelease: ${{ env.IS_BETA == "true" }} From c649a994d1f7eeaa8db94914c4e91b23610e5126 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Sat, 11 Jul 2026 05:46:30 +0000 Subject: [PATCH 7/8] =?UTF-8?q?fix(CI=20=E5=B7=A5=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E7=A8=8B):=20=E4=BF=AE=E6=AD=A3=E7=89=88=E6=9C=AC=E4=BE=86?= =?UTF-8?q?=E6=BA=90=E5=BC=95=E7=94=A8=E7=82=BA=20build=20=E8=BC=B8?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 15125b8..9978e3f 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -28,7 +28,7 @@ jobs: runs-on: ubuntu needs: [build] env: - VERSION: ${{ steps.calculate-version.outputs.version }} + VERSION: ${{ needs.build.outputs.version }} IS_BETA: ${{ needs.build.outputs.is_beta }} outputs: version: ${{ steps.calculate-version.outputs.version }} From fef95baa40c916bcc7933edee591220316ee3410 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Sat, 11 Jul 2026 05:48:41 +0000 Subject: [PATCH 8/8] =?UTF-8?q?fix(CI=20=E5=B7=A5=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E7=A8=8B):=20=E6=94=BE=E5=AF=AC=E6=AD=A3=E5=BC=8F=E7=89=88?= =?UTF-8?q?=E7=B5=90=E6=9E=9C=20job=20=E7=9A=84=E5=9F=B7=E8=A1=8C=E6=A2=9D?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 9978e3f..ea69542 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -42,7 +42,6 @@ jobs: name: 3. RESULT runs-on: ubuntu needs: [build,test] - if: ${{ needs.test.outputs.is_beta == 'false' }} env: VERSION: ${{ needs.test.outputs.version }} IS_BETA: ${{ needs.build.outputs.is_beta }}