Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(build): add go-task support #1608

Merged
merged 6 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
# the frontend builder
# cloudreve need node.js 16* to build frontend,
# separate build step and custom image tag will resolve this
FROM node:16-alpine as cloudreve_frontend_builder
# !!! Doesn't require any cleanup, as multi-stage builds are removed after completion
FROM node:16-alpine as frontend_builder

RUN apk update \
&& apk add --no-cache wget curl git yarn zip bash \
&& git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git /cloudreve_frontend
RUN set -e \
&& apk update \
&& apk add bash wget curl git zip \
&& sh -c "$(curl -sSL https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
# Maybe copying the current directory is more accurate?
# && git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git /cloudreve

# build frontend assets using build script, make sure all the steps just follow the regular release
WORKDIR /cloudreve_frontend
ENV GENERATE_SOURCEMAP false
RUN chmod +x ./build.sh && ./build.sh -a
COPY . /cloudreve

WORKDIR /cloudreve

RUN task clean-frontend build-frontend


# the backend builder
# cloudreve backend needs golang 1.18* to build
FROM golang:1.18-alpine as cloudreve_backend_builder
# !!! Doesn't require any cleanup, as multi-stage builds are removed after completion
FROM golang:1.18-alpine as backend_builder

# install dependencies and build tools
RUN apk update \
# install dependencies and build tools
&& apk add --no-cache wget curl git build-base gcc abuild binutils binutils-doc gcc-doc zip bash \
&& git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git /cloudreve_backend
RUN set -e \
&& apk update \
&& apk add bash wget curl git build-base \
&& sh -c "$(curl -sSL https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
# Maybe copying the current directory is more accurate?
# && git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git /cloudreve

WORKDIR /cloudreve_backend
COPY --from=cloudreve_frontend_builder /cloudreve_frontend/assets.zip ./
RUN chmod +x ./build.sh && ./build.sh -c
COPY . /cloudreve

WORKDIR /cloudreve

COPY --from=frontend_builder /cloudreve/assets.zip ./

# TODO: merge the frontend build and backend build into a single one image
# the final published image
RUN task clean-backend build-backend "PLATFORM=docker"


# the final builder
FROM alpine:latest

WORKDIR /cloudreve
COPY --from=cloudreve_backend_builder /cloudreve_backend/cloudreve ./cloudreve

RUN apk update \
&& apk add --no-cache tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& chmod +x ./cloudreve \
&& mkdir -p /data/aria2 \
&& chmod -R 766 /data/aria2
COPY --from=backend_builder /cloudreve/release/cloudreve-docker ./cloudreve

# !!! For i18n users, do not set timezone
RUN set -e \
&& apk update \
&& apk add bash ca-certificates tzdata \
&& rm -f /var/cache/apk/*

EXPOSE 5212

VOLUME ["/cloudreve/uploads", "/cloudreve/avatar", "/data"]

ENTRYPOINT ["./cloudreve"]
79 changes: 52 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,50 +74,75 @@ The above is a minimum deploy example, you can refer to [Getting started](https:

## :gear: Build

You need to have `Go >= 1.18`, `node.js`, `yarn`, `zip` and other necessary dependencies before you can build it yourself.
You need to have `Go >= 1.18`, `node.js`, `yarn`, `curl`, `zip`, `go-task` and other necessary dependencies before you can build it yourself.

#### Clone the code
#### Install go-task

```shell
git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git
sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
```

#### Build static resources
For more installation methods, please refer to the official documentation: [https://taskfile.dev/installation/](https://taskfile.dev/installation/)

#### Clone the code

```shell
# Enter frontend sub-module
cd assets
# Install dependencies
yarn install
# Start building
yarn run build
# Delete unused map files
cd build
find . -name "*.map" -type f -delete
# Return to main folder to pack static files
cd ../../
zip -r - assets/build >assets.zip
git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git
```

#### Compile

```shell
# Obtain version number, commit SHA
export COMMIT_SHA=$(git rev-parse --short HEAD)
export VERSION=$(git describe --tags)
# Enter the project directory
cd Cloudreve

# Execute the task command
# Note: The `task` command executes the task named `default` by default.
task

# Compile
go build -a -o cloudreve -ldflags "-s -w -X 'github.com/cloudreve/Cloudreve/v3/pkg/conf.BackendVersion=$VERSION' -X 'github.com/cloudreve/Cloudreve/v3/pkg/conf.LastCommit=$COMMIT_SHA'"
# View compiled files
ls release
```

You can also start a quick build using `build.sh` in the project root directory:
If you want to compile only the frontend code, please execute `task build-frontend`; similarly you can also execute `task build-backend` to only compile the backend code.

You can view all supported tasks through the `task --list` command:

```shell
./build.sh [-a] [-c] [-b] [-r]
a - Build assets
c - Build binary backend
b - Build both assets and backend
r - Cross-compilation for final release
~/Cloudreve ❯❯❯ task --list ✘ 146 master
task: Available tasks for this project:
* all: Build All Platform
* build: Build Cloudreve
* build-backend: Build Backend
* build-frontend: Build Frontend
* clean: Clean All Build Cache
* clean-backend: Clean Backend Build Cache
* clean-frontend: Clean Frontend Build Cache
* darwin-amd64: Build Backend(darwin-amd64)
* darwin-amd64-v2: Build Backend(darwin-amd64-v2)
* darwin-amd64-v3: Build Backend(darwin-amd64-v3)
* darwin-amd64-v4: Build Backend(darwin-amd64-v4)
* darwin-arm64: Build Backend(darwin-arm64)
* freebsd-386: Build Backend(freebsd-386)
* freebsd-amd64: Build Backend(freebsd-amd64)
* freebsd-amd64-v2: Build Backend(freebsd-amd64-v2)
* freebsd-amd64-v3: Build Backend(freebsd-amd64-v3)
* freebsd-amd64-v4: Build Backend(freebsd-amd64-v4)
* freebsd-arm: Build Backend(freebsd-arm)
* freebsd-arm64: Build Backend(freebsd-arm64)
* linux-amd64: Build Backend(linux-amd64)
* linux-amd64-v2: Build Backend(linux-amd64-v2)
* linux-amd64-v3: Build Backend(linux-amd64-v3)
* linux-amd64-v4: Build Backend(linux-amd64-v4)
* linux-armv5: Build Backend(linux-armv5)
* linux-armv6: Build Backend(linux-armv6)
* linux-armv7: Build Backend(linux-armv7)
* linux-armv8: Build Backend(linux-armv8)
* windows-amd64: Build Backend(windows-amd64)
* windows-amd64-v2: Build Backend(windows-amd64-v2)
* windows-amd64-v3: Build Backend(windows-amd64-v3)
* windows-amd64-v4: Build Backend(windows-amd64-v4)
* windows-arm64: Build Backend(windows-arm64)
```

## :alembic: Stacks
Expand Down
79 changes: 52 additions & 27 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,50 +74,75 @@ chmod +x ./cloudreve

## :gear: 构建

自行构建前需要拥有 `Go >= 1.18`、`node.js`、`yarn`、`zip` 等必要依赖。
自行构建前需要拥有 `Go >= 1.18`、`node.js`、`yarn`、`curl`、`zip`、`go-task` 等必要依赖。

#### 克隆代码
#### 安装 go-task

```shell
git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git
sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
```

#### 构建静态资源
对于其他平台或者其他安装方式, 请参考官方安装文档: [https://taskfile.dev/installation/](https://taskfile.dev/installation/)

#### 克隆代码

```shell
# 进入前端子模块
cd assets
# 安装依赖
yarn install
# 开始构建
yarn run build
# 构建完成后删除映射文件
cd build
find . -name "*.map" -type f -delete
# 返回项目主目录打包静态资源
cd ../../
zip -r - assets/build >assets.zip
git clone --recurse-submodules https://github.com/cloudreve/Cloudreve.git
```

#### 编译项目

```shell
# 获得当前版本号、Commit
export COMMIT_SHA=$(git rev-parse --short HEAD)
export VERSION=$(git describe --tags)
# 进入项目目录
cd Cloudreve

# 执行 task 命令
# 提示: 单独执行 `task` 命令实际上默认执行叫做 `default` 的 task
task

# 开始编译
go build -a -o cloudreve -ldflags "-s -w -X 'github.com/cloudreve/Cloudreve/v3/pkg/conf.BackendVersion=$VERSION' -X 'github.com/cloudreve/Cloudreve/v3/pkg/conf.LastCommit=$COMMIT_SHA'"
# 查看已编译好的文件
ls release
```

你也可以使用项目根目录下的 `build.sh` 快速开始构建:
如果你期望单独编译前端代码, 请执行 `task build-frontend` 命令; 同样你可以通过执行 `task build-backend` 命令单独编译后端代码.

你可以通过执行 `task --list` 命令查看所以已支持的 task.

```shell
./build.sh [-a] [-c] [-b] [-r]
a - 构建静态资源
c - 编译二进制文件
b - 构建前端 + 编译二进制文件
r - 交叉编译,构建用于release的版本
~/Cloudreve ❯❯❯ task --list ✘ 146 master ✱
task: Available tasks for this project:
* all: Build All Platform
* build: Build Cloudreve
* build-backend: Build Backend
* build-frontend: Build Frontend
* clean: Clean All Build Cache
* clean-backend: Clean Backend Build Cache
* clean-frontend: Clean Frontend Build Cache
* darwin-amd64: Build Backend(darwin-amd64)
* darwin-amd64-v2: Build Backend(darwin-amd64-v2)
* darwin-amd64-v3: Build Backend(darwin-amd64-v3)
* darwin-amd64-v4: Build Backend(darwin-amd64-v4)
* darwin-arm64: Build Backend(darwin-arm64)
* freebsd-386: Build Backend(freebsd-386)
* freebsd-amd64: Build Backend(freebsd-amd64)
* freebsd-amd64-v2: Build Backend(freebsd-amd64-v2)
* freebsd-amd64-v3: Build Backend(freebsd-amd64-v3)
* freebsd-amd64-v4: Build Backend(freebsd-amd64-v4)
* freebsd-arm: Build Backend(freebsd-arm)
* freebsd-arm64: Build Backend(freebsd-arm64)
* linux-amd64: Build Backend(linux-amd64)
* linux-amd64-v2: Build Backend(linux-amd64-v2)
* linux-amd64-v3: Build Backend(linux-amd64-v3)
* linux-amd64-v4: Build Backend(linux-amd64-v4)
* linux-armv5: Build Backend(linux-armv5)
* linux-armv6: Build Backend(linux-armv6)
* linux-armv7: Build Backend(linux-armv7)
* linux-armv8: Build Backend(linux-armv8)
* windows-amd64: Build Backend(windows-amd64)
* windows-amd64-v2: Build Backend(windows-amd64-v2)
* windows-amd64-v3: Build Backend(windows-amd64-v3)
* windows-amd64-v4: Build Backend(windows-amd64-v4)
* windows-arm64: Build Backend(windows-arm64)
```

## :alembic: 技术栈
Expand Down
Loading