Skip to content

Commit f511782

Browse files
committed
feat: 添加 Dockerfile 以支持多平台构建和运行
1 parent c0c6298 commit f511782

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

.github/workflows/build.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Build Multi-Platform Binaries
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
workflow_dispatch:
7+
8+
jobs:
9+
build:
10+
name: Build
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
goos: [linux, windows, darwin]
15+
goarch: [amd64, arm64]
16+
exclude:
17+
- goos: windows
18+
goarch: arm64
19+
20+
steps:
21+
- name: Check out code
22+
uses: actions/checkout@v3
23+
24+
- name: Set up Go
25+
uses: actions/setup-go@v4
26+
with:
27+
go-version: "1.21"
28+
29+
- name: Build
30+
env:
31+
GOOS: ${{ matrix.goos }}
32+
GOARCH: ${{ matrix.goarch }}
33+
run: |
34+
OUTPUT_NAME=abmc-forwarder-${{ matrix.goos }}-${{ matrix.goarch }}
35+
if [ "${{ matrix.goos }}" = "windows" ]; then
36+
OUTPUT_NAME="${OUTPUT_NAME}.exe"
37+
fi
38+
39+
go build -v -o "${OUTPUT_NAME}" -ldflags="-s -w" .
40+
41+
- name: Upload artifacts
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: nia-forwarding-${{ matrix.goos }}-${{ matrix.goarch }}
45+
path: |
46+
nia-forwarding-${{ matrix.goos }}-${{ matrix.goarch }}*

Dockerfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# ---------- Builder Stage ----------
2+
FROM golang:1.23-alpine AS builder
3+
4+
# 获取目标平台信息
5+
ARG TARGETPLATFORM
6+
ARG GOARCH
7+
RUN echo "Building for $TARGETPLATFORM"
8+
9+
# 根据目标平台设置正确的 GOARCH
10+
RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
11+
echo "GOARCH=amd64" >> /etc/environment; \
12+
elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
13+
echo "GOARCH=arm64" >> /etc/environment; \
14+
else \
15+
echo "Unsupported platform: $TARGETPLATFORM" && exit 1; \
16+
fi
17+
18+
WORKDIR /app
19+
20+
# 复制源代码
21+
COPY . .
22+
23+
# 安装构建工具、编译
24+
RUN . /etc/environment && \
25+
CGO_ENABLED=0 GOOS=linux \
26+
go build -ldflags="-s -w" -o abmc-forwarder .
27+
28+
# ---------- Runtime Stage ----------
29+
FROM scratch AS runtime
30+
31+
# 拷贝已压缩的静态二进制
32+
COPY --from=builder /app/abmc-forwarder /abmc-forwarder
33+
34+
# 以非 root(UID/GID 1000)运行
35+
USER 1000:1000
36+
37+
# 暴露端口与环境变量
38+
EXPOSE 25565 25565
39+
EXPOSE 19132 19132
40+
41+
ENTRYPOINT ["/abmc-forwarder"]

0 commit comments

Comments
 (0)