From 10fbd0611f3cf0f7ebee46a395ace4fe9626e220 Mon Sep 17 00:00:00 2001 From: mofeng-git Date: Fri, 22 Aug 2025 02:21:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=20GitHub=20Actions=20?= =?UTF-8?q?=E9=95=9C=E5=83=8F=E6=9E=84=E5=BB=BA=E5=B7=A5=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_img.yaml | 135 ++++++++++++++++++++++++++++--- 1 file changed, 123 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build_img.yaml b/.github/workflows/build_img.yaml index 1f6a3938..97b25d46 100644 --- a/.github/workflows/build_img.yaml +++ b/.github/workflows/build_img.yaml @@ -4,7 +4,7 @@ on: workflow_dispatch: inputs: device_target: - description: 'Target device name' + description: 'Target device to build' required: true type: choice options: @@ -15,6 +15,20 @@ on: - e900v22c - octopus-flanet - all + create_release: + description: 'Create GitHub Release' + required: false + default: true + type: boolean + release_name: + description: 'Custom release name (optional)' + required: false + type: string + +env: + BUILD_DATE: "" + GIT_SHA: "" + RELEASE_TAG: "" jobs: build: @@ -30,6 +44,31 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set build environment + id: build_env + shell: bash + run: | + BUILD_DATE=$(date +%y%m%d-%H%M) + # 使用 GitHub 提供的环境变量避免 Git 权限问题 + GIT_SHA="${GITHUB_SHA:0:7}" + GIT_BRANCH="${GITHUB_REF_NAME}" + + echo "BUILD_DATE=$BUILD_DATE" >> $GITHUB_ENV + echo "GIT_SHA=$GIT_SHA" >> $GITHUB_ENV + echo "GIT_BRANCH=$GIT_BRANCH" >> $GITHUB_ENV + + # 生成唯一但不创建新分支的标识符 + RELEASE_TAG="build-$BUILD_DATE-${{ github.event.inputs.device_target }}-$GIT_SHA" + echo "RELEASE_TAG=$RELEASE_TAG" >> $GITHUB_ENV + + echo "Build environment:" + echo "- Date: $BUILD_DATE" + echo "- Git SHA: $GIT_SHA" + echo "- Git Branch: $GIT_BRANCH" + echo "- Release Tag: $RELEASE_TAG" - name: Install dependencies run: | @@ -37,7 +76,8 @@ jobs: export DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ sudo tzdata docker.io qemu-utils qemu-user-static binfmt-support parted e2fsprogs \ - curl tar python3 python3-pip rsync git android-sdk-libsparse-utils coreutils zerofree wget + curl tar python3 python3-pip rsync git android-sdk-libsparse-utils coreutils zerofree wget \ + file tree apt-get clean rm -rf /var/lib/apt/lists/* ln -snf /usr/share/zoneinfo/$TZ /etc/localtime @@ -47,27 +87,98 @@ jobs: DEBIAN_FRONTEND: noninteractive - name: Build image + id: build + shell: bash run: | - echo "BUILD_DATE=$(date +%y%m%d)" >> $GITHUB_ENV - + set -eo pipefail + + echo "=== Build Configuration ===" + echo "Target: ${{ github.event.inputs.device_target }}" + echo "Build Date: $BUILD_DATE" + echo "Git SHA: $GIT_SHA" + echo "Git Branch: $GIT_BRANCH" + echo "Output Directory: ${{ github.workspace }}/output" + echo "==========================" + + mkdir -p "${{ github.workspace }}/output" chmod +x build/build_img.sh - - echo "Starting build for target: ${{ github.event.inputs.device_target }}" - bash build/build_img.sh ${{ github.event.inputs.device_target }} - - echo "Build script finished." + + echo "Starting build process..." + if bash build/build_img.sh ${{ github.event.inputs.device_target }}; then + echo "BUILD_SUCCESS=true" >> $GITHUB_OUTPUT + echo "Build completed successfully!" + else + echo "BUILD_SUCCESS=false" >> $GITHUB_OUTPUT + echo "Build failed!" >&2 + exit 1 + fi env: CI_PROJECT_DIR: ${{ github.workspace }} GITHUB_ACTIONS: true OUTPUTDIR: ${{ github.workspace }}/output + - name: Collect build artifacts + id: artifacts + run: | + cd "${{ github.workspace }}/output" + + echo "=== Build Artifacts ===" + if [ -d "${{ github.workspace }}/output" ]; then + find . -name "*.xz" | head -20 + + # 统计xz文件信息 + ARTIFACT_COUNT=$(find . -name "*.xz" | wc -l) + TOTAL_SIZE=$(du -sh . | cut -f1) + + echo "ARTIFACT_COUNT=$ARTIFACT_COUNT" >> $GITHUB_OUTPUT + echo "TOTAL_SIZE=$TOTAL_SIZE" >> $GITHUB_OUTPUT + else + echo "No output directory found!" + echo "ARTIFACT_COUNT=0" >> $GITHUB_OUTPUT + echo "TOTAL_SIZE=0" >> $GITHUB_OUTPUT + fi + echo "======================" + - name: Create GitHub Release + if: steps.build.outputs.BUILD_SUCCESS == 'true' && github.event.inputs.create_release == 'true' id: release uses: softprops/action-gh-release@v1 with: - tag_name: v${{ env.BUILD_DATE }}-${{ github.event.inputs.device_target }}-${{ github.run_id }} - name: CI Build ${{ github.event.inputs.device_target }} ${{ env.BUILD_DATE }} + tag_name: ${{ env.RELEASE_TAG }} + name: ${{ github.event.inputs.release_name || format('One-KVM {0} 构建镜像 ({1})', github.event.inputs.device_target, env.BUILD_DATE) }} + body: | + ## 📦 GitHub Actions 镜像构建 + + ### 构建信息 + - **目标设备**: `${{ github.event.inputs.device_target }}` + - **构建时间**: `${{ env.BUILD_DATE }}` + - **Git 提交**: `${{ env.GIT_SHA }}` (分支: `${{ env.GIT_BRANCH }}`) + - **构建环境**: GitHub Actions (Ubuntu 22.04) + - **工作流ID**: `${{ github.run_id }}` + files: ${{ github.workspace }}/output/*.xz prerelease: true + make_latest: false + generate_release_notes: false env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Build summary + if: always() + run: | + echo "## 📋 构建摘要" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "| 项目 | 值 |" >> $GITHUB_STEP_SUMMARY + echo "|------|-----|" >> $GITHUB_STEP_SUMMARY + echo "| **目标设备** | \`${{ github.event.inputs.device_target }}\` |" >> $GITHUB_STEP_SUMMARY + echo "| **构建时间** | \`${{ env.BUILD_DATE }}\` |" >> $GITHUB_STEP_SUMMARY + echo "| **Git SHA** | \`${{ env.GIT_SHA }}\` |" >> $GITHUB_STEP_SUMMARY + echo "| **Git 分支** | \`${{ env.GIT_BRANCH }}\` |" >> $GITHUB_STEP_SUMMARY + echo "| **构建状态** | ${{ steps.build.outputs.BUILD_SUCCESS == 'true' && '✅ 成功' || '❌ 失败' }} |" >> $GITHUB_STEP_SUMMARY + + if [ "${{ steps.build.outputs.BUILD_SUCCESS }}" = "true" ]; then + echo "| **构建产物** | ${{ steps.artifacts.outputs.ARTIFACT_COUNT || '0' }} 个文件 (${{ steps.artifacts.outputs.TOTAL_SIZE || '0' }}) |" >> $GITHUB_STEP_SUMMARY + if [ "${{ github.event.inputs.create_release }}" = "true" ]; then + echo "| **Release** | [${{ env.RELEASE_TAG }}](${{ steps.release.outputs.url }}) |" >> $GITHUB_STEP_SUMMARY + fi + fi \ No newline at end of file