1
+ name : Build All Docker Images
2
+
3
+ on :
4
+ push :
5
+ branches : [ "main", "master" ]
6
+ pull_request :
7
+ branches : [ "main", "master" ]
8
+ # 允许手动触发工作流
9
+ workflow_dispatch :
10
+
11
+ env :
12
+ REGISTRY : ghcr.io
13
+
14
+ jobs :
15
+ prepare :
16
+ runs-on : ubuntu-latest
17
+ outputs :
18
+ matrix : ${{ steps.set-matrix.outputs.matrix }}
19
+ steps :
20
+ - name : Checkout repository
21
+ uses : actions/checkout@v4
22
+
23
+ - name : Get all dockerfile directories
24
+ id : set-matrix
25
+ run : |
26
+ DIRS=$(find . -name Dockerfile -type f -exec dirname {} \; | sed 's|^\./||' | jq -R -s -c 'split("\n")[:-1]')
27
+ echo "matrix=${DIRS}" >> $GITHUB_OUTPUT
28
+
29
+ build :
30
+ needs : prepare
31
+ runs-on : ubuntu-latest
32
+ strategy :
33
+ matrix :
34
+ dockerfile_dir : ${{ fromJson(needs.prepare.outputs.matrix) }}
35
+ # 如果一个项目构建失败,继续构建其他项目
36
+ fail-fast : false
37
+
38
+ permissions :
39
+ contents : read
40
+ packages : write
41
+
42
+ steps :
43
+ - name : Checkout repository
44
+ uses : actions/checkout@v4
45
+
46
+ - name : Log in to the Container registry
47
+ uses : docker/login-action@v3
48
+ with :
49
+ registry : ${{ env.REGISTRY }}
50
+ username : ${{ github.actor }}
51
+ password : ${{ secrets.GITHUB_TOKEN }}
52
+
53
+ - name : Extract metadata (tags, labels) for Docker
54
+ id : meta
55
+ uses : docker/metadata-action@v5
56
+ with :
57
+ images : ${{ env.REGISTRY }}/${{ github.repository }}/${{ matrix.dockerfile_dir }}
58
+ tags : |
59
+ type=raw,value=latest,enable={{is_default_branch}}
60
+ type=ref,event=branch
61
+ type=ref,event=pr
62
+ type=semver,pattern={{version}}
63
+ type=semver,pattern={{major}}.{{minor}}
64
+ type=sha,format=long
65
+
66
+ - name : Build and push Docker image
67
+ uses : docker/build-push-action@v5
68
+ with :
69
+ context : ${{ matrix.dockerfile_dir }}
70
+ push : ${{ github.event_name != 'pull_request' }}
71
+ tags : ${{ steps.meta.outputs.tags }}
72
+ labels : ${{ steps.meta.outputs.labels }}
0 commit comments