4
4
workflow_dispatch :
5
5
release :
6
6
types : [published]
7
-
8
- concurrency :
9
- group : ${{ github.ref }}-${{ github.workflow }}
10
- cancel-in-progress : true
7
+ pull_request :
8
+ types : [synchronize, labeled, unlabeled]
11
9
12
10
env :
13
11
REGISTRY_IMAGE : lobehub/lobe-chat
12
+ PR_TAG_PREFIX : pr-
14
13
15
14
jobs :
16
15
build :
16
+ if : |
17
+ (github.event_name == 'pull_request' &&
18
+ contains(github.event.pull_request.labels.*.name, 'Build Docker')) ||
19
+ github.event_name != 'pull_request'
20
+
17
21
strategy :
18
22
matrix :
19
23
include :
20
24
- platform : linux/amd64
21
25
os : ubuntu-latest
22
26
- platform : linux/arm64
23
27
os : ubuntu-24.04-arm
28
+
24
29
runs-on : ${{ matrix.os }}
25
- name : Build ${{ matrix.platform }} Image
26
30
steps :
27
- - name : Prepare
28
- run : |
29
- platform=${{ matrix.platform }}
30
- echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
31
-
32
- - name : Checkout base
31
+ - name : Checkout code
33
32
uses : actions/checkout@v4
34
- with :
35
- fetch-depth : 0
36
33
37
- - name : Set up Docker Buildx
34
+ - name : Setup buildx
38
35
uses : docker/setup-buildx-action@v3
39
36
37
+ - name : Generate PR metadata
38
+ if : github.event_name == 'pull_request'
39
+ id : pr_meta
40
+ run : |
41
+ branch_name="${{ github.head_ref }}"
42
+ sanitized_branch=$(echo "${branch_name}" | sed -E 's/[^a-zA-Z0-9_.-]+/-/g')
43
+ echo "pr_tag=${sanitized_branch}-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
44
+
40
45
- name : Docker meta
41
46
id : meta
42
47
uses : docker/metadata-action@v5
43
48
with :
44
49
images : ${{ env.REGISTRY_IMAGE }}
45
50
tags : |
46
- type=semver,pattern={{version}}
47
- type=raw,value=latest,enable={{is_default_branch}}
48
-
49
- - name : Docker login
50
- uses : docker/login-action@v3
51
- with :
52
- username : ${{ secrets.DOCKER_REGISTRY_USER }}
53
- password : ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
51
+ type=raw,value=${{ env.PR_TAG_PREFIX }}${{ steps.pr_meta.outputs.pr_tag }},enable=${{ github.event_name == 'pull_request' }}
52
+ type=semver,pattern={{version}},enable=${{ github.event_name != 'pull_request' }}
53
+ type=raw,value=latest,enable=${{ github.event_name != 'pull_request' }}
54
54
55
- - name : Get commit SHA
56
- if : github.ref == 'refs/heads/master'
57
- id : vars
58
- run : echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
59
-
60
- - name : Build and export
61
- id : build
55
+ - name : Build and push
62
56
uses : docker/build-push-action@v5
63
57
with :
64
58
platforms : ${{ matrix.platform }}
65
59
context : .
66
60
file : ./Dockerfile
67
- labels : ${{ steps.meta.outputs.labels }}
68
- build-args : |
69
- SHA=${{ steps.vars.outputs.sha_short }}
70
- outputs : type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
61
+ tags : ${{ steps.meta.outputs.tags }}
62
+ push : true
71
63
72
- - name : Export digest
64
+ - name : Export manifest digest
73
65
run : |
74
- rm -rf /tmp/digests
75
66
mkdir -p /tmp/digests
76
67
digest="${{ steps.build.outputs.digest }}"
77
- touch "/tmp/digests/ ${digest#sha256:}"
68
+ echo " ${digest#sha256:}" >> /tmp/digests/manifest.txt
78
69
79
- - name : Upload artifact
70
+ - name : Upload manifest digest
80
71
uses : actions/upload-artifact@v4
81
72
with :
82
- name : digest-${{ env.PLATFORM_PAIR }}
83
- path : /tmp/digests/*
84
- if-no-files-found : error
85
- retention-days : 1
73
+ name : platform-digests
74
+ path : /tmp/digests/manifest.txt
86
75
87
76
merge :
88
- name : Merge
77
+ if : always() # 确保 PR 构建也执行合并
89
78
needs : build
90
79
runs-on : ubuntu-latest
91
80
steps :
92
81
- name : Download digests
93
82
uses : actions/download-artifact@v4
94
83
with :
95
84
path : /tmp/digests
96
- pattern : digest-*
97
- merge-multiple : true
98
-
99
- - name : Set up Docker Buildx
100
- uses : docker/setup-buildx-action@v3
85
+ pattern : platform-digests
101
86
102
- - name : Docker meta
103
- id : meta
104
- uses : docker/metadata-action@v5
105
- with :
106
- images : ${{ env.REGISTRY_IMAGE }}
107
- tags : |
108
- type=semver,pattern={{version}}
109
- type=raw,value=latest,enable={{is_default_branch}}
110
-
111
- - name : Docker login
112
- uses : docker/login-action@v3
113
- with :
114
- username : ${{ secrets.DOCKER_REGISTRY_USER }}
115
- password : ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
116
-
117
- - name : Create manifest list and push
118
- working-directory : /tmp/digests
119
- run : |
120
- docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
121
- $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
122
-
123
- - name : Inspect image
87
+ - name : Merge manifests
124
88
run : |
125
- docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
89
+ DIGESTS=$(cat /tmp/digests/**/manifest.txt | tr '\n' ' ')
90
+ docker buildx imagetools create \
91
+ -t ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.tags }} \
92
+ $(echo "$DIGESTS" | sed "s| | ${{ env.REGISTRY_IMAGE }}@sha256:|g")
0 commit comments