build: build with docker

This commit is contained in:
鲁树人 2025-03-28 12:14:59 +09:00
parent a40ecc4569
commit a07bcf2575
5 changed files with 63 additions and 3 deletions

3
.dockerignore Normal file
View File

@ -0,0 +1,3 @@
dist
node_modules
*.log

25
Dockerfile Normal file
View File

@ -0,0 +1,25 @@
FROM node:22-slim AS build
ENV PNPM_HOME="/p"
ENV PATH="$PNPM_HOME:$PATH"
WORKDIR /app
RUN corepack enable pnpm \
&& apt-get update \
&& apt-get install -y --no-install-recommends git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY package.json pnpm-lock.yaml .npmrc ./
RUN pnpm exec true
COPY . .
RUN pnpm install --frozen-lockfile
ARG GIT_COMMIT=
ARG GIT_COMMIT_FULL=
RUN pnpm build
FROM caddy:latest
COPY --from=build /app/dist /srv/um-react
EXPOSE 80
CMD ["caddy", "file-server", "--root", "/srv/um-react"]

View File

@ -59,6 +59,33 @@
[project-issues]: https://git.unlock-music.dev/um/um-react/issues/new
## 使用 Docker 构建、部署 (Linux)
首先克隆仓库并进入目录:
```sh
git clone https://git.unlock-music.dev/um/um-react.git
cd um-react
```
构建 Docker 镜像:
```sh
docker build \
-t um-react \
--build-arg GIT_COMMIT_FULL="$(git describe --long --dirty --tags --always)" \
--build-arg GIT_COMMIT="$(git rev-parse --short HEAD)" \
.
```
在后台运行 Docker 容器:
```sh
docker run -d -p 8080:80 --name um-react um-react
```
然后访问 `http://localhost:8080` 即可。
## 开发相关
从源码运行或编译生产版本,请参考文档「[新手上路](./docs/getting-started.zh.md)」。

View File

@ -7,8 +7,13 @@ import { execSync } from 'node:child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const commitHash = execSync('git rev-parse --short HEAD').toString('utf-8').trim();
let commitHash = process.env.GIT_COMMIT || 'unknown';
try {
execSync('git rev-parse --short HEAD').toString('utf-8').trim();
} catch (e) {
console.error('Failed to get commit hash:', e);
}
const pkgJson = JSON.parse(readFileSync(__dirname + '/../package.json', 'utf-8'));
const pkgVer = `${pkgJson.version ?? 'unknown'}-${commitHash ?? 'unknown'}` + '\n';
const pkgVer = `${pkgJson.version ?? 'unknown'}-${commitHash}` + '\n';
writeFileSync(__dirname + '/../dist/version.txt', pkgVer, 'utf-8');

View File

@ -15,7 +15,7 @@ const projectRoot = url.fileURLToPath(new URL('.', import.meta.url));
const pkg = JSON.parse(fs.readFileSync(projectRoot + '/package.json', 'utf-8'));
const COMMAND_GIT_VERSION = 'git describe --long --dirty --tags --always';
const shortCommit = tryCommand(COMMAND_GIT_VERSION, __dirname, 'unknown');
const shortCommit = process.env.GIT_COMMIT_FULL || tryCommand(COMMAND_GIT_VERSION, __dirname, 'unknown');
const version = `${pkg.version}-${shortCommit}`;
// https://vitejs.dev/config/