Archived
170 lines
5.6 KiB
YAML
170 lines
5.6 KiB
YAML
name: Build and Push Docker Image
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- master
|
|
- main
|
|
tags:
|
|
- 'v*'
|
|
workflow_dispatch:
|
|
inputs:
|
|
build_type:
|
|
description: 'Build type (community or plus)'
|
|
required: false
|
|
default: 'community'
|
|
type: choice
|
|
options:
|
|
- community
|
|
- plus
|
|
|
|
env:
|
|
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
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- 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: |
|
|
# 取得 git tag 或 branch 名稱
|
|
if [[ ${{ github.ref }} == refs/tags/* ]]; then
|
|
VERSION=${GITHUB_REF#refs/tags/}
|
|
else
|
|
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: 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: true
|
|
tags: |
|
|
${{ env.REGISTRY_HOST }}/${{ steps.meta.outputs.image_name }}:${{ steps.meta.outputs.version }}-${{ matrix.arch }}
|
|
build-args: |
|
|
VERSION=${{ steps.meta.outputs.version }}
|
|
TARGETARCH=${{ matrix.arch }}
|
|
|
|
- name: Save architecture info
|
|
run: |
|
|
echo "✓ 編譯完成: ${{ matrix.arch }}"
|
|
echo "映像: ${{ env.REGISTRY_HOST }}/${{ steps.meta.outputs.image_name }}:${{ steps.meta.outputs.version }}-${{ matrix.arch }}"
|