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