commit 979aa14878533827f93b901b0784ff06e9a9ffca Author: zydi Date: Fri Dec 19 04:43:57 2025 +0000 first commit diff --git a/.gitea/workflows/README.md b/.gitea/workflows/README.md new file mode 100644 index 0000000..aab1890 --- /dev/null +++ b/.gitea/workflows/README.md @@ -0,0 +1,171 @@ +## WorkFlows —— Build-images.yaml调用示例 + +### 示例 1: 根目录 Dockerfile(broccoli-api) + +```yaml +# .gitea/workflows/build.yml +name: Build Docker Images + +on: + push: + branches: + - offline + paths: + - 'Dockerfile*' + - 'requirements.txt' + - '.gitea/workflows/build.yml' + workflow_dispatch: + +jobs: + build-api: + uses: your-org/common-workflows/.gitea/workflows/reusable-docker-build.yml@main + with: + dockerfile: 'Dockerfile.api' + context: '.' + image_name: 'broccoli-api' + image_tag: 'v3.2.1' + checkout_ref: 'offline' +``` + +### 示例 2: 子目录 Dockerfile(broccoli-frontend) + +```yaml +# .gitea/workflows/build.yml +name: Build Frontend Docker Image + +on: + push: + branches: + - main + paths: + - 'Frontend/**' + - '.gitea/workflows/build.yml' + workflow_dispatch: + +jobs: + build-frontend: + uses: your-org/common-workflows/.gitea/workflows/reusable-docker-build.yml@main + with: + dockerfile: 'Frontend/Dockerfile' + context: 'Frontend/' + image_name: 'broccoli-frontend' + image_tag: 'v3.2.0' +``` + +### 示例 3: 带构建参数 + +```yaml +# .gitea/workflows/build.yml +name: Build Docker Image + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + build: + uses: your-org/common-workflows/.gitea/workflows/reusable-docker-build.yml@main + with: + dockerfile: 'Dockerfile' + context: '.' + image_name: 'my-app' + image_tag: 'v1.0.0' + build_args: | + NODE_ENV=production + APP_VERSION=v1.0.0 + BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') +``` + +### 示例 4: 多镜像构建 + +```yaml +# .gitea/workflows/build-all.yml +name: Build All Docker Images + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + build-api: + uses: your-org/common-workflows/.gitea/workflows/reusable-docker-build.yml@main + with: + dockerfile: 'Dockerfile.api' + context: '.' + image_name: 'broccoli-api' + image_tag: 'v3.2.1' + + build-frontend: + uses: your-org/common-workflows/.gitea/workflows/reusable-docker-build.yml@main + with: + dockerfile: 'Frontend/Dockerfile' + context: 'Frontend/' + image_name: 'broccoli-frontend' + image_tag: 'v3.2.0' + + build-worker: + needs: [build-api] # 可以设置依赖关系 + uses: your-org/common-workflows/.gitea/workflows/reusable-docker-build.yml@main + with: + dockerfile: 'services/worker/Dockerfile' + context: 'services/worker' + image_name: 'broccoli-worker' + image_tag: 'v1.0.0' +``` + +### 示例 5: 禁用缓存 + +```yaml +# .gitea/workflows/build.yml +name: Build Docker Image (No Cache) + +on: + workflow_dispatch: + +jobs: + build: + uses: your-org/common-workflows/.gitea/workflows/reusable-docker-build.yml@main + with: + dockerfile: 'Dockerfile' + context: '.' + image_name: 'my-app' + image_tag: 'latest' + enable_cache: false +``` + +### 示例 6: 动态版本号 + +```yaml +# .gitea/workflows/build.yml +name: Build Docker Image + +on: + push: + tags: + - 'v*' + +jobs: + build: + uses: your-org/common-workflows/.gitea/workflows/reusable-docker-build.yml@main + with: + dockerfile: 'Dockerfile' + context: '.' + image_name: 'my-app' + image_tag: ${{ github.ref_name }} # 使用 tag 名称作为版本 +``` + +## 核心特性 + +✅ **简洁专注** - 只做镜像构建,没有额外复杂功能 +✅ **灵活路径** - 支持任意 Dockerfile 位置和构建上下文 +✅ **缓存优化** - 可选的 Docker 层缓存加速构建 +✅ **参数支持** - 支持传递构建参数 +✅ **完整验证** - 自动验证镜像构建成功 +✅ **详细日志** - 清晰的构建过程和结果展示 +✅ **自动清理** - 构建后自动清理悬空镜像 + +这个简化版本更聚焦,更易维护! \ No newline at end of file diff --git a/.gitea/workflows/build-images.yaml b/.gitea/workflows/build-images.yaml new file mode 100644 index 0000000..c1b1757 --- /dev/null +++ b/.gitea/workflows/build-images.yaml @@ -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 \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29