build(ci): 优化 Docker 构建流程并添加多平台支持

- 在 Dockerfile 中添加构建参数和标签信息用于缓存破坏
- 使用 --chown 确保正确的文件权限并验证构建产物
- 添加构建产物时间戳检查以确保最新性
- 获取并使用版本号进行镜像标签管理
- 添加清理冲突镜像标签的步骤
- 配置多平台构建支持(linux/amd64,linux/arm64)
- 添加版本标签和构建参数传递
- 配置构建缓存和镜像推送功能
This commit is contained in:
coolxitech
2026-01-08 17:55:40 +08:00
parent e4c4cdd63c
commit 9634dcb023
2 changed files with 70 additions and 1 deletions

View File

@@ -61,6 +61,21 @@ jobs:
echo "❌ 构建失败docs/index.html 不存在" echo "❌ 构建失败docs/index.html 不存在"
exit 1 exit 1
fi fi
# 检查构建产物的时间戳,确保是最新的
BUILD_TIME=$(stat -c %Y docs/index.html 2>/dev/null || stat -f %m docs/index.html 2>/dev/null || echo "0")
CURRENT_TIME=$(date +%s)
TIME_DIFF=$((CURRENT_TIME - BUILD_TIME))
echo "📊 构建产物信息:"
echo " 构建时间: $(date -d @$BUILD_TIME 2>/dev/null || date -r $BUILD_TIME 2>/dev/null || echo '未知')"
echo " 当前时间: $(date)"
echo " 时间差: ${TIME_DIFF}秒"
if [ $TIME_DIFF -gt 300 ]; then
echo "⚠️ 警告: 构建产物可能不是最新的超过5分钟"
fi
echo "✅ 构建产物验证通过" echo "✅ 构建产物验证通过"
ls -la docs/ ls -la docs/
@@ -69,6 +84,14 @@ jobs:
id: date id: date
run: echo "date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT run: echo "date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
# 获取版本号
- name: 获取版本号
id: version
run: |
VERSION=$(node -p "require('./package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "📦 当前版本: $VERSION"
# 准备 CI 构建环境 # 准备 CI 构建环境
- name: 准备 CI 构建环境 - name: 准备 CI 构建环境
run: | run: |
@@ -78,6 +101,27 @@ jobs:
echo "📁 当前构建上下文文件:" echo "📁 当前构建上下文文件:"
ls -la | grep -E "(docs|nginx.conf|Dockerfile.ci|\.dockerignore)$" ls -la | grep -E "(docs|nginx.conf|Dockerfile.ci|\.dockerignore)$"
# 清理可能冲突的镜像标签
- name: 清理可能冲突的镜像标签
continue-on-error: true
run: |
VERSION="${{ steps.version.outputs.version }}"
echo "🧹 尝试清理可能冲突的镜像标签..."
# 尝试删除 GHCR 中的现有标签(如果存在)
echo "清理 GHCR 标签..."
docker buildx imagetools inspect ghcr.io/${{ github.repository_owner }}/ogame-vue-ts:$VERSION 2>/dev/null && \
echo "发现现有版本标签,将被覆盖" || echo "版本标签不存在,可以安全推送"
# 如果配置了 Docker Hub也尝试检查
if [ -n "${{ vars.DOCKERHUB_USERNAME }}" ]; then
echo "检查 Docker Hub 标签..."
docker buildx imagetools inspect ${{ vars.DOCKERHUB_USERNAME }}/ogame-vue-ts:$VERSION 2>/dev/null && \
echo "发现现有 Docker Hub 版本标签,将被覆盖" || echo "Docker Hub 版本标签不存在,可以安全推送"
fi
echo "✅ 标签冲突检查完成,构建将覆盖任何现有标签"
# QEMU 用于支持多架构构建(必须) # QEMU 用于支持多架构构建(必须)
- name: 设置 QEMU - name: 设置 QEMU
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v3
@@ -110,16 +154,26 @@ jobs:
file: ./Dockerfile.ci file: ./Dockerfile.ci
platforms: linux/amd64,linux/arm64 platforms: linux/amd64,linux/arm64
push: true push: true
no-cache: false
pull: true
tags: | tags: |
ghcr.io/${{ github.repository_owner }}/ogame-vue-ts:latest ghcr.io/${{ github.repository_owner }}/ogame-vue-ts:latest
ghcr.io/${{ github.repository_owner }}/ogame-vue-ts:${{ steps.version.outputs.version }}
ghcr.io/${{ github.repository_owner }}/ogame-vue-ts:${{ github.sha }} ghcr.io/${{ github.repository_owner }}/ogame-vue-ts:${{ github.sha }}
${{ vars.DOCKERHUB_USERNAME && format('{0}/ogame-vue-ts:latest', vars.DOCKERHUB_USERNAME) || '' }} ${{ vars.DOCKERHUB_USERNAME && format('{0}/ogame-vue-ts:latest', vars.DOCKERHUB_USERNAME) || '' }}
${{ vars.DOCKERHUB_USERNAME && format('{0}/ogame-vue-ts:{1}', vars.DOCKERHUB_USERNAME, steps.version.outputs.version) || '' }}
${{ vars.DOCKERHUB_USERNAME && format('{0}/ogame-vue-ts:{1}', vars.DOCKERHUB_USERNAME, github.sha) || '' }} ${{ vars.DOCKERHUB_USERNAME && format('{0}/ogame-vue-ts:{1}', vars.DOCKERHUB_USERNAME, github.sha) || '' }}
cache-from: type=gha cache-from: type=gha
cache-to: type=gha,mode=max cache-to: type=gha,mode=max
build-args: |
BUILDKIT_INLINE_CACHE=1
BUILD_DATE=${{ steps.date.outputs.date }}
VERSION=${{ steps.version.outputs.version }}
COMMIT_SHA=${{ github.sha }}
labels: | labels: |
org.opencontainers.image.title=OGame Vue Ts org.opencontainers.image.title=OGame Vue Ts
org.opencontainers.image.description=OGame Vue TypeScript Implementation org.opencontainers.image.description=OGame Vue TypeScript Implementation
org.opencontainers.image.version=${{ steps.version.outputs.version }}
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }} org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.created=${{ steps.date.outputs.date }} org.opencontainers.image.created=${{ steps.date.outputs.date }}

View File

@@ -3,6 +3,16 @@
FROM nginx:alpine FROM nginx:alpine
# 添加构建参数用于缓存破坏
ARG BUILD_DATE
ARG VERSION
ARG COMMIT_SHA
# 添加标签信息
LABEL build.date="${BUILD_DATE}" \
build.version="${VERSION}" \
build.commit="${COMMIT_SHA}"
# 复制 nginx 配置文件 # 复制 nginx 配置文件
COPY nginx.conf /etc/nginx/conf.d/default.conf COPY nginx.conf /etc/nginx/conf.d/default.conf
@@ -11,7 +21,12 @@ RUN rm -rf /usr/share/nginx/html/*
# 复制构建产物到 nginx 静态文件目录 # 复制构建产物到 nginx 静态文件目录
# 这里的 docs 目录是在 GitHub Actions 中构建生成的 # 这里的 docs 目录是在 GitHub Actions 中构建生成的
COPY docs /usr/share/nginx/html # 使用 --chown 确保正确的文件权限
COPY --chown=nginx:nginx docs /usr/share/nginx/html
# 验证构建产物
RUN ls -la /usr/share/nginx/html/ && \
test -f /usr/share/nginx/html/index.html || (echo "构建产物验证失败" && exit 1)
# 暴露端口 # 暴露端口
EXPOSE 80 EXPOSE 80