From 63581d055612aefaa54e396d1212d56909aa2580 Mon Sep 17 00:00:00 2001 From: tbphp Date: Sat, 7 Jun 2025 12:11:06 +0800 Subject: [PATCH] feat: github action --- .github/workflows/docker-build.yml | 63 ++++++++++++++++++++++++++++++ Dockerfile | 6 ++- README.md | 37 ++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/docker-build.yml diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 0000000..93a2617 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,63 @@ +name: Build and Push to GitHub Container Registry + +on: + push: + branches: [main] + pull_request: + branches: [main] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Container Registry + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=sha,prefix={{branch}}- + type=raw,value=latest,enable={{is_default_branch}} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + 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 + + - name: Generate artifact attestation + if: github.event_name != 'pull_request' + uses: actions/attest-build-provenance@v1 + with: + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} + subject-digest: ${{ steps.build.outputs.digest }} + push-to-registry: true diff --git a/Dockerfile b/Dockerfile index 8ee474a..4ab867f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,8 +18,10 @@ RUN go mod download # 复制源代码 COPY . . -# 构建应用 -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ +# 构建应用 - 支持多平台 +ARG TARGETOS +ARG TARGETARCH +RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \ -ldflags="-w -s -X main.Version=2.0.0" \ -o gpt-load \ ./cmd/main.go diff --git a/README.md b/README.md index 0b4082a..40dda61 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # GPT-Load +![Docker Build](https://github.com/tangbo/gpt-load/actions/workflows/docker-build.yml/badge.svg) +![Go Version](https://img.shields.io/badge/Go-1.21+-blue.svg) +![License](https://img.shields.io/badge/license-MIT-green.svg) + 一个**极致高性能**的 OpenAI API 多密钥轮询透明代理服务器,使用 Go 语言开发! ## ✨ 特性 @@ -53,6 +57,21 @@ make build ### 方式三:Docker 运行 +#### 使用预构建镜像(推荐) + +```bash +# 从GitHub Container Registry拉取镜像 +docker pull ghcr.io/tangbo/gpt-load:latest + +# 运行容器 +docker run -d -p 3000:3000 \ + -e KEYS_FILE=/app/keys.txt \ + -v $(pwd)/keys.txt:/app/keys.txt \ + ghcr.io/tangbo/gpt-load:latest +``` + +#### 本地构建镜像 + ```bash # 构建镜像 make docker-build @@ -96,6 +115,24 @@ cp .env.example .env | `/blacklist` | GET | 黑名单详情 | | `/reset-keys` | GET | 重置所有密钥状态 | +## 🚀 CI/CD 自动部署 + +项目已配置 GitHub Actions 自动构建和部署: + +- **自动触发**:每次 push 到 main 分支时自动构建 +- **多平台支持**:同时构建 Linux AMD64 和 ARM64 镜像 +- **GitHub 集成**:自动推送到 GitHub Container Registry +- **缓存优化**:使用 GitHub Actions 缓存加速构建 + +### 预构建镜像 + +```bash +# 拉取最新镜像 +docker pull ghcr.io/tangbo/gpt-load:latest +``` + +详细配置说明请参考:[GitHub Actions 配置指南](.docs/github-actions-setup.md) + ## 🔧 开发指南 ### 可用命令