-
Notifications
You must be signed in to change notification settings - Fork 1
77 lines (67 loc) · 2.53 KB
/
docker-build-deploy.yml
File metadata and controls
77 lines (67 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
name: Build and Push Docker Image to GHCR
on:
workflow_dispatch: # 手动触发
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
permissions:
contents: read
packages: write
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Get short SHA
id: vars
run: echo "short_sha=${GITHUB_SHA::8}" >> $GITHUB_OUTPUT
- name: Log in to GHCR with GITHUB_TOKEN
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push frontend image
uses: docker/build-push-action@v5
with:
context: ./frontend
file: ./frontend/Dockerfile
push: true
tags: |
ghcr.io/${{ github.repository_owner }}/kb-frontend:latest
ghcr.io/${{ github.repository_owner }}/kb-frontend:${{ github.sha }}
ghcr.io/${{ github.repository_owner }}/kb-frontend:${{ steps.vars.outputs.short_sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
- name: Build and push backend image
uses: docker/build-push-action@v5
with:
context: ./backend
file: ./backend/Dockerfile
push: true
tags: |
ghcr.io/${{ github.repository_owner }}/kb-backend:latest
ghcr.io/${{ github.repository_owner }}/kb-backend:${{ github.sha }}
ghcr.io/${{ github.repository_owner }}/kb-backend:${{ steps.vars.outputs.short_sha }}
build-args: |
# 构建时传递环境变量
DATABASE_URL=${{ secrets.DATABASE_URL || 'sqlite:///./kb_upload.db' }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
- name: Build summary
run: |
echo "✅ 容器构建完成!"
echo ""
echo "前端镜像标签:"
echo " - ghcr.io/${{ github.repository_owner }}/kb-frontend:latest"
echo " - ghcr.io/${{ github.repository_owner }}/kb-frontend:${{ steps.vars.outputs.short_sha }}"
echo ""
echo "后端镜像标签:"
echo " - ghcr.io/${{ github.repository_owner }}/kb-backend:latest"
echo " - ghcr.io/${{ github.repository_owner }}/kb-backend:${{ steps.vars.outputs.short_sha }}"