1
- name : Build All Docker Images
1
+ name : Build Docker Images
2
2
3
3
on :
4
4
push :
5
5
branches : [ "main", "master" ]
6
+ paths :
7
+ - ' **/Dockerfile'
8
+ - ' **/*.dockerfile'
9
+ - ' .github/workflows/docker-build-all.yml'
6
10
pull_request :
7
11
branches : [ "main", "master" ]
12
+ paths :
13
+ - ' **/Dockerfile'
14
+ - ' **/*.dockerfile'
15
+ - ' .github/workflows/docker-build-all.yml'
8
16
# 允许手动触发工作流
9
17
workflow_dispatch :
18
+ inputs :
19
+ specific_dir :
20
+ description : ' 指定构建目录(可选,留空构建所有变更的目录)'
21
+ required : false
22
+ type : string
10
23
11
24
env :
12
25
REGISTRY : ghcr.io
@@ -19,15 +32,45 @@ jobs:
19
32
steps :
20
33
- name : Checkout repository
21
34
uses : actions/checkout@v4
35
+ with :
36
+ fetch-depth : 2
22
37
23
- - name : Get all dockerfile directories
38
+ - name : Get changed dockerfile directories
24
39
id : set-matrix
25
40
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
41
+ if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.specific_dir }}" ]; then
42
+ # 如果是手动触发并指定了目录,只构建指定目录
43
+ if [ -f "${{ github.event.inputs.specific_dir }}/Dockerfile" ]; then
44
+ echo "matrix=[\"${{ github.event.inputs.specific_dir }}\"]" >> $GITHUB_OUTPUT
45
+ else
46
+ echo "Error: Specified directory does not contain a Dockerfile"
47
+ exit 1
48
+ fi
49
+ else
50
+ # 获取变更的文件列表
51
+ if [ "${{ github.event_name }}" = "pull_request" ]; then
52
+ git fetch origin ${{ github.base_ref }}
53
+ CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
54
+ elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
55
+ # 手动触发且未指定目录时,获取所有 Dockerfile
56
+ CHANGED_FILES=$(find . -name Dockerfile)
57
+ else
58
+ CHANGED_FILES=$(git diff --name-only HEAD^ HEAD)
59
+ fi
60
+
61
+ # 提取包含 Dockerfile 的目录
62
+ DIRS=$(echo "$CHANGED_FILES" | grep -E "Dockerfile$" | xargs -I {} dirname {} | sed 's|^\./||' | sort -u | jq -R -s -c 'split("\n")[:-1]')
63
+ if [ "$DIRS" = "[]" ] || [ -z "$DIRS" ]; then
64
+ echo "No Dockerfile changes detected"
65
+ echo "matrix=[]" >> $GITHUB_OUTPUT
66
+ else
67
+ echo "matrix=$DIRS" >> $GITHUB_OUTPUT
68
+ fi
69
+ fi
28
70
29
71
build :
30
72
needs : prepare
73
+ if : fromJson(needs.prepare.outputs.matrix)[0] != null
31
74
runs-on : ubuntu-latest
32
75
strategy :
33
76
matrix :
0 commit comments