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 }}
0 commit comments