diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index b9e96e3..ff73ba2 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -1,27 +1,70 @@ -name: Build and Publish Docker Image +name: Create and publish Docker images on: push: tags: - 'v*.*.*' + workflow_dispatch: + inputs: + use_latest_tag: + description: 'Tag as latest instead of branch name' + required: false + type: boolean + default: false env: REGISTRY: ghcr.io - IMAGE_NAME: richardr1126/openreader-webui + +permissions: + contents: read jobs: + prepare: + runs-on: ubuntu-24.04 + outputs: + image_name: ${{ steps.image-name.outputs.image_name }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + steps: + - name: Compute lowercase image name + id: image-name + run: echo "image_name=${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT" + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ steps.image-name.outputs.image_name }} + tags: | + type=raw,value=latest,enable=${{ (github.event_name == 'push' && !contains(github.ref, '-pre')) || (github.event_name == 'workflow_dispatch' && inputs.use_latest_tag == true) }},priority=1000 + type=semver,pattern={{version}} + type=ref,event=tag + type=ref,event=branch,enable=${{ github.event_name == 'workflow_dispatch' && inputs.use_latest_tag != true }} + build: - runs-on: ubuntu-latest + needs: prepare + runs-on: ${{ matrix.runner }} permissions: + actions: write contents: read packages: write + strategy: + fail-fast: false + matrix: + include: + - arch: amd64 + platform: linux/amd64 + runner: ubuntu-24.04 + - arch: arm64 + platform: linux/arm64 + runner: ubuntu-24.04-arm steps: - name: Checkout repository uses: actions/checkout@v4 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 + with: + ref: ${{ github.sha }} + fetch-depth: 0 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -33,22 +76,76 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract metadata for Docker - id: meta - uses: docker/metadata-action@v5 - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - tags: | - type=raw,value=latest,enable=${{ !contains(github.ref, '-pre') }} - type=semver,pattern={{version}} - - - name: Build and push Docker image + - name: Build and push Docker image (${{ matrix.arch }}) + id: build uses: docker/build-push-action@v5 with: context: . - platforms: linux/amd64,linux/arm64 - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max \ No newline at end of file + platforms: ${{ matrix.platform }} + labels: ${{ needs.prepare.outputs.labels }} + cache-from: type=gha,scope=${{ matrix.arch }} + cache-to: type=gha,mode=max,scope=${{ matrix.arch }} + provenance: false + outputs: type=image,name=${{ env.REGISTRY }}/${{ needs.prepare.outputs.image_name }},push-by-digest=true,name-canonical=true,push=true + + - name: Export digest + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digest + uses: actions/upload-artifact@v4 + with: + name: digests-${{ matrix.arch }} + path: /tmp/digests + if-no-files-found: error + retention-days: 1 + + merge: + needs: [prepare, build] + runs-on: ubuntu-24.04 + permissions: + actions: read + contents: read + packages: write + + steps: + - name: Download digests + uses: actions/download-artifact@v4 + with: + path: /tmp/digests + pattern: digests-* + merge-multiple: true + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create \ + $(echo "$TAGS" | xargs -I {} echo -t {}) \ + $(printf '${{ env.REGISTRY }}/${{ needs.prepare.outputs.image_name }}@sha256:%s ' *) + env: + TAGS: ${{ needs.prepare.outputs.tags }} + + - name: Output build information + run: | + echo "✅ Docker images built and pushed successfully!" + echo "🐋 Images:" + echo '${{ needs.prepare.outputs.tags }}' | sed 's/^/ - /' + echo "📝 Event: ${{ github.event_name }}" + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then + echo "📝 Triggered by manual workflow dispatch on branch: ${{ github.ref_name }}" + echo "📝 use_latest_tag: ${{ inputs.use_latest_tag }}" + else + echo "📝 Triggered by tag: ${{ github.ref_name }}" + fi