name: Docker publish

# Manual image publish from the checked-out tree, for versions whose tag
# predates the Dockerfile (backfill) or to rebuild an image. The release
# workflow publishes images automatically for new tags; this is the escape hatch.
on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to tag the images (e.g. 0.11.0) — must match the crate version in this tree'
        required: true
      move_latest:
        description: 'Also move pgNN and latest to these images (only for the newest release)'
        type: boolean
        default: false

permissions:
  contents: read

env:
  IMAGE: pineconeteam/pg_pinecone

jobs:
  verify:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      # Attestations name the workflow ref that made them; keep that to main or a tag.
      - name: Dispatch only from main or a tag
        if: github.ref != 'refs/heads/main' && !startsWith(github.ref, 'refs/tags/v')
        run: |
          echo "::error::Dispatch this workflow from main or a v* tag, not $GITHUB_REF" >&2
          exit 1
      - name: Version matches the crate
        env:
          VERSION: ${{ inputs.version }}
        run: |
          crate=$(grep -m1 '^version' pg_pinecone/Cargo.toml | cut -d'"' -f2)
          if [ "$VERSION" != "$crate" ]; then
            echo "Requested $VERSION but this tree is crate version $crate" >&2
            exit 1
          fi

  docker:
    needs: verify
    strategy:
      fail-fast: false
      matrix:
        pg: [17, 18]
        arch: [amd64, arm64]
        include:
          - arch: amd64
            runner: ubuntu-latest
          - arch: arm64
            runner: ubuntu-24.04-arm
    runs-on: ${{ matrix.runner }}
    permissions:
      contents: read
      id-token: write
      attestations: write
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
      - uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - id: build
        uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
        with:
          context: .
          build-args: PG_MAJOR=${{ matrix.pg }}
          platforms: linux/${{ matrix.arch }}
          provenance: false
          outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true
      - name: Attest image provenance
        uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2
        with:
          subject-name: index.docker.io/${{ env.IMAGE }}
          subject-digest: ${{ steps.build.outputs.digest }}
          push-to-registry: true
          create-storage-record: false
      - uses: sigstore/cosign-installer@756fe2d284c7292c4e9b62c8a28f1e5629c59532 # v4.1.3
      - name: Sign image
        env:
          DIGEST: ${{ steps.build.outputs.digest }}
        run: cosign sign --yes "${IMAGE}@${DIGEST}"
      - name: Export digest
        run: |
          mkdir -p /tmp/digests
          touch "/tmp/digests/${DIGEST#sha256:}"
        env:
          DIGEST: ${{ steps.build.outputs.digest }}
      - uses: actions/upload-artifact@v7
        with:
          name: digests-pg${{ matrix.pg }}-${{ matrix.arch }}
          path: /tmp/digests/*
          if-no-files-found: error
          retention-days: 1

  docker-manifest:
    needs: docker
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
      attestations: write
    strategy:
      fail-fast: false
      matrix:
        pg: [17, 18]
    steps:
      - uses: actions/download-artifact@v8
        with:
          pattern: digests-pg${{ matrix.pg }}-*
          path: /tmp/digests
          merge-multiple: true
      - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
      - uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Create and push manifest tags
        working-directory: /tmp/digests
        env:
          VERSION: ${{ inputs.version }}
          MOVE_LATEST: ${{ inputs.move_latest }}
        run: |
          tags=(-t "${IMAGE}:${VERSION}-pg${{ matrix.pg }}")
          if [ "$MOVE_LATEST" = true ]; then
            tags+=(-t "${IMAGE}:pg${{ matrix.pg }}")
            [ "${{ matrix.pg }}" = 18 ] && tags+=(-t "${IMAGE}:latest")
          fi
          docker buildx imagetools create "${tags[@]}" \
            $(for d in *; do echo "${IMAGE}@sha256:$d"; done)
          docker buildx imagetools inspect "${IMAGE}:${VERSION}-pg${{ matrix.pg }}"

      # Sign and attest the multi-arch index, the digest users pull.
      - id: digest
        env:
          VERSION: ${{ inputs.version }}
        run: |
          digest=$(docker buildx imagetools inspect "${IMAGE}:${VERSION}-pg${{ matrix.pg }}" --format '{{json .Manifest}}' | jq -r .digest)
          echo "digest=$digest" >> "$GITHUB_OUTPUT"
      - name: Attest image provenance
        uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2
        with:
          subject-name: index.docker.io/${{ env.IMAGE }}
          subject-digest: ${{ steps.digest.outputs.digest }}
          push-to-registry: true
          create-storage-record: false
      - uses: sigstore/cosign-installer@756fe2d284c7292c4e9b62c8a28f1e5629c59532 # v4.1.3
      - name: Sign image
        env:
          DIGEST: ${{ steps.digest.outputs.digest }}
        run: cosign sign --yes "${IMAGE}@${DIGEST}"
