Files
calculate-version/Dockerfile
T

43 lines
2.3 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# syntax=docker/dockerfile:1
# =============================================================================
# 用途: 建置 calculate-version Action 的容器映像。
# 採多階段建置:build 階段以最新 node 準備主程式 (/app) 與進入點腳本,
# runtime 階段改用 node slim 基底縮小最終映像,再由 entrypoint.sh 啟動 Node.js 主程式。
# 更新日期: 2026/06/30 12:12:52
# =============================================================================
# 1. 參數處理:node 版本以 ARG 注入。預設使用最新版 (build: node:latest / runtime: node:slim)
# 可於建置時以 --build-arg NODE_VERSION=22 --build-arg NODE_RUNTIME=22-slim 覆寫。
# 注意:最新版 slim 一律為 node:slim,無 node:latest-slim 這個 tag,故以獨立 NODE_RUNTIME 帶入。
ARG NODE_VERSION=latest
ARG NODE_RUNTIME=slim
# ---- build 階段:安裝相依、準備主程式與進入點 ----
FROM node:${NODE_VERSION} AS build
WORKDIR /build
# 2. 安裝套件:先單獨複製相依描述以利 layer 快取,再安裝 production 相依。
# 本專案目前無外部相依;有 package.json 時仍嘗試安裝 (有 lockfile 用 npm ci,否則 npm install)
# 無相依時為 no-op,但保留此步可在未來新增相依時自動生效。
COPY app/package*.json ./app/
RUN if [ -f ./app/package.json ]; then \
cd ./app && (npm ci --omit=dev || npm install --omit=dev); \
fi
# 3. 複製檔案:複製主程式 (app/) 與容器進入點腳本。
COPY app/ ./app/
COPY entrypoint.sh ./entrypoint.sh
# 4. 執行程序:賦予進入點腳本可執行權限。本專案為純 JS,無需 build/transpile,故此步僅設定權限。
RUN chmod +x ./entrypoint.sh
# ---- runtime 階段:以 slim 基底縮小最終映像 ----
# 5. 縮小映像檔:多階段建置,runtime 僅自 build 階段帶入執行真正需要的檔案 (app/ 與 entrypoint.sh)
# 不含 build 階段的快取。node slim 為 Debian 基底,內建 bash,足以執行 entrypoint.sh。
FROM node:${NODE_RUNTIME} AS runtime
COPY --from=build /build/app /app
COPY --from=build /build/entrypoint.sh /entrypoint.sh
# 6. 設定入口:以 entrypoint.sh 為容器進入點 (其內以 exec node /app/index.js "$@" 啟動主程式)。
ENTRYPOINT ["/entrypoint.sh"]