Skip to content

Commit eafe706

Browse files
committed
feat: 优化Docker构建流程并添加缓存镜像支持
1 parent 7f5c1d8 commit eafe706

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

.cache.dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:18.20-alpine as Builder
2+
3+
WORKDIR /space
4+
5+
COPY . .
6+
7+
RUN apk add git \
8+
&& git config --global url."https://".insteadOf git:// \
9+
&& npm install --registry=http://registry.npmmirror.com
10+
11+
ENV NODE_PATH=/space/node_modules

.cnb/workflows/build-docker-images.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ main:
66
- docker
77
- git-clone-yyds
88
stages:
9+
- name: 构建缓存镜像
10+
type: docker:cache
11+
options:
12+
dockerfile: cache.dockerfile
13+
by:
14+
- package.json
15+
- package-lock.json
16+
versionBy:
17+
- package.json
18+
exports:
19+
name: DOCKER_CACHE_IMAGE_NAME
20+
- name: 直接使用缓存镜像环境安装依赖并编译
21+
image: $DOCKER_CACHE_IMAGE_NAME
22+
script: |
23+
ln -snf $NODE_PATH node_modules
24+
time npm install
25+
sed -i 's@http://localhost:8888/@/@g' .env.production
26+
time yarn build:prod
927
- name: 🐋 推送镜像
1028
script: |
1129
docker run -d --name buildkitd \

.cnb/workflows/release.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,27 @@ $:
66
- docker
77
- git-clone-yyds
88
stages:
9-
- name: 📦 构建制品
10-
image: docker.cnb.cool/znb/images/debian:all
9+
- name: 构建缓存镜像
10+
type: docker:cache
11+
options:
12+
dockerfile: cache.dockerfile
13+
by:
14+
- package.json
15+
- package-lock.json
16+
versionBy:
17+
- package.json
18+
exports:
19+
name: DOCKER_CACHE_IMAGE_NAME
20+
- name: 直接使用缓存镜像环境安装依赖并编译
21+
image: $DOCKER_CACHE_IMAGE_NAME
1122
script: |
23+
ln -snf $NODE_PATH node_modules
24+
time npm install
1225
sed -i 's@http://localhost:8888/@/@g' .env.production
13-
npm install --registry=http://registry.npmmirror.com
14-
yarn build:prod
26+
time yarn build:prod
27+
- name: 📦 构建制品
28+
image: docker.cnb.cool/znb/images/debian
29+
script: |
1530
zip -q -r dist.zip dist
1631
openssl md5 dist.zip > dist.zip.md5
1732
- name: 🐋 推送制品

.github/workflows/dist-to-release.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Checkout 🛎️
15-
uses: actions/checkout@v3
15+
uses: actions/checkout@v5
1616
- name: Inject slug/short variables
17-
uses: rlespinasse/github-slug-action@v4
17+
uses: rlespinasse/github-slug-action@v5
1818

1919
- name: build project ⚗️
20-
uses: actions/setup-node@v3
20+
uses: actions/setup-node@v5
2121
with:
2222
node-version: 14
23+
cache: 'npm'
24+
cache-dependency-path: "package-lock.json"
2325

2426
- run: |
2527
sed -i 's@http://localhost:8888/@/@g' .env.production
@@ -32,7 +34,7 @@ jobs:
3234
openssl md5 dist.zip > dist.zip.md5
3335
3436
- name: Upload release package
35-
uses: softprops/action-gh-release@v1
37+
uses: softprops/action-gh-release@v2
3638
with:
3739
files: |
3840
dist.zip

Dockerfile

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
# 此 Dockerfile 仅用于构建可复制的 dist 内容,并不包含运行环境
2-
# 第一阶段构建不区分平台,可通过指定 --platform=linux/amd64 来声明环境
3-
FROM --platform=linux/amd64 docker.cnb.cool/znb/images/node:18 AS builder
4-
5-
WORKDIR /app
6-
7-
ADD . .
8-
9-
RUN sed -i 's@http://localhost:8888/@/@g' .env.production \
10-
&& git config --global url."https://".insteadOf git:// \
11-
&& npm install --registry=http://registry.npmmirror.com \
12-
&& yarn build:prod
13-
141
FROM docker.cnb.cool/znb/images/alpine
152

163
WORKDIR /app
174

18-
COPY --from=builder /app/dist/ dist/
5+
COPY dist ./dist

0 commit comments

Comments
 (0)