feat: publish container images to GHCR

This commit is contained in:
Jason Xu committed 2026-08-20 04:45:14 +08:00
1 parent cc71ba8aff
commit 948ef0be11
6 files changed
+222 -103

No files matched your search

+7 -1
View File
@@ -1,3 +1,9 @@
*
!Dockerfile
!*.go
!go.mod
!go.sum
!webui/
!webui/**
!config.example.json
!docker-entrypoint.sh
!Dockerfile
+111 -29
View File
@@ -1,4 +1,4 @@
name: Build and release
name: Release binaries and GHCR image
on:
push:
@@ -12,15 +12,57 @@ on:
type: string
permissions:
contents: write
contents: read
concurrency:
group: release-${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref }}
cancel-in-progress: false
jobs:
prepare:
name: Prepare release
runs-on: ubuntu-latest
outputs:
release_tag: ${{ steps.metadata.outputs.release_tag }}
image_name: ${{ steps.metadata.outputs.image_name }}
steps:
- name: Resolve release metadata
id: metadata
shell: bash
env:
DISPATCH_TAG: ${{ inputs.tag }}
run: |
if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
release_tag="$DISPATCH_TAG"
else
release_tag="$GITHUB_REF_NAME"
fi
if [[ ! "$release_tag" =~ ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z]+([.-][0-9A-Za-z]+)*)?$ ]]; then
echo "Release tag must be a semantic version such as v1.0.0 or v1.0.0-rc.1" >&2
exit 1
fi
echo "release_tag=$release_tag" >> "$GITHUB_OUTPUT"
echo "image_name=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Check out source
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: true
- name: Run tests
run: go test ./...
build:
name: Build ${{ matrix.goos }}/${{ matrix.goarch }}
needs: [prepare, test]
runs-on: ubuntu-latest
strategy:
fail-fast: false
@@ -61,21 +103,13 @@ jobs:
go-version-file: go.mod
cache: true
- name: Prepare release metadata
- name: Prepare package name
id: metadata
shell: bash
env:
RELEASE_TAG: ${{ needs.prepare.outputs.release_tag }}
run: |
if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
release_tag="${{ inputs.tag }}"
else
release_tag="$GITHUB_REF_NAME"
fi
if [[ ! "$release_tag" =~ ^v[0-9] ]]; then
echo "Release tag must start with v followed by a number, for example v1.0.0" >&2
exit 1
fi
package_name="opencode2api_${release_tag}_${{ matrix.goos }}_${{ matrix.goarch }}"
echo "release_tag=$release_tag" >> "$GITHUB_OUTPUT"
package_name="opencode2api_${RELEASE_TAG}_${{ matrix.goos }}_${{ matrix.goarch }}"
echo "package_name=$package_name" >> "$GITHUB_OUTPUT"
- name: Build and package
@@ -84,7 +118,7 @@ jobs:
CGO_ENABLED: "0"
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
RELEASE_TAG: ${{ steps.metadata.outputs.release_tag }}
RELEASE_TAG: ${{ needs.prepare.outputs.release_tag }}
PACKAGE_NAME: ${{ steps.metadata.outputs.package_name }}
EXTENSION: ${{ matrix.extension }}
ARCHIVE_FORMAT: ${{ matrix.archive }}
@@ -110,22 +144,66 @@ jobs:
dist/${{ steps.metadata.outputs.package_name }}.${{ matrix.archive }}.sha256
if-no-files-found: error
container:
name: Publish GHCR image
needs: [prepare, test]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Check out source
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate image metadata
id: image_metadata
uses: docker/metadata-action@v5
with:
images: ${{ needs.prepare.outputs.image_name }}
tags: |
type=raw,value=${{ needs.prepare.outputs.release_tag }}
type=semver,pattern={{version}},value=${{ needs.prepare.outputs.release_tag }}
type=semver,pattern={{major}}.{{minor}},value=${{ needs.prepare.outputs.release_tag }},enable=${{ !contains(needs.prepare.outputs.release_tag, '-') }}
type=semver,pattern={{major}},value=${{ needs.prepare.outputs.release_tag }},enable=${{ !contains(needs.prepare.outputs.release_tag, '-') }}
labels: |
org.opencontainers.image.title=opencode2api
org.opencontainers.image.description=OpenAI-compatible API gateway for OpenCode Zen and Go
- name: Build and push image
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.image_metadata.outputs.tags }}
labels: ${{ steps.image_metadata.outputs.labels }}
build-args: |
VERSION=${{ needs.prepare.outputs.release_tag }}
cache-from: type=gha,scope=container
cache-to: type=gha,mode=max,scope=container
provenance: mode=max
sbom: true
release:
name: Publish GitHub Release
needs: build
needs: [prepare, build, container]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Resolve release tag
id: metadata
shell: bash
run: |
if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
release_tag="${{ inputs.tag }}"
else
release_tag="$GITHUB_REF_NAME"
fi
echo "release_tag=$release_tag" >> "$GITHUB_OUTPUT"
- name: Download release files
uses: actions/download-artifact@v4
with:
@@ -137,10 +215,14 @@ jobs:
shell: bash
env:
GH_TOKEN: ${{ github.token }}
RELEASE_TAG: ${{ steps.metadata.outputs.release_tag }}
RELEASE_TAG: ${{ needs.prepare.outputs.release_tag }}
run: |
release_type=()
if [[ "$RELEASE_TAG" == *-* ]]; then
release_type+=(--prerelease)
fi
if gh release view "$RELEASE_TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then
gh release upload "$RELEASE_TAG" release-files/* --clobber --repo "$GITHUB_REPOSITORY"
else
gh release create "$RELEASE_TAG" release-files/* --generate-notes --title "$RELEASE_TAG" --target "$GITHUB_SHA" --repo "$GITHUB_REPOSITORY"
gh release create "$RELEASE_TAG" release-files/* "${release_type[@]}" --generate-notes --title "$RELEASE_TAG" --target "$GITHUB_SHA" --repo "$GITHUB_REPOSITORY"
fi
+40 -5
View File
@@ -1,12 +1,47 @@
FROM golang:1.24-alpine
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS builder
RUN apk add --no-cache ca-certificates
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=dev
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 \
GOOS="${TARGETOS:-linux}" \
GOARCH="${TARGETARCH:-amd64}" \
go build \
-trimpath \
-ldflags="-s -w -X main.version=${VERSION}" \
-o /out/opencode2api ./
FROM alpine:3.22
RUN apk add --no-cache ca-certificates su-exec tzdata \
&& addgroup -S opencode2api \
&& adduser -S -G opencode2api -h /var/lib/opencode2api opencode2api \
&& mkdir -p /app /var/lib/opencode2api \
&& chown -R opencode2api:opencode2api /app /var/lib/opencode2api
COPY --from=builder /out/opencode2api /usr/local/bin/opencode2api
COPY --chmod=0755 docker-entrypoint.sh /usr/local/bin/docker-entrypoint
COPY --chown=opencode2api:opencode2api config.example.json /app/config.example.json
ENV CONFIG_PATH=/var/lib/opencode2api/config.json \
CONFIG_SEED_PATH= \
LISTEN_ADDRESS=0.0.0.0:8080 \
WEBUI_LISTEN_ADDRESS=0.0.0.0:8081 \
STATE_DIR=/var/lib/opencode2api
WORKDIR /app
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod 0755 /usr/local/bin/docker-entrypoint
EXPOSE 8080 8081
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD wget -q -O /dev/null http://127.0.0.1:8080/healthz || exit 1
ENTRYPOINT ["/usr/local/bin/docker-entrypoint"]
+24 -6
View File
@@ -112,33 +112,51 @@ go build -o opencode2api ./
预编译的 Windows、Linux 和 macOS 可执行文件可从 [GitHub Releases](https://github.com/jasonxu114514/opencode2api/releases) 下载。
## Docker Compose 部署
## GHCR / Docker Compose 部署
正式镜像发布在 `ghcr.io/jasonxu114514/opencode2api`
服务器安装 Docker 与 Docker Compose 后,可以在克隆项目后直接启动:
```bash
git clone https://github.com/jasonxu114514/opencode2api.git
cd opencode2api
cp config.example.json config.json
# 编辑 server_keys、zen_keys/go_keys,并修改 webui.password
docker compose pull
docker compose up -d
```
首次启动会把 `config.json` 导入 `opencode2api-state` 命名卷。之后应通过 WebUI 修改配置;如需再次从宿主机导入配置,可执行:
```bash
cp config.example.json config.json
# 编辑 server_keys、zen_keys/go_keys,并修改 webui.password
docker compose cp config.json opencode2api:/var/lib/opencode2api/config.json
docker compose restart
```
健康检查、WebUI 和日志:
```bash
curl http://127.0.0.1:8080/healthz
# 浏览器打开 http://127.0.0.1:8081
docker compose logs -f
```
如需修改宿主机端口:
可通过环境变量固定镜像版本和修改宿主机端口:
```bash
OPENCODE2API_PORT=18080 OPENCODE2API_WEBUI_PORT=18081 docker compose up -d
OPENCODE2API_VERSION=v1.2.3 OPENCODE2API_PORT=18080 OPENCODE2API_WEBUI_PORT=18081 docker compose up -d
```
不使用 Compose 时也可直接运行 GHCR 镜像:
```bash
docker volume create opencode2api-state
docker run -d --name opencode2api --restart unless-stopped \
-p 8080:8080 -p 8081:8081 \
-e CONFIG_SEED_PATH=/run/config/opencode2api.json \
-v "$(pwd)/config.json:/run/config/opencode2api.json:ro" \
-v opencode2api-state:/var/lib/opencode2api \
ghcr.io/jasonxu114514/opencode2api:latest
```
## 配置
+16 -16
View File
@@ -1,30 +1,30 @@
services:
opencode2api:
build:
context: .
dockerfile: Dockerfile
image: opencode2api:local
image: ghcr.io/jasonxu114514/opencode2api:${OPENCODE2API_VERSION:-latest}
pull_policy: always
restart: unless-stopped
read_only: true
security_opt:
- no-new-privileges:true
environment:
CONFIG_PATH: /app/config.json
CONFIG_PATH: /var/lib/opencode2api/config.json
CONFIG_SEED_PATH: /run/config/opencode2api.json
LISTEN_ADDRESS: 0.0.0.0:8080
WEBUI_LISTEN_ADDRESS: 0.0.0.0:8081
ports:
- "${OPENCODE2API_PORT:-8080}:8080"
- "${OPENCODE2API_WEBUI_PORT:-8081}:8081"
configs:
- source: opencode2api_config
target: /run/config/opencode2api.json
volumes:
- .:/app
- opencode2api-state:/var/lib/opencode2api
- go-build-cache:/root/.cache/go-build
- go-module-cache:/go/pkg/mod
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/healthz"]
interval: 30s
timeout: 5s
retries: 3
start_period: 20s
tmpfs:
- /tmp
configs:
opencode2api_config:
file: ./config.json
volumes:
opencode2api-state:
go-build-cache:
go-module-cache:
+24 -46
View File
@@ -1,57 +1,35 @@
#!/bin/sh
set -eu
app_dir=${APP_DIR:-/app}
state_dir=${STATE_DIR:-/var/lib/opencode2api}
config_path=${CONFIG_PATH:-$app_dir/config.json}
config_path=${CONFIG_PATH:-$state_dir/config.json}
config_seed_path=${CONFIG_SEED_PATH:-}
listen_address=${LISTEN_ADDRESS:-0.0.0.0:8080}
webui_listen_address=${WEBUI_LISTEN_ADDRESS:-0.0.0.0:8081}
binary_path=$state_dir/opencode2api
fingerprint_path=$state_dir/source.sha256
mkdir -p "$state_dir"
if [ "$(id -u)" = "0" ]; then
mkdir -p "$state_dir"
chown opencode2api:opencode2api "$state_dir"
exec su-exec opencode2api:opencode2api "$0" "$@"
fi
if [ -f "$config_path" ]; then
active_config=$config_path
else
generated_config=$state_dir/config.json
if [ ! -f "$generated_config" ]; then
cp "$app_dir/config.example.json" "$generated_config"
if [ "$#" -gt 0 ]; then
exec "$@"
fi
if [ ! -f "$config_path" ]; then
mkdir -p "$(dirname "$config_path")"
if [ -n "$config_seed_path" ] && [ -f "$config_seed_path" ]; then
cp "$config_seed_path" "$config_path"
printf '%s\n' "Initialized $config_path from $config_seed_path."
else
cp /app/config.example.json "$config_path"
printf '%s\n' \
"config.json not found; created $config_path. Set API keys or enable anonymous mode, and change the WebUI password before use."
fi
active_config=$generated_config
printf '%s\n' "config.json not found; using a persistent generated copy. Set real API keys or enable anonymous mode, and change the WebUI password before use."
fi
source_fingerprint() {
{
go version
go env GOOS GOARCH CGO_ENABLED
find "$app_dir" \
-path "$app_dir/.git" -prune -o \
-type f \( -name '*.go' -o -name 'go.mod' -o -name 'go.sum' -o -path "$app_dir/webui/*" \) -print \
| LC_ALL=C sort \
| while IFS= read -r source_file; do
sha256sum "$source_file"
done
} | sha256sum | awk '{print $1}'
}
current_fingerprint=$(source_fingerprint)
stored_fingerprint=""
if [ -f "$fingerprint_path" ]; then
stored_fingerprint=$(cat "$fingerprint_path")
fi
if [ ! -x "$binary_path" ] || [ "$current_fingerprint" != "$stored_fingerprint" ]; then
printf '%s\n' "Source changed or no cached binary exists; building opencode2api..."
temporary_binary=$state_dir/opencode2api.new
cd "$app_dir"
go build -trimpath -o "$temporary_binary" ./
mv "$temporary_binary" "$binary_path"
printf '%s\n' "$current_fingerprint" > "$fingerprint_path"
printf '%s\n' "Build completed and cached in the persistent Docker volume."
else
printf '%s\n' "Source is unchanged; starting the cached opencode2api binary."
fi
exec "$binary_path" -config "$active_config" -listen "$listen_address" -web-listen "$webui_listen_address"
exec /usr/local/bin/opencode2api \
-config "$config_path" \
-listen "$listen_address" \
-web-listen "$webui_listen_address"