feat(啟動橫幅與 log 格式): 啟動輸出 Action 資訊橫幅並統一 log 訊息為 [等級][時間] 格式

This commit is contained in:
Jeffery
2026-06-30 12:42:11 +08:00
parent b14b676d0c
commit 2e5c46b893
2 changed files with 58 additions and 7 deletions
+43 -4
View File
@@ -4,39 +4,78 @@
const LINE = '='.repeat(50);
const SUBLINE = '-'.repeat(50);
/**
* 產生台灣時區(Asia/Taipei)的時間戳,格式固定為 `yyyy/MM/dd HH:mm:ss`。
*
* 供 log 訊息統一格式 `[{等級}][{時間}]: {訊息}` 的時間欄位使用。
*
* @returns {string} 台灣時區當下時間字串,例如 `2026/06/30 12:13:04`。
*/
function taipeiTimestamp() {
const parts = new Intl.DateTimeFormat('zh-TW', {
timeZone: 'Asia/Taipei',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
}).formatToParts(new Date());
const get = (type) => parts.find((part) => part.type === type)?.value ?? '';
// 部分執行環境會把 24 時制的午夜輸出為 "24",統一正規化為 "00"
const hour = get('hour') === '24' ? '00' : get('hour');
return `${get('year')}/${get('month')}/${get('day')} ${hour}:${get('minute')}:${get('second')}`;
}
/**
* 輸出帶標題的區塊段落至標準輸出,標題前後以分隔線包夾,
* 用於在 log 中建立可視的段落區隔。
*
* 輸出格式為:換行 + 主分隔線(50 個 `=`)+ 標題 + 次分隔線(50 個 `-`)。
* 此為結構性段落標題(非 INF/WRN/ERR 等級訊息),故不套用 `[{等級}][{時間}]` 前綴。
*
* @param {string} title - 區塊標題文字;會原樣輸出於兩條分隔線之間。
* @returns {void}
* @remarks 通常在進入一個處理階段前呼叫(例如「參數檢查」「取得版本資料」),
* 用以在 CI/容器 log 中分隔各階段輸出,便於閱讀與定位。
*/
function section(title) {
process.stdout.write(`\n${LINE}\n${title}\n${SUBLINE}\n`);
}
/**
* 輸出一般資訊層級的 log 訊息至標準輸出,自動加上 `[info]` 前綴與換行
* 輸出一般資訊INF層級的 log 訊息至標準輸出。
*
* 訊息格式統一為 `[INF][{台灣時間}]: {訊息}`,時間使用台灣時區(Asia/Taipei),
* 格式為 `yyyy/MM/dd HH:mm:ss`,並於結尾換行。
*
* @param {string} message - 要輸出的資訊內容。
* @returns {void}
* @remarks 用於回報正常流程進度(如設定值、URL、各頁取得筆數);此調整僅變更輸出前綴格式,
* 不改變訊息所反映的實際行為與內容。
*/
function info(message) {
process.stdout.write(`[info] ${message}\n`);
process.stdout.write(`[INF][${taipeiTimestamp()}]: ${message}\n`);
}
/**
* 輸出錯誤層級的 log 訊息至標準錯誤輸出(stderr),自動加上 `[error]` 前綴與換行
* 輸出錯誤ERR層級的 log 訊息至標準錯誤輸出(stderr)。
*
* 訊息格式統一為 `[ERR][{台灣時間}]: {訊息}`,時間使用台灣時區(Asia/Taipei),
* 格式為 `yyyy/MM/dd HH:mm:ss`,並於結尾換行。
*
* 僅負責輸出,不終止行程;是否結束由呼叫端(進入點)決定,以利測試與錯誤復原。
*
* @param {string} message - 要輸出的錯誤描述內容。
* @returns {void}
* @remarks 由進入點在頂層捕捉到例外時呼叫,輸出至 stderr 後再由呼叫端決定 exit code
* 此調整僅變更輸出前綴格式,不改變訊息所反映的實際行為與內容。
*/
function error(message) {
process.stderr.write(`[error] ${message}\n`);
process.stderr.write(`[ERR][${taipeiTimestamp()}]: ${message}\n`);
}
module.exports = { section, info, error };
Regular → Executable
+15 -3
View File
@@ -1,15 +1,27 @@
#!/bin/bash
# =============================================================================
# 用途: calculate-version Action 的容器進入點 (entrypoint)。
# 專案已由 bash 改寫為 Node.js,本腳本僅負責啟動 /app/index.js,
# 並將容器收到的引數原封傳遞給 Node.js 程式執行版本計算邏輯
# 更新日期: 2026/06/26 10:28:36
# 專案已由 bash 改寫為 Node.js,本腳本先輸出 Action 資訊橫幅,
# 再啟動 /app/index.js,並將容器收到的引數原封傳遞給 Node.js 程式。
# 更新日期: 2026/06/30 12:12:56
# =============================================================================
# set -e: 任一指令失敗即中止; set -u: 使用未定義變數即報錯;
# set -o pipefail: 管線中任一指令失敗即視為整體失敗。確保錯誤能即時暴露。
set -euo pipefail
# Action 基本資訊:與 action.yaml 的 name/description 一致;更新時間為本腳本最後異動的台灣時間。
ACTION_NAME='Calculate Version'
ACTION_DESC='計算版本號'
ACTION_UPDATED='2026/06/30 12:12:56'
# 啟動時印出 Action 名稱 / 用途 / 更新時間橫幅,方便在 CI/容器 log 中辨識本次執行。
printf '\n%s\n' '=================================================='
printf '名稱 : %s\n' "$ACTION_NAME"
printf '用途 : %s\n' "$ACTION_DESC"
printf '更新 : %s\n' "$ACTION_UPDATED"
printf '%s\n' '=================================================='
# 進入點:實際邏輯改以 Node.js 實作,置於 /app
# 以 exec 取代當前 shell,讓 node 成為 PID 1 正確接收訊號;
# "$@" 將容器接收到的所有引數原樣轉交給 Node.js 程式。