調整:ci/cd 一致化,版本計算抽到 next-version #1
@@ -1,5 +1,5 @@
|
||||
name: cd
|
||||
# 只在「任何分支 → main」的 PR 合併時發版:用 release-go 的 next-version 算下一版,沒有跳過就在 PR 來源分支的最新提交(pull_request.head.sha)上打標籤並推送。
|
||||
# 只在「任何分支 → main」的 PR 合併時發版:action-cd 用 next-version 算下一版,沒有跳過就在 PR 來源分支的最新提交(pull_request.head.sha)上打標籤並推送。
|
||||
# branches 寫死 main,來源分支不限;PR 沒合併就關閉時,tag 工作在工作層級跳過。直接推送 main 不發版。
|
||||
# 版本只遞增 patch、每位最大 9,沒有標籤時從 v0.0.1 開始。只動文件、圖示、測試、.gitea/、go.mod 時不發版(這些不會進映像檔,也不影響使用者)。
|
||||
# 呼叫端用標籤引用這個 action(版本取自 Gitea 變數),所以發版後要到呼叫端把 ACTION_<名稱>_VERSION 改成新標籤才會生效。
|
||||
@@ -17,29 +17,6 @@ jobs:
|
||||
if: ${{ github.event.pull_request.merged == true }}
|
||||
runs-on: docker-ubuntu
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: https://gitea.jsc.idv.tw/actions/action-cd@${{ vars.ACTION_ACTION_CD_VERSION }}
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ref: ${{ github.event.pull_request.head.sha }}
|
||||
token: ${{ secrets.TOKEN || github.token }}
|
||||
- id: ver
|
||||
uses: https://gitea.jsc.idv.tw/actions/release-go@${{ vars.ACTION_RELEASE_GO_VERSION }}
|
||||
with:
|
||||
task: next-version
|
||||
channel: release
|
||||
go_registry: false
|
||||
docs_pattern: '^\.gitea/|^icon\.|^test/|^go\.mod$|\.md$'
|
||||
token: ${{ secrets.TOKEN || github.token }}
|
||||
- name: 只動文件等檔案,不發版
|
||||
if: ${{ steps.ver.outputs.skip == 'true' }}
|
||||
run: echo "不發版"
|
||||
- name: 打標籤並推送 ${{ steps.ver.outputs.version }}
|
||||
if: ${{ steps.ver.outputs.skip != 'true' }}
|
||||
env:
|
||||
VERSION: ${{ steps.ver.outputs.version }}
|
||||
TOKEN: ${{ secrets.TOKEN || github.token }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
git tag "$VERSION" HEAD
|
||||
git push "${GITHUB_SERVER_URL%%://*}://oauth2:${TOKEN}@${GITHUB_SERVER_URL#*://}/${GITHUB_REPOSITORY}.git" "refs/tags/$VERSION"
|
||||
echo "已推送標籤 $VERSION"
|
||||
|
||||
@@ -10,11 +10,8 @@ jobs:
|
||||
test:
|
||||
runs-on: docker-ubuntu
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: https://gitea.jsc.idv.tw/actions/action-ci@${{ vars.ACTION_ACTION_CI_VERSION }}
|
||||
with:
|
||||
go_test: true
|
||||
docker_build: true
|
||||
docker_run_options: |
|
||||
--env=INPUT_BRANCH=dev
|
||||
--env=INPUT_BASE_BRANCH=main
|
||||
|
||||
@@ -1,48 +1,57 @@
|
||||
# release-go
|
||||
|
||||
發佈流程的小步驟,`task` 選一個:`resolve-channel`(PR 來源與目標分支 → release/dev/none)、`next-version`(算下一版與是否跳過)、`deploy-version`(取得要部署的版本與提交)。
|
||||
發佈流程的小步驟,`task` 選一個:`resolve-channel`(PR 的來源與目標分支 → `release`/`dev`/`none`)、`check-version`(**Go 專用**:檢查版本號在 Gitea Go 套件庫是否可用,已存在就失敗)、`deploy-version`(取得要部署的版本與提交)。算版本號在 [next-version](https://gitea.jsc.idv.tw/actions/next-version);算出的版本再交給 `check-version` 確認套件庫沒有。
|
||||
|
||||
## 用法
|
||||
|
||||
版本由呼叫端的 Gitea Actions 變數決定(例如 `vars.ACTION_RELEASE_GO_VERSION`),不要用分支名稱:runner 依 ref 重用舊映像檔,改了腳本不會生效,所以每次改動都發新標籤。
|
||||
|
||||
```yaml
|
||||
- uses: https://gitea.jsc.idv.tw/actions/release-go@${{ vars.ACTION_RELEASE_GO_VERSION }}
|
||||
- id: ch
|
||||
uses: https://gitea.jsc.idv.tw/actions/release-go@${{ vars.ACTION_RELEASE_GO_VERSION }}
|
||||
with:
|
||||
task: ...
|
||||
task: resolve-channel
|
||||
branch: ${{ github.head_ref }}
|
||||
base_branch: ${{ github.base_ref }}
|
||||
main_branch: main
|
||||
dev_branch: dev
|
||||
- id: ver
|
||||
uses: https://gitea.jsc.idv.tw/actions/next-version@${{ vars.ACTION_NEXT_VERSION_VERSION }}
|
||||
- uses: https://gitea.jsc.idv.tw/actions/release-go@${{ vars.ACTION_RELEASE_GO_VERSION }} # Go 專用:版本在套件庫是否可用
|
||||
with:
|
||||
task: check-version
|
||||
version: ${{ steps.ver.outputs.version }}
|
||||
token: ${{ secrets.TOKEN || github.token }}
|
||||
```
|
||||
|
||||
## 輸入
|
||||
|
||||
|名稱|必填|預設|說明|
|
||||
|---|---|---|---|
|
||||
|`task`|是||resolve-channel、next-version、deploy-version|
|
||||
|`task`|是||resolve-channel、check-version、deploy-version|
|
||||
|`branch`|||resolve-channel:PR 的來源分支名稱(例如 github.head_ref)|
|
||||
|`base_branch`|||resolve-channel:PR 的目標分支名稱(例如 github.base_ref)|
|
||||
|`main_branch`|||resolve-channel:正式分支名稱(例如 main)|
|
||||
|`dev_branch`|||resolve-channel:測試分支名稱(例如 dev)|
|
||||
|`channel`|||next-version、deploy-version:release、dev 或 none(通常來自 resolve-channel;deploy-version 留空且沒指定版本=取這個提交上的版本標籤)|
|
||||
|`version`|||deploy-version:指定要部署的版本(例如 v0.5.4 或 v0.5.5-dev.2),有值就直接使用|
|
||||
|`package`|||next-version、deploy-version:Go 套件庫查詢的套件名稱(預設是儲存庫名稱)|
|
||||
|`go_registry`||`true`|next-version、deploy-version:false=不查 Go 套件庫,只看 git 標籤(不是 Go 套件的專案,例如 action repo 本身)|
|
||||
|`docs_pattern`|||next-version:只動這些檔案就不發佈的 egrep 規則(預設 ^docs/\|\.md$,即 docs/ 與 *.md)|
|
||||
|`token`|||next-version、deploy-version:查詢 Go 套件庫版本的權杖(需要 read:package)|
|
||||
|`channel`|||deploy-version:release、dev 或 none(通常來自 resolve-channel;deploy-version 留空且沒指定版本=取這個提交上的版本標籤)|
|
||||
|`version`|||check-version:要檢查的版本;deploy-version:指定要部署的版本(例如 v0.5.4 或 v0.5.5-dev.2),有值就直接使用|
|
||||
|`package`|||check-version:Go 套件名稱的最後一段(預設是儲存庫名稱);完整的模組路徑是「Gitea 主機/擁有者/名稱」|
|
||||
|`module`|||check-version:完整的 Go 模組路徑(套件名稱),有值就不用 package 組|
|
||||
|`token`|||check-version:查詢套件庫用的權杖(需要 read:package)|
|
||||
|
||||
## 輸出
|
||||
|
||||
|名稱|說明|
|
||||
|---|---|
|
||||
|`channel`|resolve-channel:release、dev 或 none|
|
||||
|`version`|next-version:下一版;deploy-version:要部署的版本(沒有時是空字串)|
|
||||
|`skip`|next-version:true 表示只動文件,不發佈;deploy-version:true 表示沒有版本可部署|
|
||||
|`version`|deploy-version:要部署的版本(沒有時是空字串)|
|
||||
|`available`|check-version:true=版本可用(失敗時這個 task 直接結束碼非 0)|
|
||||
|`skip`|deploy-version:true 表示沒有版本可部署|
|
||||
|`sha`|deploy-version:這次部署對應的提交(用來抓同一個提交的部署腳本)|
|
||||
|
||||
## 開發
|
||||
## 開發與發版
|
||||
|
||||
`ci.yaml` 用共用的 [action-ci](https://gitea.jsc.idv.tw/actions/action-ci) 跑 shellcheck、Go 黑箱測試,並建置映像檔、實際執行一次。
|
||||
|
||||
圖示:`icon.svg`(Gitea 儲存庫頭像用 `icon.png`)。
|
||||
|
||||
## 發版
|
||||
|
||||
只在「任何分支 → `main`」的 PR 合併時,`cd.yaml` 用 [release-go](https://gitea.jsc.idv.tw/actions/release-go) 算下一個版本,在 PR 來源分支的最新提交上打標籤(只遞增 patch,每位最大 9);只動文件、圖示、測試、`.gitea/`、`go.mod` 時不發版。直接推送 `main` 不發版。呼叫端要把 Gitea 變數 `ACTION_RELEASE_GO_VERSION` 改成新標籤才會採用。
|
||||
- CI(`ci.yaml`):用 [action-ci](https://gitea.jsc.idv.tw/actions/action-ci) 跑 shellcheck、Go 黑箱測試(`resolve-channel`),建置映像檔並實際執行一次。
|
||||
- CD(`cd.yaml`):「任何分支 → `main`」的 PR 合併時,用 [action-cd](https://gitea.jsc.idv.tw/actions/action-cd) 算下一個版本([next-version](https://gitea.jsc.idv.tw/actions/next-version),只遞增 patch、每位最大 9)並在 PR 來源最新提交上打標籤;只動文件、圖示、測試、`.gitea/`、`go.mod` 時不發版。直接推送 `main` 不發版。
|
||||
- 呼叫端要把 Gitea 變數 `ACTION_RELEASE_GO_VERSION` 改成新標籤才會採用。
|
||||
- 圖示:`icon.svg`(儲存庫頭像用 `icon.png`)。
|
||||
|
||||
+12
-13
@@ -1,8 +1,8 @@
|
||||
name: release-go
|
||||
description: "發佈流程的小步驟(Docker action,bash+git+curl):resolve-channel 依 PR 的來源與目標分支判斷通道、next-version 算下一版、deploy-version 取得要部署的版本。分支名稱、版本、token 都由呼叫端傳入。需要 actions/checkout(next-version、deploy-version 要 fetch-depth: 0)"
|
||||
description: "發佈流程的小步驟(Docker action,bash+git):resolve-channel 依 PR 的來源與目標分支判斷通道、check-version 檢查版本號在 Gitea Go 套件庫是否可用(Go 專用)、deploy-version 取得要部署的版本。算版本號在 next-version action。分支名稱、版本都由呼叫端傳入。需要 actions/checkout(deploy-version 要 fetch-depth: 0)"
|
||||
inputs:
|
||||
task:
|
||||
description: "resolve-channel、next-version、deploy-version"
|
||||
description: "resolve-channel、check-version、deploy-version"
|
||||
required: true
|
||||
branch:
|
||||
description: "resolve-channel:PR 的來源分支名稱(例如 github.head_ref)"
|
||||
@@ -17,30 +17,29 @@ inputs:
|
||||
description: "resolve-channel:測試分支名稱(例如 dev)"
|
||||
default: ""
|
||||
channel:
|
||||
description: "next-version、deploy-version:release、dev 或 none(通常來自 resolve-channel;deploy-version 留空且沒指定版本=取這個提交上的版本標籤)"
|
||||
description: "deploy-version:release、dev 或 none(通常來自 resolve-channel;deploy-version 留空且沒指定版本=取這個提交上的版本標籤)"
|
||||
default: ""
|
||||
version:
|
||||
description: "deploy-version:指定要部署的版本(例如 v0.5.4 或 v0.5.5-dev.2),有值就直接使用"
|
||||
description: "check-version:要檢查的版本;deploy-version:指定要部署的版本(例如 v0.5.4 或 v0.5.5-dev.2),有值就直接使用"
|
||||
default: ""
|
||||
package:
|
||||
description: "next-version、deploy-version:Go 套件庫查詢的套件名稱(預設是儲存庫名稱)"
|
||||
description: "check-version:Go 套件名稱的最後一段(預設是儲存庫名稱);完整的模組路徑是「Gitea 主機/擁有者/名稱」"
|
||||
default: ""
|
||||
go_registry:
|
||||
description: "next-version、deploy-version:false=不查 Go 套件庫,只看 git 標籤(不是 Go 套件的專案,例如 action repo 本身)"
|
||||
default: "true"
|
||||
docs_pattern:
|
||||
description: "next-version:只動這些檔案就不發佈的 egrep 規則(預設 ^docs/|\\.md$,即 docs/ 與 *.md)"
|
||||
module:
|
||||
description: "check-version:完整的 Go 模組路徑(套件名稱),有值就不用 package 組"
|
||||
default: ""
|
||||
token:
|
||||
description: "next-version、deploy-version:查詢 Go 套件庫版本的權杖(需要 read:package)"
|
||||
description: "check-version:查詢套件庫用的權杖(需要 read:package)"
|
||||
default: ""
|
||||
outputs:
|
||||
channel:
|
||||
description: "resolve-channel:release、dev 或 none"
|
||||
version:
|
||||
description: "next-version:下一版;deploy-version:要部署的版本(沒有時是空字串)"
|
||||
description: "deploy-version:要部署的版本(沒有時是空字串)"
|
||||
available:
|
||||
description: "check-version:true=版本可用(失敗時這個 task 直接結束碼非 0)"
|
||||
skip:
|
||||
description: "next-version:true 表示只動文件,不發佈;deploy-version:true 表示沒有版本可部署"
|
||||
description: "deploy-version:true 表示沒有版本可部署"
|
||||
sha:
|
||||
description: "deploy-version:這次部署對應的提交(用來抓同一個提交的部署腳本)"
|
||||
runs:
|
||||
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
# 檢查版本號在 Gitea 的 Go 套件庫是否可用(還沒被佔用)。Go 套件庫不能覆寫同一個版本,已存在就失敗。
|
||||
# [GITEA_TOKEN=<需要 read:package>] check-version.sh <Gitea 網址> <擁有者> <模組路徑> <版本>
|
||||
# 模組路徑是「Gitea 主機/擁有者/名稱」(也就是套件名稱)。
|
||||
# 結束碼 0=可用(套件庫沒有這個版本);1=已存在或查詢失敗(401、5xx、連不上都不當成可用,不猜)。
|
||||
set -euo pipefail
|
||||
base=${1:?用法:check-version.sh <Gitea 網址> <擁有者> <模組路徑> <版本>}; owner=${2:?缺少擁有者}; module=${3:?缺少模組路徑}; version=${4:?缺少版本}
|
||||
auth=()
|
||||
[ -z "${GITEA_TOKEN:-}" ] || auth=(-H "Authorization: token $GITEA_TOKEN")
|
||||
# 套件名稱與版本都要 URL 編碼(模組路徑裡的 / 變成 %2f)
|
||||
enc() { printf %s "$1" | sed 's#/#%2f#g; s#+#%2b#g'; }
|
||||
url="${base%/}/api/v1/packages/$owner/go/$(enc "$module")/$(enc "$version")"
|
||||
code=$(curl -sS -o /dev/null -w '%{http_code}' --max-time 20 "${auth[@]}" "$url") || { echo "查詢套件庫失敗:$url" >&2; exit 1; }
|
||||
case "$code" in
|
||||
404) echo "$module@$version 在 Go 套件庫還沒有,可以發佈" ;;
|
||||
200) echo "$module@$version 已經在 Go 套件庫了(Go 套件庫不能覆寫同一個版本);可能是手動發佈過,或前一次發佈上傳後沒推成標籤" >&2; exit 1 ;;
|
||||
*) echo "查詢套件庫失敗(HTTP $code):$url" >&2; exit 1 ;;
|
||||
esac
|
||||
+9
-21
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
# 用法:entrypoint.sh <task>;輸入從 INPUT_* 環境變數讀(Docker action 的標準做法);工作目錄是掛進來的 workspace
|
||||
# 用法:entrypoint.sh <task>(resolve-channel、check-version、deploy-version;算版本號在 next-version action);輸入從 INPUT_* 環境變數讀(Docker action 的標準做法);工作目錄是掛進來的 workspace
|
||||
set -euo pipefail
|
||||
task=${1:?task}
|
||||
cd "${GITHUB_WORKSPACE:-$PWD}"
|
||||
@@ -17,28 +17,16 @@ resolve-channel)
|
||||
out channel "$channel"
|
||||
echo "PR $head → ${base:-(無)}:$channel(正式分支:${main:-未設},測試分支:${dev:-未設})"
|
||||
;;
|
||||
next-version)
|
||||
flag=""
|
||||
[ "${INPUT_CHANNEL:-release}" != dev ] || flag="--dev"
|
||||
export GITEA_TOKEN=${INPUT_TOKEN:-}
|
||||
[ "${INPUT_GO_REGISTRY:-true}" = false ] || export PACKAGES_API="${GITHUB_SERVER_URL}/api/v1/packages/${GITHUB_REPOSITORY%%/*}?type=go&q=${INPUT_PACKAGE:-${GITHUB_REPOSITORY#*/}}"
|
||||
git fetch --tags --quiet origin
|
||||
latest=$("$(dirname "${BASH_SOURCE[0]}")/next-version.sh" --latest $flag)
|
||||
next=$("$(dirname "${BASH_SOURCE[0]}")/next-version.sh" $flag)
|
||||
skip=false
|
||||
if [ -n "$latest" ] && [ -n "$(git tag -l "$latest")" ]; then
|
||||
# 和最新標籤比較:全部是 docs/ 或 *.md 就跳過(也涵蓋前一次發佈失敗後的補發)。
|
||||
# 套件庫比標籤新(標籤沒推成功)時不跳過,照常發佈
|
||||
code=$(git diff --name-only "$latest" HEAD | grep -vE "${INPUT_DOCS_PATTERN:-^docs/|\.md\$}" || true)
|
||||
[ -z "$code" ] && skip=true
|
||||
fi
|
||||
out version "$next"
|
||||
out skip "$skip"
|
||||
echo "最新:${latest:-無},下一版:$next,跳過:$skip"
|
||||
check-version)
|
||||
# Go 專用:版本號(通常是 next-version 算的)在 Gitea Go 套件庫是否可用,已存在就失敗
|
||||
version=${INPUT_VERSION:?缺少 version}
|
||||
host=${GITHUB_SERVER_URL#*://}; host=${host%%/*}
|
||||
owner=${GITHUB_REPOSITORY%%/*}
|
||||
module=${INPUT_MODULE:-$host/$owner/${INPUT_PACKAGE:-${GITHUB_REPOSITORY#*/}}}
|
||||
GITEA_TOKEN=${INPUT_TOKEN:-} bash "$(dirname "${BASH_SOURCE[0]}")/check-version.sh" "$GITHUB_SERVER_URL" "$owner" "$module" "$version"
|
||||
out available true
|
||||
;;
|
||||
deploy-version)
|
||||
export GITEA_TOKEN=${INPUT_TOKEN:-}
|
||||
[ "${INPUT_GO_REGISTRY:-true}" = false ] || export PACKAGES_API="${GITHUB_SERVER_URL}/api/v1/packages/${GITHUB_REPOSITORY%%/*}?type=go&q=${INPUT_PACKAGE:-${GITHUB_REPOSITORY#*/}}"
|
||||
channel=${INPUT_CHANNEL:-}; version=${INPUT_VERSION:-}
|
||||
git fetch --tags --quiet origin
|
||||
sha=$(git rev-parse HEAD)
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# 讀現有 vX.Y.Z 標籤與 Go 套件庫已有版本中的最新版,輸出下一個版本:只遞增 patch,每位最大 9(v0.9.9 的下一版是 v1.0.0)。
|
||||
# 忽略不是 vX.Y.Z 的標籤(例如 v0.0.0-ci-probe);都沒有時輸出 v0.0.1。
|
||||
# 套件庫的版本也要算:手動發佈或標籤沒推成功時,套件庫會比標籤新,只看標籤會算出已存在的版本(上傳回 409)。
|
||||
# 設 PACKAGES_API(例如 https://gitea.jsc.idv.tw/api/v1/packages/<擁有者>?type=go&q=<套件名稱>)才查套件庫,
|
||||
# 需要驗證時帶 GITEA_TOKEN;查詢失敗就整個失敗,不猜。
|
||||
# next-version.sh --latest 只輸出目前最新版(含套件庫),沒有時輸出空字串
|
||||
# 測試分支用 --dev:版本是「下一個正式版」加 -dev.N(例如最新正式版 v0.4.8 → v0.4.9-dev.1、v0.4.9-dev.2…),N 沒有上限;
|
||||
# 正式版發佈後基底往前,N 從 1 重算。--latest --dev 輸出該基底目前最新的 dev 版,還沒有就輸出最新正式版。
|
||||
set -euo pipefail
|
||||
|
||||
registry_versions() {
|
||||
[ -n "${PACKAGES_API:-}" ] || return 0
|
||||
local page=1 body found
|
||||
local auth=()
|
||||
[ -z "${GITEA_TOKEN:-}" ] || auth=(-H "Authorization: token $GITEA_TOKEN")
|
||||
while :; do
|
||||
body=$(curl -fsS --max-time 20 "${auth[@]}" "$PACKAGES_API&limit=50&page=$page") || { echo "查詢套件庫版本失敗:$PACKAGES_API" >&2; return 1; }
|
||||
found=$(printf %s "$body" | grep -oE '"version" *: *"v[0-9]+\.[0-9]+\.[0-9]+(-dev\.[0-9]+)?"' | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+(-dev\.[0-9]+)?' || true)
|
||||
[ -n "$found" ] || return 0
|
||||
printf '%s\n' "$found"
|
||||
page=$((page + 1))
|
||||
done
|
||||
}
|
||||
|
||||
dev=false latest_only=false
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--dev) dev=true ;;
|
||||
--latest) latest_only=true ;;
|
||||
*) echo "不認得的參數:$arg" >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
registry=$(registry_versions)
|
||||
all=$({ git tag -l 'v*'; printf '%s\n' "$registry"; })
|
||||
latest=$(printf '%s\n' "$all" | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1 || true)
|
||||
|
||||
# 正式版的下一版:只遞增 patch,每位最大 9
|
||||
bump() {
|
||||
local major minor patch
|
||||
IFS=. read -r major minor patch <<<"${1#v}"
|
||||
patch=$((patch + 1))
|
||||
if [ "$patch" -gt 9 ]; then
|
||||
patch=0
|
||||
minor=$((minor + 1))
|
||||
fi
|
||||
if [ "$minor" -gt 9 ]; then
|
||||
minor=0
|
||||
major=$((major + 1))
|
||||
fi
|
||||
echo "v$major.$minor.$patch"
|
||||
}
|
||||
|
||||
next=v0.0.1
|
||||
[ -z "$latest" ] || next=$(bump "$latest")
|
||||
|
||||
if [ "$dev" = true ]; then
|
||||
last_dev=$(printf '%s\n' "$all" | grep -E "^${next//./\\.}-dev\.[0-9]+\$" | sort -V | tail -n 1 || true)
|
||||
if [ "$latest_only" = true ]; then
|
||||
echo "${last_dev:-$latest}"
|
||||
else
|
||||
n=1
|
||||
[ -z "$last_dev" ] || n=$((${last_dev##*-dev.} + 1))
|
||||
echo "$next-dev.$n"
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
if [ "$latest_only" = true ]; then
|
||||
echo "$latest"
|
||||
exit 0
|
||||
fi
|
||||
echo "$next"
|
||||
@@ -1,136 +0,0 @@
|
||||
// next-version.sh 的測試:在臨時 git 倉庫建立標籤,檢查輸出的下一個版本。
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func nextVersion(t *testing.T, tags ...string) string { return nextVersionArgs(t, nil, tags...) }
|
||||
|
||||
func nextVersionArgs(t *testing.T, args []string, tags ...string) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
run := func(args ...string) {
|
||||
cmd := exec.Command("git", args...)
|
||||
cmd.Dir = dir
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("git %v:%v\n%s", args, err, out)
|
||||
}
|
||||
}
|
||||
run("init", "-q")
|
||||
run("-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "--allow-empty", "-m", "init")
|
||||
for _, tag := range tags {
|
||||
run("tag", tag)
|
||||
}
|
||||
script, err := filepath.Abs("../next-version.sh")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd := exec.Command("bash", append([]string{script}, args...)...)
|
||||
cmd.Dir = dir
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("next-version.sh:%v\n%s", err, out)
|
||||
}
|
||||
return strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
func TestNextVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tags []string
|
||||
want string
|
||||
}{
|
||||
{"無標籤", nil, "v0.0.1"},
|
||||
{"遞增 patch", []string{"v0.4.7", "v0.4.8"}, "v0.4.9"},
|
||||
{"patch 進位到 minor", []string{"v0.4.9"}, "v0.5.0"},
|
||||
{"minor 進位到 major", []string{"v0.9.9"}, "v1.0.0"},
|
||||
{"取最新而非最後建立", []string{"v0.5.0", "v0.4.8", "v0.4.9"}, "v0.5.1"},
|
||||
{"忽略探測標籤", []string{"v0.4.8", "v0.0.0-ci-probe", "v9.9.9-rc1"}, "v0.4.9"},
|
||||
{"只有探測標籤", []string{"v0.0.0-ci-probe"}, "v0.0.1"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := nextVersion(t, tt.tags...); got != tt.want {
|
||||
t.Fatalf("得到 %s,預期 %s", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 套件庫比標籤新(手動發佈、標籤沒推成功)時,下一版要接在套件庫的最新版後面。
|
||||
func TestNextVersionCountsRegistry(t *testing.T) {
|
||||
pages := map[string]string{
|
||||
"1": `[{"name":"m","version":"v0.4.9"},{"name":"m","version":"v0.5.0"},{"name":"m","version":"v0.5.0-rc1"}]`,
|
||||
"2": `[]`,
|
||||
}
|
||||
var auth string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
auth = r.Header.Get("Authorization")
|
||||
fmt.Fprint(w, pages[r.URL.Query().Get("page")])
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
dir := t.TempDir()
|
||||
for _, args := range [][]string{{"init", "-q"}, {"-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "--allow-empty", "-m", "i"}, {"tag", "v0.4.8"}} {
|
||||
cmd := exec.Command("git", args...)
|
||||
cmd.Dir = dir
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("git %v:%v\n%s", args, err, out)
|
||||
}
|
||||
}
|
||||
script, _ := filepath.Abs("../next-version.sh")
|
||||
run := func(api string, args ...string) (string, error) {
|
||||
cmd := exec.Command("bash", append([]string{script}, args...)...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(cmd.Environ(), "PACKAGES_API="+api, "GITEA_TOKEN=tok")
|
||||
out, err := cmd.Output()
|
||||
return strings.TrimSpace(string(out)), err
|
||||
}
|
||||
if got, err := run(srv.URL + "/?type=go"); err != nil || got != "v0.5.1" {
|
||||
t.Fatalf("得到 %q(%v),預期 v0.5.1", got, err)
|
||||
}
|
||||
if auth != "token tok" {
|
||||
t.Fatalf("Authorization = %q", auth)
|
||||
}
|
||||
if got, err := run(srv.URL+"/?type=go", "--latest"); err != nil || got != "v0.5.0" {
|
||||
t.Fatalf("--latest 得到 %q(%v),預期 v0.5.0", got, err)
|
||||
}
|
||||
// 查詢失敗要整個失敗,不能退回只看標籤而算出已存在的版本
|
||||
if got, err := run("http://127.0.0.1:1/?type=go"); err == nil {
|
||||
t.Fatalf("套件庫連不上卻成功:%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// 測試分支:下一個正式版加 -dev.N,N 沒有上限,正式版發佈後從 1 重算。
|
||||
func TestNextDevVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
tags []string
|
||||
want string
|
||||
}{
|
||||
{"無標籤", []string{"--dev"}, nil, "v0.0.1-dev.1"},
|
||||
{"第一個 dev", []string{"--dev"}, []string{"v0.4.8"}, "v0.4.9-dev.1"},
|
||||
{"遞增 N", []string{"--dev"}, []string{"v0.4.8", "v0.4.9-dev.1", "v0.4.9-dev.2"}, "v0.4.9-dev.3"},
|
||||
{"N 超過 9 不進位", []string{"--dev"}, []string{"v0.4.8", "v0.4.9-dev.9", "v0.4.9-dev.10"}, "v0.4.9-dev.11"},
|
||||
{"基底進位", []string{"--dev"}, []string{"v0.4.9"}, "v0.5.0-dev.1"},
|
||||
{"正式版發佈後 N 重算", []string{"--dev"}, []string{"v0.4.8", "v0.4.9-dev.7", "v0.4.9"}, "v0.5.0-dev.1"},
|
||||
{"正式版不受 dev 影響", nil, []string{"v0.4.8", "v0.4.9-dev.3"}, "v0.4.9"},
|
||||
{"最新 dev", []string{"--latest", "--dev"}, []string{"v0.4.8", "v0.4.9-dev.2", "v0.4.9-dev.10"}, "v0.4.9-dev.10"},
|
||||
{"沒有 dev 時最新是正式版", []string{"--latest", "--dev"}, []string{"v0.4.8"}, "v0.4.8"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := nextVersionArgs(t, tt.args, tt.tags...); got != tt.want {
|
||||
t.Fatalf("得到 %s,預期 %s", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// release-go 的黑箱測試:resolve-channel(PR 的來源與目標分支 → 發佈通道)與 check-version(版本號在 Go 套件庫是否可用)。
|
||||
package test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func resolve(t *testing.T, head, base, main, dev string) string {
|
||||
t.Helper()
|
||||
script, _ := filepath.Abs("../entrypoint.sh")
|
||||
out := filepath.Join(t.TempDir(), "out")
|
||||
cmd := exec.Command("bash", script, "resolve-channel")
|
||||
cmd.Dir = t.TempDir()
|
||||
cmd.Env = append(os.Environ(), "GITHUB_WORKSPACE="+cmd.Dir, "GITHUB_OUTPUT="+out,
|
||||
"INPUT_BRANCH="+head, "INPUT_BASE_BRANCH="+base, "INPUT_MAIN_BRANCH="+main, "INPUT_DEV_BRANCH="+dev)
|
||||
if b, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("resolve-channel: %v\n%s", err, b)
|
||||
}
|
||||
b, _ := os.ReadFile(out)
|
||||
return strings.TrimSpace(strings.TrimPrefix(string(b), "channel="))
|
||||
}
|
||||
|
||||
func TestResolveChannel(t *testing.T) {
|
||||
cases := []struct{ head, base, main, dev, want string }{
|
||||
{"dev", "main", "main", "dev", "release"}, // 測試分支 → 正式分支
|
||||
{"feature/x", "dev", "main", "dev", "dev"}, // 任何分支 → 測試分支
|
||||
{"main", "dev", "main", "dev", "none"}, // 正式分支 → 測試分支是同步回去,不發佈
|
||||
{"feature/x", "main", "main", "dev", "none"}, // 不是 dev 來的 PR 進 main
|
||||
{"feature/x", "other", "main", "dev", "none"}, // 其他目標
|
||||
{"dev", "main", "", "", "none"}, // 沒設分支名稱
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := resolve(t, c.head, c.base, c.main, c.dev); got != c.want {
|
||||
t.Errorf("%s → %s:got %q want %q", c.head, c.base, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check-version.sh:假的 Gitea 套件 API,檢查可用、已存在、查詢失敗與 URL 編碼、權杖。
|
||||
func TestCheckVersion(t *testing.T) {
|
||||
var gotPath, auth string
|
||||
status := map[string]int{
|
||||
"/api/v1/packages/acme/go/git.example.org%2facme%2fvault/v0.4.9": http.StatusOK,
|
||||
"/api/v1/packages/acme/go/git.example.org%2facme%2fvault/v0.5.0": http.StatusNotFound,
|
||||
"/api/v1/packages/acme/go/git.example.org%2facme%2fvault/v0.6.0": http.StatusUnauthorized,
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath, auth = r.URL.EscapedPath(), r.Header.Get("Authorization")
|
||||
code, ok := status[gotPath]
|
||||
if !ok {
|
||||
code = http.StatusInternalServerError
|
||||
}
|
||||
w.WriteHeader(code)
|
||||
}))
|
||||
defer srv.Close()
|
||||
script, _ := filepath.Abs("../check-version.sh")
|
||||
run := func(base, version string) (string, error) {
|
||||
cmd := exec.Command("bash", script, base, "acme", "git.example.org/acme/vault", version)
|
||||
cmd.Env = append(os.Environ(), "GITEA_TOKEN=tok")
|
||||
out, err := cmd.CombinedOutput()
|
||||
return string(out), err
|
||||
}
|
||||
if out, err := run(srv.URL, "v0.5.0"); err != nil {
|
||||
t.Fatalf("套件庫沒有的版本應該可用:%v\n%s", err, out)
|
||||
}
|
||||
if gotPath != "/api/v1/packages/acme/go/git.example.org%2facme%2fvault/v0.5.0" || auth != "token tok" {
|
||||
t.Fatalf("請求不對:path=%q auth=%q", gotPath, auth)
|
||||
}
|
||||
if out, err := run(srv.URL, "v0.4.9"); err == nil || !strings.Contains(out, "已經在 Go 套件庫") {
|
||||
t.Fatalf("已存在的版本應該失敗:%v\n%s", err, out)
|
||||
}
|
||||
// 查詢本身失敗(401、5xx、連不上)不能當成可用
|
||||
for _, v := range []string{"v0.6.0", "v9.9.9"} {
|
||||
if out, err := run(srv.URL, v); err == nil {
|
||||
t.Fatalf("%s 查詢失敗卻當成可用:%s", v, out)
|
||||
}
|
||||
}
|
||||
if out, err := run("http://127.0.0.1:1", "v0.5.0"); err == nil {
|
||||
t.Fatalf("套件庫連不上卻當成可用:%s", out)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user