Skip to content

Commit 06481d1

Browse files
committed
feat(ci): 添加 Docker 镜像构建和发布功能
- 在 GitHub Actions 工作流中增加 Docker 构建和推送步骤 - 配置 Docker 权限以支持包写入操作 - 添加 Docker 镜像元数据提取和标签生成 - 更新 .env.example 中的镜像路径配置 - 添加 Docker Compose 配置文件用于本地部署 - 创建完整的 README.md 文档说明 Docker 使用方法 - 配置自动化的 Docker 镜像发布流程到 GHCR
1 parent 05b5fbf commit 06481d1

4 files changed

Lines changed: 63 additions & 36 deletions

File tree

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GCS_BINARY_PATH=./dist/gcs-linux-x86_64
1+
GCS_IMAGE=ghcr.io/<your-org-or-user>/<your-repo>:latest
22
GCS_WORKDIR_HOST=./gcs_workspace
33
GCS_PORT=8765
44
GCS_WORKERS=8

.github/workflows/release.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77

88
permissions:
99
contents: write
10+
packages: write
1011

1112
jobs:
1213
build:
@@ -83,3 +84,56 @@ jobs:
8384
with:
8485
generate_release_notes: true
8586
files: release-assets/*
87+
88+
docker:
89+
name: Build And Publish Docker Image
90+
runs-on: ubuntu-latest
91+
needs: build
92+
steps:
93+
- name: Checkout
94+
uses: actions/checkout@v4
95+
96+
- name: Download Linux Artifact
97+
uses: actions/download-artifact@v4
98+
with:
99+
name: linux-x86_64
100+
path: dist
101+
102+
- name: Ensure Binary Is Executable
103+
run: chmod +x dist/gcs-linux-x86_64
104+
105+
- name: Smoke Test Docker Image
106+
run: |
107+
docker build -t gcs-smoke:latest .
108+
docker run --rm gcs-smoke:latest --version
109+
docker run --rm gcs-smoke:latest search --help
110+
docker run --rm gcs-smoke:latest serve --help
111+
112+
- name: Setup Docker Buildx
113+
uses: docker/setup-buildx-action@v3
114+
115+
- name: Login To GHCR
116+
uses: docker/login-action@v3
117+
with:
118+
registry: ghcr.io
119+
username: ${{ github.actor }}
120+
password: ${{ secrets.GITHUB_TOKEN }}
121+
122+
- name: Extract Docker Metadata
123+
id: meta
124+
uses: docker/metadata-action@v5
125+
with:
126+
images: ghcr.io/${{ github.repository }}
127+
tags: |
128+
type=ref,event=tag
129+
type=raw,value=latest
130+
131+
- name: Build And Push Docker Image
132+
uses: docker/build-push-action@v6
133+
with:
134+
context: .
135+
file: ./Dockerfile
136+
platforms: linux/amd64
137+
push: true
138+
tags: ${{ steps.meta.outputs.tags }}
139+
labels: ${{ steps.meta.outputs.labels }}

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,15 @@
209209
dist/gcs
210210
```
211211

212-
## Docker Compose 直接运行编译后二进制
212+
## Docker Compose 直接运行发布镜像
213213

214-
适用于已经拿到 Linux 可执行文件的场景,例如 GitHub Release 里的 `gcs-linux-x86_64`
214+
默认推荐直接使用 GitHub Action 发布到 GHCR 的镜像,不需要先下载二进制
215215

216-
1. 准备二进制和环境变量
216+
1. 准备环境变量
217217

218218
```bash
219219
cp .env.example .env
220-
#Linux 二进制放到 .env 里的 GCS_BINARY_PATH,默认是 ./dist/gcs-linux-x86_64
220+
#GCS_IMAGE 改成仓库实际发布出来的 GHCR 镜像地址
221221
```
222222

223223
2. 启动 `serve`
@@ -234,10 +234,9 @@ docker compose logs -f gcs-serve
234234

235235
说明:
236236

237-
- `compose.yaml` 会直接挂载并执行编译后的二进制,不会在容器里重新安装 Python 依赖或重新构建
238-
- `GCS_BINARY_PATH` 必须指向 Linux 二进制,不能使用当前机器打出来的 macOS `dist/gcs`
237+
- `compose.yaml` 默认直接拉取 GHCR 镜像,别人只需要填 `.env` 就可以启动
239238
- `GCS_WORKDIR_HOST` 会映射到容器内 `/data`,其中包含 SQLite、导出文件和 `serve` 持久化 PAT,应放在私有目录
240-
- 如果使用 arm64 Linux 二进制,请同时修改 `GCS_BINARY_PATH``GCS_DOCKER_PLATFORM`
239+
- 当前 GitHub Action 发布的是 `linux/amd64` 镜像;在 Apple Silicon 上可以继续用 Docker Desktop 模拟运行
241240

242241
## GitHub Actions 自动发布
243242

@@ -250,6 +249,7 @@ docker compose logs -f gcs-serve
250249
- macOS Apple Silicon(arm64)
251250
- Linux x86_64
252251
- Windows x86_64
252+
- 自动构建并推送 Docker 镜像到 GitHub Container Registry(GHCR)
253253
- 自动创建 GitHub Release 并上传二进制附件
254254

255255
触发示例:

compose.yaml

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
services:
22
gcs-serve:
3-
image: debian:bookworm-slim
3+
image: ${GCS_IMAGE:?set GCS_IMAGE in .env or your shell}
44
platform: ${GCS_DOCKER_PLATFORM:-linux/amd64}
55
restart: unless-stopped
66
init: true
7-
working_dir: /srv/gcs
87
environment:
98
GCS_ADMIN_TOKEN: ${GCS_ADMIN_TOKEN:?set GCS_ADMIN_TOKEN in .env or your shell}
109
GCS_GITLAB_URL: ${GCS_GITLAB_URL:-}
@@ -14,32 +13,6 @@ services:
1413
ports:
1514
- "${GCS_PORT:-8765}:${GCS_PORT:-8765}"
1615
volumes:
17-
- type: bind
18-
source: ${GCS_BINARY_PATH:-./dist/gcs-linux-x86_64}
19-
target: /binary/gcs
20-
read_only: true
2116
- type: bind
2217
source: ${GCS_WORKDIR_HOST:-./gcs_workspace}
2318
target: /data
24-
command:
25-
- /bin/sh
26-
- -lc
27-
- |
28-
set -eu
29-
cp /binary/gcs /tmp/gcs
30-
chmod +x /tmp/gcs
31-
if [ -n "${GCS_GITLAB_URL:-}" ]; then
32-
exec /tmp/gcs serve \
33-
--workdir "${GCS_WORKDIR}" \
34-
--admin-token "${GCS_ADMIN_TOKEN}" \
35-
--host 0.0.0.0 \
36-
--port "${GCS_PORT}" \
37-
--gitlab-url "${GCS_GITLAB_URL}" \
38-
--workers "${GCS_WORKERS}"
39-
fi
40-
exec /tmp/gcs serve \
41-
--workdir "${GCS_WORKDIR}" \
42-
--admin-token "${GCS_ADMIN_TOKEN}" \
43-
--host 0.0.0.0 \
44-
--port "${GCS_PORT}" \
45-
--workers "${GCS_WORKERS}"

0 commit comments

Comments
 (0)