first commit

This commit is contained in:
2025-12-19 04:43:57 +00:00
commit 979aa14878
3 changed files with 375 additions and 0 deletions
+204
View File
@@ -0,0 +1,204 @@
# .gitea/workflows/reusable-docker-build.yml
name: Reusable Docker Build
on:
workflow_call:
inputs:
dockerfile:
description: 'Dockerfile 路径(相对于仓库根目录)'
required: true
type: string
context:
description: '构建上下文路径(相对于仓库根目录)'
required: false
type: string
default: '.'
image_name:
description: 'Docker 镜像名称(不含标签)'
required: true
type: string
image_tag:
description: 'Docker 镜像标签/版本'
required: true
type: string
checkout_ref:
description: '要检出的分支或标签'
required: false
type: string
default: ''
enable_cache:
description: '是否启用 Docker 构建缓存'
required: false
type: boolean
default: true
build_args:
description: 'Docker 构建参数(每行一个,格式: KEY=VALUE'
required: false
type: string
default: ''
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ inputs.checkout_ref || github.ref }}
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
if: ${{ inputs.enable_cache }}
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ inputs.image_name }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-${{ inputs.image_name }}-
${{ runner.os }}-buildx-
- name: Prepare build arguments
id: prepare
run: |
echo "====== 准备构建参数 ======"
DOCKERFILE="${{ inputs.dockerfile }}"
CONTEXT="${{ inputs.context }}"
IMAGE_NAME="${{ inputs.image_name }}"
IMAGE_TAG="${{ inputs.image_tag }}"
FULL_IMAGE="${IMAGE_NAME}:${IMAGE_TAG}"
echo "Dockerfile: $DOCKERFILE"
echo "Context: $CONTEXT"
echo "Image: $FULL_IMAGE"
# 验证 Dockerfile 存在
if [ ! -f "$DOCKERFILE" ]; then
echo "✗ Dockerfile 不存在: $DOCKERFILE"
exit 1
fi
# 验证上下文目录存在
if [ ! -d "$CONTEXT" ]; then
echo "✗ 构建上下文目录不存在: $CONTEXT"
exit 1
fi
echo "✓ 构建参数验证通过"
echo ""
# 设置输出变量
echo "full_image=$FULL_IMAGE" >> $GITHUB_OUTPUT
# 处理构建参数
BUILD_ARGS=""
if [ -n "${{ inputs.build_args }}" ]; then
echo "====== 构建参数 ======"
while IFS= read -r line; do
if [ -n "$line" ]; then
echo " $line"
BUILD_ARGS="$BUILD_ARGS --build-arg $line"
fi
done <<< "${{ inputs.build_args }}"
echo ""
fi
echo "build_args=$BUILD_ARGS" >> $GITHUB_OUTPUT
- name: Build Docker image
run: |
echo "====== 开始构建 Docker 镜像 ======"
echo "构建时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "分支: ${{ github.ref_name }}"
echo "提交: ${{ github.sha }}"
if [ -n "${{ github.event.head_commit.message }}" ]; then
echo "提交信息: ${{ github.event.head_commit.message }}"
echo "提交者: ${{ github.event.head_commit.author.name }}"
fi
echo ""
echo "====== 构建配置 ======"
echo "镜像: ${{ steps.prepare.outputs.full_image }}"
echo "Dockerfile: ${{ inputs.dockerfile }}"
echo "Context: ${{ inputs.context }}"
echo ""
# 构建命令
BUILD_CMD="docker build \
-t ${{ steps.prepare.outputs.full_image }} \
-f ${{ inputs.dockerfile }} \
${{ steps.prepare.outputs.build_args }} \
${{ inputs.context }}"
echo "执行命令:"
echo "$BUILD_CMD"
echo ""
# 执行构建
eval $BUILD_CMD
echo ""
echo "====== 镜像构建完成 ======"
- name: Verify image
run: |
echo "====== 验证构建的镜像 ======"
IMAGE="${{ steps.prepare.outputs.full_image }}"
if docker image inspect "${IMAGE}" >/dev/null 2>&1; then
SIZE=$(docker image inspect "${IMAGE}" --format='{{.Size}}' | awk '{printf "%.2f", $1/1024/1024}')
CREATED=$(docker image inspect "${IMAGE}" --format='{{.Created}}')
ID=$(docker image inspect "${IMAGE}" --format='{{.Id}}' | cut -d':' -f2 | cut -c1-12)
echo "✓ ${IMAGE}"
echo " 镜像 ID: ${ID}"
echo " 大小: ${SIZE} MB"
echo " 创建时间: ${CREATED}"
else
echo "✗ ${IMAGE} 验证失败"
exit 1
fi
echo ""
echo "✓ 镜像验证成功"
- name: Show image details
run: |
echo "====== 镜像详细信息 ======"
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}\t{{.CreatedAt}}" | grep "${{ inputs.image_name }}" || true
echo ""
echo "====== 镜像层信息(前10层)======"
docker history "${{ steps.prepare.outputs.full_image }}" --format "table {{.CreatedBy}}\t{{.Size}}" | head -11
- name: Cleanup
if: always()
run: |
echo "====== 清理悬空镜像 ======"
docker image prune -f
echo "✓ 清理完成"
- name: Build summary
if: always()
run: |
echo "====== 构建摘要 ======"
echo "镜像名称: ${{ steps.prepare.outputs.full_image }}"
echo "Dockerfile: ${{ inputs.dockerfile }}"
echo "构建上下文: ${{ inputs.context }}"
echo "分支: ${{ github.ref_name }}"
echo "提交: ${{ github.sha }}"
echo "触发方式: ${{ github.event_name }}"
echo "构建状态: ${{ job.status }}"
echo ""
if [ "${{ job.status }}" == "success" ]; then
echo "🎉 镜像构建成功!"
else
echo "❌ 镜像构建失败,请检查日志"
fi