Skip to content

Commit 6ffaf10

Browse files
committed
ci: 添加 Docker 镜像发布工作流并更新构建参数
添加 GitHub Actions 工作流以自动构建和推送 Docker 镜像,同时更新 Dockerfile 和 docker-compose.yml 以支持构建参数的传递。这些更改旨在简化镜像发布流程并确保构建时正确读取环境变量。
1 parent 1e40a0b commit 6ffaf10

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Docker 镜像发布
2+
3+
on:
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
8+
jobs:
9+
build-and-push:
10+
name: 构建并推送Docker镜像
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
packages: write
15+
16+
steps:
17+
- name: 检出代码
18+
uses: actions/checkout@v4
19+
20+
- name: 设置 Docker Buildx
21+
uses: docker/setup-buildx-action@v3
22+
23+
- name: 登录到 GitHub Container Registry
24+
uses: docker/login-action@v3
25+
with:
26+
registry: ghcr.io
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: 提取元数据
31+
id: meta
32+
uses: docker/metadata-action@v5
33+
with:
34+
images: |
35+
ghcr.io/${{ github.repository }}/backend
36+
ghcr.io/${{ github.repository }}/frontend
37+
tags: |
38+
type=semver,pattern={{version}}
39+
type=semver,pattern={{major}}.{{minor}}
40+
type=ref,event=branch
41+
type=sha,format=short
42+
43+
- name: 构建并推送后端镜像
44+
uses: docker/build-push-action@v5
45+
with:
46+
context: ./backend
47+
push: true
48+
tags: ghcr.io/${{ github.repository }}/backend:${{ steps.meta.outputs.version }},ghcr.io/${{ github.repository }}/backend:latest
49+
cache-from: type=gha
50+
cache-to: type=gha,mode=max
51+
52+
- name: 构建并推送前端镜像
53+
uses: docker/build-push-action@v5
54+
with:
55+
context: ./blog-web
56+
push: true
57+
tags: ghcr.io/${{ github.repository }}/frontend:${{ steps.meta.outputs.version }},ghcr.io/${{ github.repository }}/frontend:latest
58+
cache-from: type=gha
59+
cache-to: type=gha,mode=max
60+
build-args: |
61+
PUBLIC_API_BASE_URL=http://localhost:8080/api
62+
INTERNAL_API_BASE_URL=http://backend:8080/api
63+
64+
- name: 更新 Docker Compose 文件
65+
run: |
66+
VERSION=${{ steps.meta.outputs.version }}
67+
sed -i "s|build:\s*context: ./backend|image: ghcr.io/${{ github.repository }}/backend:${VERSION}|g" docker-compose.yml
68+
sed -i "s|build:\s*context: ./blog-web|image: ghcr.io/${{ github.repository }}/frontend:${VERSION}|g" docker-compose.yml
69+
70+
- name: 创建 Docker Compose 发布文件
71+
run: |
72+
cp docker-compose.yml docker-compose.release.yml
73+
74+
- name: 上传 Docker Compose 文件作为发布资产
75+
uses: softprops/action-gh-release@v1
76+
with:
77+
files: docker-compose.release.yml
78+
token: ${{ secrets.GITHUB_TOKEN }}

blog-web/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ FROM node:20-slim
4949

5050
WORKDIR /app
5151

52+
# 声明构建参数,接受 docker-compose 传入的 args
53+
ARG PUBLIC_API_BASE_URL
54+
ARG INTERNAL_API_BASE_URL
55+
56+
# 设置环境变量,供构建时读取
57+
ENV PUBLIC_API_BASE_URL=$PUBLIC_API_BASE_URL
58+
ENV INTERNAL_API_BASE_URL=$INTERNAL_API_BASE_URL
59+
5260
RUN npm install -g pnpm
5361
# Copy built application from the builder stage
5462
COPY --from=builder /app/dist /app/dist

docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ services:
3333
frontend:
3434
build:
3535
context: ./blog-web
36+
args:
37+
PUBLIC_API_BASE_URL: "http://localhost:8080/api"
38+
INTERNAL_API_BASE_URL: "http://backend:8080/api"
3639
container_name: blog-frontend
3740
environment:
3841
- HOST=0.0.0.0
3942
- PORT=4321
40-
- PUBLIC_API_BASE_URL=http://localhost:8080/api
41-
- INTERNAL_API_BASE_URL=http://backend:8080/api
4243
ports:
4344
- "4321:4321"
4445
depends_on:

0 commit comments

Comments
 (0)