进阶 docs.cloudreve.org 2026-10-17 18:46:57 · 21 阅读

第7章 从源代码构建 Cloudreve

从源代码构建

Cloudreve 项目主要由两部分组成:后端主仓库 cloudreve/Cloudreve 和前端仓库 cloudreve/frontend。在编译 Cloudreve 后端之前,你需要先在 assets 目录中构建前端子模块,然后使用 embed 将其嵌入到后端可执行文件中。

安装依赖

  • Golang >= 1.23
  • Node.js >= 20
  • Yarn
  • Goreleaser

克隆仓库

# 克隆仓库
git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git

# 进入项目目录
cd Cloudreve

# 切换到要编译的版本
git checkout 4.x.x

构建前端

# 获取当前版本号及提交哈希
export COMMIT_SHA=$(git rev-parse --short HEAD)
export VERSION=$(git describe --tags)

chmod +x ./.build/build-assets.sh

# 构建前端
./.build/build-assets.sh $VERSION

编译后的前端资源包位于 application/statics/assets.zip。

对于 Pro 版本用户,请从许可管理后台下载前端源代码,然后在源代码目录下手动构建:

# 安装依赖
yarn install

# 标记前端版本号
yarn version --new-version 4.x.x --no-git-tag-version

# 构建前端
yarn run build

构建输出位于 build 目录。

构建最终可执行文件

# 开始编译
go build -a -o cloudreve \
    -ldflags "-s -w -X 'github.com/cloudreve/Cloudreve/v4/application/constants.BackendVersion=$VERSION' -X 'github.com/cloudreve/Cloudreve/v4/application/constants.LastCommit=$COMMIT_SHA'"

编译完成后,项目根目录将生成最终的可执行文件 cloudreve。

构建辅助工具

你可以使用 goreleaser 快速完成构建、打包等操作。用法如下:

# 安装 goreleaser
go install github.com/goreleaser/goreleaser/v2@latest

# 构建项目
goreleaser build --clean --single-target --snapshot

或者交叉编译所有可用版本:

goreleaser build --clean --snapshot

评论 (0)