Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Commit b415d4b

Browse files
committed
feat: arm64
1 parent 3f5ba6e commit b415d4b

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

.github/workflows/docker-build.yml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ on:
1717
required: true
1818
default: true
1919
type: boolean
20+
build_arm64:
21+
description: '构建 ARM64 架构 (较慢)'
22+
required: true
23+
default: false
24+
type: boolean
2025

2126
env:
2227
REGISTRY: ghcr.io
@@ -28,13 +33,24 @@ jobs:
2833
permissions:
2934
contents: read
3035
packages: write
36+
strategy:
37+
matrix:
38+
include:
39+
- platform: linux/amd64
40+
arch: amd64
41+
- platform: linux/arm64
42+
arch: arm64
3143

3244
steps:
3345
- name: 检出代码
3446
uses: actions/checkout@v4
3547

3648
- name: 设置 Docker Buildx
3749
uses: docker/setup-buildx-action@v3
50+
with:
51+
driver-opts: |
52+
image=moby/buildkit:buildx-stable-1
53+
network=host
3854
3955
- name: 登录到 GitHub Container Registry
4056
uses: docker/login-action@v3
@@ -57,26 +73,36 @@ jobs:
5773
type=raw,value=latest,enable={{is_default_branch}}
5874
type=raw,value=${{ github.event.inputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }}
5975
60-
- name: 构建并推送 Docker 镜像
76+
- name: 构建并推送 Docker 镜像 (${{ matrix.arch }})
77+
if: matrix.arch == 'amd64' || (matrix.arch == 'arm64' && (github.event_name != 'workflow_dispatch' || github.event.inputs.build_arm64 == 'true'))
6178
uses: docker/build-push-action@v5
6279
with:
6380
context: .
64-
platforms: linux/amd64,linux/arm64
81+
file: ${{ matrix.arch == 'arm64' && 'Dockerfile.arm64' || 'Dockerfile' }}
82+
platforms: ${{ matrix.platform }}
6583
push: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.push_to_registry == 'true' }}
6684
tags: ${{ steps.meta.outputs.tags }}
6785
labels: ${{ steps.meta.outputs.labels }}
68-
cache-from: type=gha
69-
cache-to: type=gha,mode=max
86+
cache-from: type=gha,scope=${{ matrix.arch }}
87+
cache-to: type=gha,mode=max,scope=${{ matrix.arch }}
88+
build-args: |
89+
BUILDPLATFORM=${{ matrix.platform }}
90+
TARGETPLATFORM=${{ matrix.platform }}
91+
outputs: type=registry
92+
provenance: false
93+
sbom: false
7094

7195
- name: 生成摘要
7296
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
7397
run: |
7498
echo "## 🐳 Docker 镜像构建完成" >> $GITHUB_STEP_SUMMARY
7599
echo "" >> $GITHUB_STEP_SUMMARY
76100
echo "**触发方式:** ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY
101+
echo "**构建架构:** ${{ matrix.arch }}" >> $GITHUB_STEP_SUMMARY
77102
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
78103
echo "**自定义标签:** ${{ github.event.inputs.tag }}" >> $GITHUB_STEP_SUMMARY
79104
echo "**推送到注册表:** ${{ github.event.inputs.push_to_registry }}" >> $GITHUB_STEP_SUMMARY
105+
echo "**构建 ARM64:** ${{ github.event.inputs.build_arm64 }}" >> $GITHUB_STEP_SUMMARY
80106
fi
81107
echo "" >> $GITHUB_STEP_SUMMARY
82108
echo "**镜像标签:**" >> $GITHUB_STEP_SUMMARY

Dockerfile.arm64

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# ARM64 优化版本的 Dockerfile
2+
# 使用官方 Go 镜像作为构建阶段
3+
FROM --platform=linux/arm64 golang:1.23.7-alpine AS builder
4+
5+
# 设置工作目录
6+
WORKDIR /app
7+
8+
# 安装必要的系统依赖
9+
RUN apk add --no-cache git gcc musl-dev sqlite-dev
10+
11+
# 复制 go mod 文件
12+
COPY go.mod go.sum ./
13+
14+
# 下载依赖
15+
RUN go mod download
16+
17+
# 复制源代码
18+
COPY . .
19+
20+
# 构建应用程序 - ARM64 优化
21+
RUN CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build \
22+
-a -installsuffix cgo \
23+
-ldflags="-w -s" \
24+
-o main .
25+
26+
# 使用轻量级的 Alpine 镜像作为运行阶段
27+
FROM --platform=linux/arm64 alpine:latest
28+
29+
# 安装运行时依赖
30+
RUN apk --no-cache add ca-certificates sqlite
31+
32+
# 创建非 root 用户
33+
RUN addgroup -g 1001 -S appgroup && \
34+
adduser -u 1001 -S appuser -G appgroup
35+
36+
# 设置工作目录
37+
WORKDIR /app
38+
39+
# 从构建阶段复制二进制文件
40+
COPY --from=builder /app/main .
41+
42+
# 创建数据目录并设置权限
43+
RUN mkdir -p /data && \
44+
chown -R appuser:appgroup /app /data
45+
46+
# 切换到非 root 用户
47+
USER appuser
48+
49+
# 暴露端口
50+
EXPOSE 8080
51+
52+
# 设置环境变量默认值
53+
ENV USERNAME=your_username
54+
ENV PASSWORD=your_password
55+
ENV SUBSCRIPTION_URL=https://your_subscription_url.com
56+
57+
# 健康检查
58+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
59+
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/api/login || exit 1
60+
61+
# 启动应用程序
62+
CMD ["./main"]

0 commit comments

Comments
 (0)