1- name : Publish Docker Image
2- permissions :
3- contents : read
4- pull-requests : write
1+ name : Docker PR Build
52
63on :
7- workflow_dispatch :
8- release :
9- types : [published]
10- pull_request_target :
11- types : [synchronize, labeled, unlabeled]
4+ pull_request :
5+ types : [synchronize, labeled, unlabeled] # PR 更新或标签变化时触发
126
7+ # 确保同一 PR 同一时间只运行一个相同的 workflow,取消正在进行的旧的运行
138concurrency :
14- group : ${{ github.ref }}-${{ github.workflow }}
15- # PR 构建时取消旧的运行,但 release 构建不取消
16- cancel-in-progress : ${{ github.event_name != 'release' }}
9+ group : pr-${{ github.event.pull_request.number }}-${{ github.workflow }}
10+ cancel-in-progress : true
11+
12+ # Add default permissions
13+ permissions :
14+ contents : read
15+ pull-requests : write
1716
1817env :
1918 REGISTRY_IMAGE : lobehub/lobehub
2019 PR_TAG_PREFIX : pr-
2120
2221jobs :
2322 build :
24- # 添加 PR label 触发条件
25- if : |
26- github.event_name == 'release' ||
27- github.event_name == 'workflow_dispatch' ||
28- (github.event_name == 'pull_request_target' &&
29- contains(github.event.pull_request.labels.*.name, 'trigger:build-docker'))
30-
23+ name : Build ${{ matrix.platform }} Docker Image
24+ # 添加 PR label 触发条件,只有添加了 trigger:build-docker 标签的 PR 才会触发构建
25+ if : contains(github.event.pull_request.labels.*.name, 'trigger:build-docker')
3126 strategy :
3227 matrix :
3328 include :
@@ -36,54 +31,46 @@ jobs:
3631 - platform : linux/arm64
3732 os : ubuntu-24.04-arm
3833 runs-on : ${{ matrix.os }}
39- name : Build ${{ matrix.platform }} Image
4034 steps :
4135 - name : Prepare
4236 run : |
4337 platform=${{ matrix.platform }}
4438 echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
4539
46- - name : Checkout base
40+ - name : Checkout PR branch
4741 uses : actions/checkout@v5
4842 with :
4943 fetch-depth : 0
5044
5145 - name : Set up Docker Buildx
5246 uses : docker/setup-buildx-action@v3
5347
54- # 为 PR 生成特殊的 tag
48+ # 为 PR 生成特殊的 tag,使用 PR 的实际 commit SHA
5549 - name : Generate PR metadata
56- if : github.event_name == 'pull_request_target'
5750 id : pr_meta
5851 env :
5952 BRANCH_NAME : ${{ github.head_ref }}
6053 run : |
6154 sanitized_branch=$(echo "${BRANCH_NAME}" | sed -E 's/[^a-zA-Z0-9_.-]+/-/g')
62- echo "pr_tag=${sanitized_branch}-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
55+ commit_sha=$(git rev-parse --short HEAD)
56+ echo "pr_tag=${sanitized_branch}-${commit_sha}" >> $GITHUB_OUTPUT
57+ echo "commit_sha=${commit_sha}" >> $GITHUB_OUTPUT
58+ echo "📦 Docker Tag: ${sanitized_branch}-${commit_sha}"
6359
6460 - name : Docker meta
6561 id : meta
6662 uses : docker/metadata-action@v5
6763 with :
6864 images : ${{ env.REGISTRY_IMAGE }}
6965 tags : |
70- # PR 构建使用特殊的 tag
71- type=raw,value=${{ env.PR_TAG_PREFIX }}${{ steps.pr_meta.outputs.pr_tag }},enable=${{ github.event_name == 'pull_request_target' }}
72- # release 构建使用版本号
73- type=semver,pattern={{version}},enable=${{ github.event_name != 'pull_request_target' }}
74- type=raw,value=latest,enable=${{ github.event_name != 'pull_request_target' }}
66+ type=raw,value=${{ env.PR_TAG_PREFIX }}${{ steps.pr_meta.outputs.pr_tag }}
7567
7668 - name : Docker login
7769 uses : docker/login-action@v3
7870 with :
7971 username : ${{ secrets.DOCKER_REGISTRY_USER }}
8072 password : ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
8173
82- - name : Get commit SHA
83- if : github.ref == 'refs/heads/main'
84- id : vars
85- run : echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
86-
8774 - name : Build and export
8875 id : build
8976 uses : docker/build-push-action@v6
9380 file : ./Dockerfile
9481 labels : ${{ steps.meta.outputs.labels }}
9582 build-args : |
96- SHA=${{ steps.vars .outputs.sha_short }}
83+ SHA=${{ steps.pr_meta .outputs.commit_sha }}
9784 outputs : type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
9885
9986 - name : Export digest
@@ -112,11 +99,13 @@ jobs:
11299 retention-days : 1
113100
114101 merge :
115- name : Merge
102+ name : Merge and Publish
116103 needs : build
117104 runs-on : ubuntu-latest
105+ # 只为非 fork 的 PR 发布(fork 的 PR 没有写权限)
106+ if : github.event.pull_request.head.repo.full_name == github.repository
118107 steps :
119- - name : Checkout base
108+ - name : Checkout PR branch
120109 uses : actions/checkout@v5
121110 with :
122111 fetch-depth : 0
@@ -133,23 +122,22 @@ jobs:
133122
134123 # 为 merge job 添加 PR metadata 生成
135124 - name : Generate PR metadata
136- if : github.event_name == 'pull_request_target'
137125 id : pr_meta
138126 env :
139127 BRANCH_NAME : ${{ github.head_ref }}
140128 run : |
141129 sanitized_branch=$(echo "${BRANCH_NAME}" | sed -E 's/[^a-zA-Z0-9_.-]+/-/g')
142- echo "pr_tag=${sanitized_branch}-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
130+ commit_sha=$(git rev-parse --short HEAD)
131+ echo "pr_tag=${sanitized_branch}-${commit_sha}" >> $GITHUB_OUTPUT
132+ echo "commit_sha=${commit_sha}" >> $GITHUB_OUTPUT
143133
144134 - name : Docker meta
145135 id : meta
146136 uses : docker/metadata-action@v5
147137 with :
148138 images : ${{ env.REGISTRY_IMAGE }}
149139 tags : |
150- type=raw,value=${{ env.PR_TAG_PREFIX }}${{ steps.pr_meta.outputs.pr_tag }},enable=${{ github.event_name == 'pull_request_target' }}
151- type=semver,pattern={{version}},enable=${{ github.event_name != 'pull_request_target' }}
152- type=raw,value=latest,enable=${{ github.event_name != 'pull_request_target' }}
140+ type=raw,value=${{ env.PR_TAG_PREFIX }}${{ steps.pr_meta.outputs.pr_tag }}
153141
154142 - name : Docker login
155143 uses : docker/login-action@v3
@@ -168,7 +156,6 @@ jobs:
168156 docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
169157
170158 - name : Comment on PR with Docker build info
171- if : github.event_name == 'pull_request_target'
172159 uses : actions/github-script@v8
173160 with :
174161 github-token : ${{ secrets.GITHUB_TOKEN }}
0 commit comments