diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index a695ab6..0d8bc15 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -2,6 +2,11 @@ name: Build and push Docker image on: workflow_dispatch: + inputs: + release_tag: + description: Optional release tag created by Release Please + required: false + type: string push: branches: - master @@ -20,6 +25,7 @@ jobs: - name: Checkout uses: actions/checkout@v4 with: + ref: ${{ inputs.release_tag || github.ref }} submodules: true - name: Extract metadata @@ -27,14 +33,63 @@ jobs: uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY_IMAGE }} + flavor: | + latest=false tags: | - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} + type=ref,event=branch,enable=${{ inputs.release_tag == '' }} + type=ref,event=pr,enable=${{ inputs.release_tag == '' }} + type=semver,pattern={{version}},enable=${{ inputs.release_tag == '' }} + type=semver,pattern={{major}}.{{minor}},enable=${{ inputs.release_tag == '' }} + type=semver,value=${{ inputs.release_tag }},pattern={{version}},enable=${{ inputs.release_tag != '' }} + type=semver,value=${{ inputs.release_tag }},pattern={{major}}.{{minor}},enable=${{ inputs.release_tag != '' }} type=raw,value=dev,enable=${{ github.ref == 'refs/heads/dev' }} - type=raw,value=beta,enable={{is_default_branch}} - type=raw,value=latest,enable={{is_default_branch}} + type=sha,prefix=edge-,enable=${{ github.ref == 'refs/heads/master' && inputs.release_tag == '' }} + type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') || inputs.release_tag != '' }} + + - name: Verify release version + if: ${{ startsWith(github.ref, 'refs/tags/v') || inputs.release_tag != '' }} + shell: bash + env: + RELEASE_TAG: ${{ inputs.release_tag || github.ref_name }} + run: | + source_version="$(tr -d '[:space:]' < VERSION)" + tag_version="${RELEASE_TAG#v}" + test "$source_version" = "$tag_version" || { + echo "Version mismatch: VERSION=$source_version, tag=$tag_version" >&2 + exit 1 + } + + - name: Resolve source revision + id: source-revision + shell: bash + run: | + revision="$(git rev-parse HEAD)" + echo "value=$revision" >> "$GITHUB_OUTPUT" + echo "short=${revision::12}" >> "$GITHUB_OUTPUT" + + - name: Resolve latest frontend revisions + id: frontend-revisions + shell: bash + run: | + frontend_2024_ref="$(git ls-remote https://github.com/vastsa/FileCodeBoxFronted.git HEAD | cut -f1)" + frontend_2023_ref="$(git ls-remote https://github.com/vastsa/FileCodeBoxFronted2023.git HEAD | cut -f1)" + test -n "$frontend_2024_ref" + test -n "$frontend_2023_ref" + echo "frontend_2024=$frontend_2024_ref" >> "$GITHUB_OUTPUT" + echo "frontend_2023=$frontend_2023_ref" >> "$GITHUB_OUTPUT" + + - name: Resolve application version + id: app-version + shell: bash + env: + RELEASE_TAG: ${{ inputs.release_tag }} + run: | + source_version="$(tr -d '[:space:]' < VERSION)" + if [[ "$GITHUB_REF" == refs/tags/v* || -n "$RELEASE_TAG" ]]; then + echo "value=$source_version" >> "$GITHUB_OUTPUT" + else + echo "value=${source_version}-dev+${{ steps.source-revision.outputs.short }}" >> "$GITHUB_OUTPUT" + fi - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -60,6 +115,11 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + build-args: | + APP_VERSION=${{ steps.app-version.outputs.value }} + VCS_REF=${{ steps.source-revision.outputs.value }} + FRONTEND_2024_REF=${{ steps.frontend-revisions.outputs.frontend_2024 }} + FRONTEND_2023_REF=${{ steps.frontend-revisions.outputs.frontend_2023 }} cache-from: type=gha cache-to: type=gha,mode=max provenance: false diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..e0cea9b --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,54 @@ +name: Create release + +on: + push: + branches: + - master + workflow_dispatch: + +permissions: + actions: write + contents: write + issues: write + pull-requests: write + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +jobs: + release-please: + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Create or update release + id: release + uses: googleapis/release-please-action@v4 + with: + config-file: release-please-config.json + manifest-file: .release-please-manifest.json + + - name: Merge release pull request + if: ${{ steps.release.outputs.prs_created == 'true' }} + env: + GH_TOKEN: ${{ github.token }} + RELEASE_PR: ${{ fromJSON(steps.release.outputs.pr).number }} + run: | + for attempt in {1..120}; do + if gh pr merge "$RELEASE_PR" --squash --delete-branch; then + break + fi + if [[ "$attempt" -eq 120 ]]; then + echo "Timed out waiting for release PR #$RELEASE_PR to merge" >&2 + exit 1 + fi + sleep 10 + done + gh workflow run release.yml --ref master + + - name: Publish release image + if: ${{ steps.release.outputs.release_created == 'true' }} + env: + GH_TOKEN: ${{ github.token }} + RELEASE_TAG: ${{ steps.release.outputs.tag_name }} + run: gh workflow run docker-image.yml --ref master -f release_tag="$RELEASE_TAG" diff --git a/.release-please-manifest.json b/.release-please-manifest.json new file mode 100644 index 0000000..a549f59 --- /dev/null +++ b/.release-please-manifest.json @@ -0,0 +1,3 @@ +{ + ".": "2.4.0" +} diff --git a/Dockerfile b/Dockerfile index 0a1778c..5306f50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,9 @@ # 前端产物与 CPU 架构无关,使用原生构建平台避免在 QEMU 中运行 Node/pnpm。 FROM --platform=$BUILDPLATFORM node:20-alpine AS frontend-builder +ARG FRONTEND_2024_REF=main +ARG FRONTEND_2023_REF=main + RUN apk add --no-cache git python3 make g++ RUN corepack enable && \ @@ -9,28 +12,43 @@ RUN corepack enable && \ WORKDIR /build -# 克隆并构建 2024 主题 -RUN git clone --depth 1 https://github.com/vastsa/FileCodeBoxFronted.git /build/fronted-2024 && \ +# 克隆并构建固定版本的 2024 主题 +RUN git clone --filter=blob:none --no-checkout https://github.com/vastsa/FileCodeBoxFronted.git /build/fronted-2024 && \ cd /build/fronted-2024 && \ + git fetch --depth 1 origin "${FRONTEND_2024_REF}" && \ + git checkout --detach FETCH_HEAD && \ pnpm install --frozen-lockfile --prod=false && \ - pnpm run build + VITE_GIT_COMMIT="$(git rev-parse HEAD)" pnpm run build -# 克隆并构建 2023 主题 -RUN git clone --depth 1 https://github.com/vastsa/FileCodeBoxFronted2023.git /build/fronted-2023 && \ +# 克隆并构建固定版本的 2023 主题 +RUN git clone --filter=blob:none --no-checkout https://github.com/vastsa/FileCodeBoxFronted2023.git /build/fronted-2023 && \ cd /build/fronted-2023 && \ + git fetch --depth 1 origin "${FRONTEND_2023_REF}" && \ + git checkout --detach FETCH_HEAD && \ npm install --legacy-peer-deps && \ npm run build # 第二阶段:构建最终镜像 FROM python:3.12-slim-bookworm +ARG APP_VERSION +ARG VCS_REF=unknown +ARG FRONTEND_2024_REF=main +ARG FRONTEND_2023_REF=main LABEL author="Lan" LABEL email="xzu@live.com" +LABEL org.opencontainers.image.version="${APP_VERSION}" +LABEL org.opencontainers.image.revision="${VCS_REF}" +LABEL org.opencontainers.image.filecodebox.frontend-2024-revision="${FRONTEND_2024_REF}" +LABEL org.opencontainers.image.filecodebox.frontend-2023-revision="${FRONTEND_2023_REF}" WORKDIR /app # 复制项目文件(通过 .dockerignore 排除不必要的文件) COPY . . +# 分支镜像使用带提交号的开发版本;正式镜像使用 VERSION 中的版本。 +ENV APP_VERSION="${APP_VERSION}" + # 设置时区 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo 'Asia/Shanghai' > /etc/timezone diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..197c4d5 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +2.4.0 diff --git a/core/version.py b/core/version.py index bb219e8..85755df 100644 --- a/core/version.py +++ b/core/version.py @@ -1 +1,20 @@ -APP_VERSION = "2.4.0" +import os +import re +from pathlib import Path + + +VERSION_FILE = Path(__file__).resolve().parents[1] / "VERSION" +VERSION_PATTERN = re.compile( + r"^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$" +) + + +def load_app_version() -> str: + version = os.getenv("APP_VERSION") or VERSION_FILE.read_text(encoding="utf-8") + version = version.strip() + if not VERSION_PATTERN.fullmatch(version): + raise RuntimeError(f"无效的应用版本号: {version!r}") + return version + + +APP_VERSION = load_app_version() diff --git a/docker-compose.yml b/docker-compose.yml index eff13d8..b46d849 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: "3" services: file-code-box: - image: lanol/filecodebox:latest + image: lanol/filecodebox:2.4.0 # x-release-please-version volumes: - fcb-data:/app/data:rw restart: unless-stopped @@ -9,4 +9,4 @@ services: - "12345:12345" volumes: fcb-data: - external: false \ No newline at end of file + external: false diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index 6276980..1d4b847 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -55,6 +55,7 @@ export default defineConfig({ { text: '存储配置', link: '/guide/storage' }, { text: '安全设置', link: '/guide/security' }, { text: '系统配置', link: '/guide/configuration' }, + { text: '版本发布', link: '/guide/releasing' }, ], }, ], @@ -148,6 +149,7 @@ export default defineConfig({ text: 'System Configuration', link: '/en/guide/configuration', }, + { text: 'Releasing', link: '/en/guide/releasing' }, ], }, ], diff --git a/docs/en/changelog.md b/docs/en/changelog.md index 0dbc7a7..94d9d9c 100644 --- a/docs/en/changelog.md +++ b/docs/en/changelog.md @@ -1,5 +1,7 @@ ## [Unreleased] + + ### Added - Added a global storage capacity limit with concurrency-safe reservations for regular, chunked, presigned, and admin file uploads. diff --git a/docs/en/guide/releasing.md b/docs/en/guide/releasing.md new file mode 100644 index 0000000..c0c9b57 --- /dev/null +++ b/docs/en/guide/releasing.md @@ -0,0 +1,25 @@ +# Releasing + +The project uses Semantic Versioning and Conventional Commits. + +Complete these settings once in both GitHub repositories: + +- In `Settings → Actions → General → Workflow permissions`, grant read/write + access and allow GitHub Actions to create pull requests. +- Keep squash merging enabled. The release workflow waits for required checks + before merging the release pull request. +- Add `DOCKER_USERNAME` and `DOCKER_PASSWORD` Actions secrets to the backend repository. + +1. Merge commits using Conventional Commit types such as `feat:` and `fix:` into the default branch. +2. Release Please creates and merges the release pull request and updates `VERSION` and the changelog. +3. The workflow then creates `vX.Y.Z`, a GitHub Release, and dispatches the production image build. +4. Releases publish `X.Y.Z`, `X.Y`, and `latest`; branches publish `dev` or `edge-*` images. +5. Each image build resolves the latest commit from both frontend default branches automatically. + +Every `fix:`, `feat:`, or breaking change merged into the default branch therefore +creates a Patch, Minor, or Major release automatically. Non-release commits such +as `docs:` and `chore:` do not publish a release on their own. + +The backend runtime version comes from `VERSION`. Container branch builds inject a +commit-qualified development version through `APP_VERSION`; release tags must exactly +match `VERSION`. OCI image labels record the resolved backend and frontend commits for traceability. diff --git a/docs/guide/releasing.md b/docs/guide/releasing.md new file mode 100644 index 0000000..fb8fbf5 --- /dev/null +++ b/docs/guide/releasing.md @@ -0,0 +1,23 @@ +# 版本发布 + +项目采用 Semantic Versioning 和 Conventional Commits 管理版本。 + +首次启用时,需要在两个 GitHub 仓库完成以下设置: + +- `Settings → Actions → General → Workflow permissions` 选择读写权限,并允许 + GitHub Actions 创建 Pull Request。 +- 保持 Squash merge 可用;自动发布工作流会等待必需检查通过后合并发布 PR。 +- 后端仓库配置 `DOCKER_USERNAME` 和 `DOCKER_PASSWORD` Actions Secrets。 + +1. 日常提交使用 `feat:`、`fix:` 等 Conventional Commits 前缀并合入主分支。 +2. Release Please 自动创建并合并发布 PR,更新 `VERSION` 和更新日志。 +3. 工作流随后自动创建 `vX.Y.Z`、GitHub Release 并触发正式镜像构建。 +4. 正式镜像发布 `X.Y.Z`、`X.Y` 和 `latest`;分支镜像使用 `dev` 或 `edge-*`。 +5. 镜像构建时自动读取两个前端仓库默认分支的最新提交,无需手工同步版本。 + +因此每次合入主分支的 `fix:`、`feat:` 或破坏性变更都会自动产生对应的 +Patch、Minor 或 Major 版本。`docs:`、`chore:` 等不影响版本的提交不会单独发版。 + +后端运行版本来自 `VERSION`。容器分支构建通过 `APP_VERSION` 注入带提交号的 +开发版本;正式标签必须与 `VERSION` 完全一致。Docker 镜像的 OCI 标签还记录 +后端和两个前端仓库当次解析到的精确提交,便于定位构建内容。 diff --git a/main.py b/main.py index 1968b08..7073cae 100644 --- a/main.py +++ b/main.py @@ -745,7 +745,7 @@ async def load_config(): ip_limit["upload"].minutes = settings.uploadMinute ip_limit["upload"].count = settings.uploadCount -app = FastAPI(lifespan=lifespan) +app = FastAPI(lifespan=lifespan, version=APP_VERSION) @app.middleware("http") async def refresh_settings_middleware(request, call_next): diff --git a/readme.md b/readme.md index b248d67..4da7905 100644 --- a/readme.md +++ b/readme.md @@ -175,11 +175,14 @@ FileCodeBox 是一个轻量级的文件分享工具,基于 **FastAPI + Vue3** ### Docker 部署(推荐) +生产环境建议使用固定版本标签(例如 `2.4.0`)以便复现和回滚; +`latest` 仅在正式版本发布时更新,`edge-*` 是 master 分支开发镜像。 + **方式一:Docker CLI** ```bash # Docker Hub(推荐) -docker run -d --restart always -p 12345:12345 -v /opt/FileCodeBox:/app/data --name filecodebox lanol/filecodebox:latest +docker run -d --restart always -p 12345:12345 -v /opt/FileCodeBox:/app/data --name filecodebox lanol/filecodebox:2.4.0 # x-release-please-version # 国内镜像(如果 Docker Hub 拉取缓慢) docker run -d --restart always -p 12345:12345 -v /opt/FileCodeBox:/app/data --name filecodebox docker.cnb.cool/aixk/filecodebox @@ -190,7 +193,7 @@ docker run -d --restart always -p 12345:12345 -v /opt/FileCodeBox:/app/data --na ```yaml services: filecodebox: - image: lanol/filecodebox:latest + image: lanol/filecodebox:2.4.0 # x-release-please-version container_name: filecodebox restart: unless-stopped ports: diff --git a/readme_en.md b/readme_en.md index 91cb8ed..576ae32 100644 --- a/readme_en.md +++ b/readme_en.md @@ -175,11 +175,15 @@ FileCodeBox is a lightweight file sharing tool built with **FastAPI + Vue3**. Us ### Docker Deployment (Recommended) +Pin a version tag (for example, `2.4.0`) in production for reproducible +deployments and rollbacks. `latest` moves only on formal releases; `edge-*` +identifies development images built from master. + **Option 1: Docker CLI** ```bash # Docker Hub (Recommended) -docker run -d --restart always -p 12345:12345 -v /opt/FileCodeBox:/app/data --name filecodebox lanol/filecodebox:latest +docker run -d --restart always -p 12345:12345 -v /opt/FileCodeBox:/app/data --name filecodebox lanol/filecodebox:2.4.0 # x-release-please-version # China Mirror (if Docker Hub is slow) docker run -d --restart always -p 12345:12345 -v /opt/FileCodeBox:/app/data --name filecodebox docker.cnb.cool/aixk/filecodebox @@ -190,7 +194,7 @@ docker run -d --restart always -p 12345:12345 -v /opt/FileCodeBox:/app/data --na ```yaml services: filecodebox: - image: lanol/filecodebox:latest + image: lanol/filecodebox:2.4.0 # x-release-please-version container_name: filecodebox restart: unless-stopped ports: diff --git a/release-please-config.json b/release-please-config.json new file mode 100644 index 0000000..4cdc350 --- /dev/null +++ b/release-please-config.json @@ -0,0 +1,32 @@ +{ + "$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json", + "packages": { + ".": { + "release-type": "simple", + "component": "filecodebox", + "package-name": "filecodebox", + "include-v-in-tag": true, + "include-component-in-tag": false, + "version-file": "VERSION", + "changelog-path": "docs/changelog.md", + "extra-files": [ + { + "type": "generic", + "path": "docs/en/changelog.md" + }, + { + "type": "generic", + "path": "docker-compose.yml" + }, + { + "type": "generic", + "path": "readme.md" + }, + { + "type": "generic", + "path": "readme_en.md" + } + ] + } + } +} diff --git a/tests/test_version.py b/tests/test_version.py new file mode 100644 index 0000000..32c3f39 --- /dev/null +++ b/tests/test_version.py @@ -0,0 +1,40 @@ +import json +import os +import unittest +from unittest.mock import patch + +from core.version import APP_VERSION, VERSION_FILE, load_app_version +from main import app + + +class ApplicationVersionTests(unittest.TestCase): + def test_version_file_is_the_runtime_version_source(self): + version = VERSION_FILE.read_text(encoding="utf-8").strip() + + self.assertRegex(version, r"^\d+\.\d+\.\d+$") + with patch.dict(os.environ, {}, clear=True): + self.assertEqual(load_app_version(), version) + self.assertEqual(app.version, APP_VERSION) + + def test_release_manifest_matches_version_file(self): + manifest_file = VERSION_FILE.parent / ".release-please-manifest.json" + manifest = json.loads(manifest_file.read_text(encoding="utf-8")) + + self.assertEqual(manifest["."], VERSION_FILE.read_text(encoding="utf-8").strip()) + + def test_build_can_override_version_with_environment(self): + for version in ("9.8.7", "9.8.7-dev+abc123"): + with self.subTest(version=version): + with patch.dict(os.environ, {"APP_VERSION": version}): + self.assertEqual(load_app_version(), version) + + def test_rejects_non_canonical_versions(self): + for version in ("v2.4.0", "2.4", "2.4.0 beta"): + with self.subTest(version=version): + with patch.dict(os.environ, {"APP_VERSION": version}): + with self.assertRaises(RuntimeError): + load_app_version() + + +if __name__ == "__main__": + unittest.main()