From acf63ca974e0c9c3a1c404290a1a071ed855ccbd Mon Sep 17 00:00:00 2001 From: ChenKaiLiuG Date: Sun, 4 Jan 2026 12:42:32 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20.github/workflows/build.ym?= =?UTF-8?q?l?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ChenKaiLiuG --- .github/workflows/build.yml | 226 ++++++++++++++++++++---------------- 1 file changed, 125 insertions(+), 101 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 94dfa6a..fbe09de 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,21 +1,17 @@ -name: Build and Push EdgeNode Docker Image +name: Build and Push Docker Image -on: +on: push: + branches: + - master + - main tags: - 'v*' - branches: - - master - - main - pull_request: - branches: - - master - - main workflow_dispatch: inputs: build_type: description: 'Build type (community or plus)' - required: true + required: false default: 'community' type: choice options: @@ -23,123 +19,151 @@ on: - plus env: - REGISTRY_GHCR: ghcr.io - IMAGE_NAME_GHCR: ${{ github.repository }} - REGISTRY_DOCKER: docker.io - IMAGE_NAME_DOCKER: ${{ secrets.DOCKER_HUB_USERNAME }}/edge-node + REGISTRY_HOST: 172.24.0.10:3000 + EXTERNAL_REGISTRY: 192.168.10.100:3000 + DOCKER_BUILDKIT: 1 jobs: build: runs-on: ubuntu-latest permissions: contents: read - packages: write - - strategy: - matrix: - include: - - platform: linux/amd64 - arch: amd64 - - platform: linux/arm64 - arch: arm64 steps: - name: Checkout code uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - - name: Log in to GitHub Container Registry - if: github.event_name != 'pull_request' - uses: docker/login-action@v3 with: - registry: ${{ env.REGISTRY_GHCR }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} + driver-options: | + image=moby/buildkit:latest + network=host - - name: Log in to Docker Hub - if: github.event_name != 'pull_request' && secrets.DOCKER_HUB_USERNAME != '' - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKER_HUB_USERNAME }} - password: ${{ secrets.DOCKER_HUB_TOKEN }} - - - name: Extract metadata + - name: Extract version and metadata id: meta - uses: docker/metadata-action@v5 - with: - images: | - ${{ env.REGISTRY_GHCR }}/${{ env.IMAGE_NAME_GHCR }} - ${{ env.REGISTRY_DOCKER }}/${{ env.IMAGE_NAME_DOCKER }} - tags: | - type=ref,event=branch - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - type=sha,prefix={{branch}}- - type=raw,value=latest,enable={{is_default_branch}} - - - name: Determine build type - id: build_type run: | - if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then - echo "build_type=${{ inputs.build_type }}" >> $GITHUB_OUTPUT + # 取得 git tag 或 branch 名稱 + if [[ ${{ github.ref }} == refs/tags/* ]]; then + VERSION=${GITHUB_REF#refs/tags/} else - echo "build_type=community" >> $GITHUB_OUTPUT + VERSION=${{ github.ref_name }}-$(git rev-parse --short HEAD) fi + echo "version=${VERSION}" >> $GITHUB_OUTPUT + + # 轉換為小寫的 image name + IMAGE_NAME=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]') + echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT + + # 判斷編譯類型 + BUILD_TYPE=${{ inputs.build_type || 'community' }} + echo "build_type=${BUILD_TYPE}" >> $GITHUB_OUTPUT + + echo "=== 構建信息 ===" + echo "版本: ${VERSION}" + echo "映像: ${IMAGE_NAME}" + echo "構建類型: ${BUILD_TYPE}" - - name: Build and push GHCR + - name: Debug Network Connectivity + run: | + echo "測試內部 Registry 連接..." + ping -c 3 172.24.0.10 || true + timeout 5 bash -c 'cat < /dev/null > /dev/tcp/172.24.0.10/3000' && echo "✓ Registry 可達" || echo "✗ Registry 不可達" + + - name: Login to Gitea Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY_HOST }} + username: ${{ github.repository_owner }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Build and push (single architecture) + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + push: true + tags: | + ${{ env.REGISTRY_HOST }}/${{ steps.meta.outputs.image_name }}:${{ steps.meta.outputs.version }} + ${{ env.REGISTRY_HOST }}/${{ steps.meta.outputs.image_name }}:latest + build-args: | + VERSION=${{ steps.meta.outputs.version }} + BUILD_TYPE=${{ steps.meta.outputs.build_type }} + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Display push results + run: | + echo "" + echo "✓ 編譯完成!" + echo "" + echo "=== 內部訪問 (Docker 容器內) ===" + echo " docker pull ${{ env.REGISTRY_HOST }}/${{ steps.meta.outputs.image_name }}:${{ steps.meta.outputs.version }}" + echo " docker pull ${{ env.REGISTRY_HOST }}/${{ steps.meta.outputs.image_name }}:latest" + echo "" + echo "=== 外部訪問 (Portainer/本地機器) ===" + echo " docker pull ${{ env.EXTERNAL_REGISTRY }}/${{ steps.meta.outputs.image_name }}:${{ steps.meta.outputs.version }}" + echo " docker pull ${{ env.EXTERNAL_REGISTRY }}/${{ steps.meta.outputs.image_name }}:latest" + echo "" + echo "映像名稱: ${{ steps.meta.outputs.image_name }}" + echo "版本標籤: ${{ steps.meta.outputs.version }}" + echo "構建類型: ${{ steps.meta.outputs.build_type }}" + + # 可選:如果您想支持多架構編譯(需要有合適的 runner) + build-multiarch: + runs-on: ubuntu-latest + if: contains(github.ref, 'tags/v') # 只在發佈版本時執行 + permissions: + contents: read + + strategy: + matrix: + include: + - arch: amd64 + platform: linux/amd64 + - arch: arm64 + platform: linux/arm64 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + driver-options: image=moby/buildkit:latest,network=host + + - name: Extract version and metadata + id: meta + run: | + VERSION=${GITHUB_REF#refs/tags/} + IMAGE_NAME=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]') + echo "version=${VERSION}" >> $GITHUB_OUTPUT + echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT + + - name: Login to Gitea Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY_HOST }} + username: ${{ github.repository_owner }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Build for ${{ matrix.arch }} uses: docker/build-push-action@v5 with: context: . file: ./Dockerfile platforms: ${{ matrix.platform }} - push: ${{ github.event_name != 'pull_request' }} - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max + push: true + tags: | + ${{ env.REGISTRY_HOST }}/${{ steps.meta.outputs.image_name }}:${{ steps.meta.outputs.version }}-${{ matrix.arch }} build-args: | - VERSION=${{ github.ref_name }} - BUILD_TYPE=${{ steps.build_type.outputs.build_type }} + VERSION=${{ steps.meta.outputs.version }} TARGETARCH=${{ matrix.arch }} - publish-manifest: - runs-on: ubuntu-latest - needs: build - if: github.event_name != 'pull_request' - permissions: - contents: read - packages: write - - steps: - - name: Log in to GitHub Container Registry - uses: docker/login-action@v3 - with: - registry: ${{ env.REGISTRY_GHCR }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Create manifest for GHCR + - name: Save architecture info run: | - docker manifest create \ - ${{ env.REGISTRY_GHCR }}/${{ env.IMAGE_NAME_GHCR }}:${{ github.ref_name }} \ - --amend ${{ env.REGISTRY_GHCR }}/${{ env.IMAGE_NAME_GHCR }}:${{ github.ref_name }}-amd64 \ - --amend ${{ env.REGISTRY_GHCR }}/${{ env.IMAGE_NAME_GHCR }}:${{ github.ref_name }}-arm64 - docker manifest push ${{ env.REGISTRY_GHCR }}/${{ env.IMAGE_NAME_GHCR }}:${{ github.ref_name }} - - - name: Log in to Docker Hub - if: secrets.DOCKER_HUB_USERNAME != '' - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKER_HUB_USERNAME }} - password: ${{ secrets.DOCKER_HUB_TOKEN }} - - - name: Create manifest for Docker Hub - if: secrets.DOCKER_HUB_USERNAME != '' - run: | - docker manifest create \ - ${{ env.REGISTRY_DOCKER }}/${{ env.IMAGE_NAME_DOCKER }}:${{ github.ref_name }} \ - --amend ${{ env.REGISTRY_DOCKER }}/${{ env.IMAGE_NAME_DOCKER }}:${{ github.ref_name }}-amd64 \ - --amend ${{ env.REGISTRY_DOCKER }}/${{ env.IMAGE_NAME_DOCKER }}:${{ github.ref_name }}-arm64 - docker manifest push ${{ env.REGISTRY_DOCKER }}/${{ env.IMAGE_NAME_DOCKER }}:${{ github.ref_name }} + echo "✓ 編譯完成: ${{ matrix.arch }}" + echo "映像: ${{ env.REGISTRY_HOST }}/${{ steps.meta.outputs.image_name }}:${{ steps.meta.outputs.version }}-${{ matrix.arch }}"