build: doker镜像构建时记录构建时间与gitcommit
All checks were successful
deploy to server / build-and-deploy (push) Successful in 2m59s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 2m59s
- 镜像构建时会向项目目录内的version.json写入构建时间与commit-sha,并在镜像启动时输出日志 - 添加用于获取版本号的server api
This commit is contained in:
12
Dockerfile
12
Dockerfile
@ -32,10 +32,20 @@ WORKDIR /app
|
|||||||
# COPY .output folder
|
# COPY .output folder
|
||||||
COPY --from=build /app/.output ./
|
COPY --from=build /app/.output ./
|
||||||
|
|
||||||
|
|
||||||
|
ARG BUILD_TIME
|
||||||
|
ARG GIT_COMMIT
|
||||||
|
|
||||||
|
RUN echo "{\"buildTime\":\"$BUILD_TIME\",\"gitCommit\":\"$GIT_COMMIT\"}" > /app/version.json
|
||||||
|
|
||||||
|
|
||||||
|
COPY entrypoint.sh /entrypoint.sh
|
||||||
|
RUN chmod +x /entrypoint.sh
|
||||||
|
|
||||||
ENV PORT=3000
|
ENV PORT=3000
|
||||||
ENV NITRO_PRESET=node-server
|
ENV NITRO_PRESET=node-server
|
||||||
ENV HOST=0.0.0.0
|
ENV HOST=0.0.0.0
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
ENTRYPOINT ["/entrypoint.sh"]
|
||||||
CMD ["node", "/app/server/index.mjs"]
|
CMD ["node", "/app/server/index.mjs"]
|
||||||
|
|||||||
63
docker-build.sh
Executable file
63
docker-build.sh
Executable file
@ -0,0 +1,63 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# ========== 脚本配置 ==========
|
||||||
|
#
|
||||||
|
IMAGE_NAME="git.jinshen.cn/remilia/jinshen-website" # 含仓库前缀的镜像名称
|
||||||
|
DOCKERFILE="Dockerfile" # Dockerfile 文件路径
|
||||||
|
BUILD_CONTEXT="." # 构建上下文路径
|
||||||
|
PUSH_IMAGE=true # 是否推送镜像到远程仓库
|
||||||
|
DOCKER_LOGIN=false # 是否需要登录 Docker 仓库
|
||||||
|
REGISTRY_USER="" #Docker 仓库用户名
|
||||||
|
REGISTRY_PASSWORD="" #Docker 仓库密码
|
||||||
|
|
||||||
|
# ==============================
|
||||||
|
|
||||||
|
# 生成时间戳TAG
|
||||||
|
TIMESTAMP_TAG=$(date +'%Y%m%d%H%M%S')
|
||||||
|
BUILD_TIME=$(date +"%Y-%m-%d %H:%M:%S")
|
||||||
|
GIT_COMMIT=$(git rev-parse --short HEAD || echo "unknown")
|
||||||
|
|
||||||
|
echo "📦 构建镜像: ${IMAGE_NAME}"
|
||||||
|
echo "⏱️ 时间戳标签: ${TIMESTAMP_TAG}"
|
||||||
|
echo "🔖 Git 提交: ${GIT_COMMIT}"
|
||||||
|
echo "⭐ 同时构建 latest 标签"
|
||||||
|
|
||||||
|
# docker login(如果启用)
|
||||||
|
if [ "$DOCKER_LOGIN" = true ]; then
|
||||||
|
if [ -z "$REGISTRY_PASSWORD" ]; then
|
||||||
|
echo "❌ 登录失败:启用了 DOCKER_LOGIN,但 REGISTRY_PASSWORD 为空"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "🔐 正在登录 Docker Registry..."
|
||||||
|
echo "$REGISTRY_PASSWORD" | docker login "$(echo "$IMAGE_NAME" | cut -d'/' -f1)" \
|
||||||
|
--username "$REGISTRY_USER" --password-stdin
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 根据是否推送决定 buildx 参数
|
||||||
|
if [ "$PUSH_IMAGE" = true ]; then
|
||||||
|
BUILDX_MODE="--push"
|
||||||
|
echo "🚀 将构建并推送镜像到仓库"
|
||||||
|
else
|
||||||
|
BUILDX_MODE="--load"
|
||||||
|
echo "🛠️ 仅本地构建镜像(不推送)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 构建镜像(带 version metadata)
|
||||||
|
docker buildx build \
|
||||||
|
-t "${IMAGE_NAME}:${TIMESTAMP_TAG}" \
|
||||||
|
-t "${IMAGE_NAME}:latest" \
|
||||||
|
--build-arg BUILD_TIME="$BUILD_TIME" \
|
||||||
|
--build-arg GIT_COMMIT="$GIT_COMMIT" \
|
||||||
|
-f "$DOCKERFILE" "$BUILD_CONTEXT" \
|
||||||
|
$BUILDX_MODE
|
||||||
|
|
||||||
|
|
||||||
|
echo "🎉 镜像构建完成!"
|
||||||
|
echo "📌 可用镜像:"
|
||||||
|
echo " - ${IMAGE_NAME}:${TIMESTAMP_TAG}"
|
||||||
|
echo " - ${IMAGE_NAME}:latest"
|
||||||
|
|
||||||
|
if [ "$PUSH_IMAGE" = false ]; then
|
||||||
|
echo "⚠️ 镜像未推送(PUSH_IMAGE=false)"
|
||||||
|
fi
|
||||||
9
entrypoint.sh
Normal file
9
entrypoint.sh
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
echo "Starting Nuxt App..."
|
||||||
|
if [ -f "/app/version.json" ]; then
|
||||||
|
echo "Version info:"
|
||||||
|
cat /app/version.json
|
||||||
|
else
|
||||||
|
echo "⚠️ version.json not found!"
|
||||||
|
fi
|
||||||
|
exec "$@"
|
||||||
16
server/api/version.ts
Normal file
16
server/api/version.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { existsSync, readFileSync } from 'fs';
|
||||||
|
import { execSync } from 'child_process';
|
||||||
|
|
||||||
|
export default defineEventHandler(() => {
|
||||||
|
const path = process.cwd() + '/version.json';
|
||||||
|
|
||||||
|
if (existsSync(path)) {
|
||||||
|
return JSON.parse(readFileSync(path, 'utf-8'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
buildTime: new Date().toISOString(),
|
||||||
|
gitCommit: execSync('git rev-parse --short HEAD').toString().trim(),
|
||||||
|
generated: true,
|
||||||
|
};
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user