Skip to content

Commit 950bce3

Browse files
committed
add a label trigger
1 parent 4536b87 commit 950bce3

File tree

1 file changed

+37
-70
lines changed

1 file changed

+37
-70
lines changed

.github/workflows/docker.yml

Lines changed: 37 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,89 @@ on:
44
workflow_dispatch:
55
release:
66
types: [published]
7-
8-
concurrency:
9-
group: ${{ github.ref }}-${{ github.workflow }}
10-
cancel-in-progress: true
7+
pull_request:
8+
types: [synchronize, labeled, unlabeled]
119

1210
env:
1311
REGISTRY_IMAGE: lobehub/lobe-chat
12+
PR_TAG_PREFIX: pr-
1413

1514
jobs:
1615
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+
1721
strategy:
1822
matrix:
1923
include:
2024
- platform: linux/amd64
2125
os: ubuntu-latest
2226
- platform: linux/arm64
2327
os: ubuntu-24.04-arm
28+
2429
runs-on: ${{ matrix.os }}
25-
name: Build ${{ matrix.platform }} Image
2630
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
3332
uses: actions/checkout@v4
34-
with:
35-
fetch-depth: 0
3633

37-
- name: Set up Docker Buildx
34+
- name: Setup buildx
3835
uses: docker/setup-buildx-action@v3
3936

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+
4045
- name: Docker meta
4146
id: meta
4247
uses: docker/metadata-action@v5
4348
with:
4449
images: ${{ env.REGISTRY_IMAGE }}
4550
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' }}
5454
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
6256
uses: docker/build-push-action@v5
6357
with:
6458
platforms: ${{ matrix.platform }}
6559
context: .
6660
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
7163

72-
- name: Export digest
64+
- name: Export manifest digest
7365
run: |
74-
rm -rf /tmp/digests
7566
mkdir -p /tmp/digests
7667
digest="${{ steps.build.outputs.digest }}"
77-
touch "/tmp/digests/${digest#sha256:}"
68+
echo "${digest#sha256:}" >> /tmp/digests/manifest.txt
7869
79-
- name: Upload artifact
70+
- name: Upload manifest digest
8071
uses: actions/upload-artifact@v4
8172
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
8675

8776
merge:
88-
name: Merge
77+
if: always() # 确保 PR 构建也执行合并
8978
needs: build
9079
runs-on: ubuntu-latest
9180
steps:
9281
- name: Download digests
9382
uses: actions/download-artifact@v4
9483
with:
9584
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
10186

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
12488
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

Comments
 (0)