# Build and publish Docker images.
#
# Uses Depot for multi-platform builds. Includes both a `uv` base image, which
# is just the binary in a scratch image, and a set of extra, common images with
# the uv binary installed.
#
# On pull requests, triggered via CI workflow when Docker-related files change
# (e.g., Dockerfile, Cargo.toml, rust-toolchain.toml). Images are built but not
# pushed, to verify the build still works.
#
# On release, assumed to run as a subworkflow of .github/workflows/release.yml;
# specifically, as a local artifacts job within `cargo-dist`. In this case,
# images are published based on the `plan`.
#
# TODO(charlie): Ideally, the publish step would happen as a publish job within
# `cargo-dist`, but sharing the built image as an artifact between jobs is
# challenging.
name: "Docker images"

on:
  workflow_call:
    inputs:
      plan:
        required: false
        type: string
        default: ""
      push-dev:
        required: false
        type: boolean
        default: false

env:
  UV_GHCR_IMAGE: ghcr.io/${{ github.repository_owner }}/${{ inputs.push-dev && 'uv-dev' || 'uv' }}
  UV_DOCKERHUB_IMAGE: ${{ !inputs.push-dev && 'docker.io/astral/uv' || '' }}

permissions: {}

jobs:
  docker-plan:
    name: plan
    runs-on: ubuntu-latest
    timeout-minutes: 2
    outputs:
      login: ${{ steps.plan.outputs.login }}
      push: ${{ steps.plan.outputs.push }}
      push-version: ${{ steps.plan.outputs.push-version }}
      tag: ${{ steps.plan.outputs.tag }}
      action: ${{ steps.plan.outputs.action }}
      extra-images: ${{ steps.extra-images.outputs.matrix }}
    steps:
      - name: Set push variable
        env:
          DRY_RUN: ${{ inputs.plan == '' || fromJson(inputs.plan).announcement_tag_is_implicit }}
          TAG: ${{ inputs.plan != '' && fromJson(inputs.plan).announcement_tag }}
          PUSH_DEV: ${{ inputs.push-dev }}
          IS_LOCAL_PR: ${{ github.event.pull_request.head.repo.full_name == 'astral-sh/uv' }}
        id: plan
        run: |
          if [ "${PUSH_DEV}" == "true" ] && [ "${IS_LOCAL_PR}" == "true" ]; then
            echo "login=true" >> "$GITHUB_OUTPUT"
            echo "push=true" >> "$GITHUB_OUTPUT"
            echo "push-version=false" >> "$GITHUB_OUTPUT"
            echo "tag=sha" >> "$GITHUB_OUTPUT"
            echo "action=build and publish to uv-dev" >> "$GITHUB_OUTPUT"
          elif [ "${DRY_RUN}" == "false" ]; then
            echo "login=true" >> "$GITHUB_OUTPUT"
            echo "push=true" >> "$GITHUB_OUTPUT"
            echo "push-version=true" >> "$GITHUB_OUTPUT"
            echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
            echo "action=build and publish" >> "$GITHUB_OUTPUT"
          else
            echo "login=${IS_LOCAL_PR}" >> "$GITHUB_OUTPUT"
            echo "push=false" >> "$GITHUB_OUTPUT"
            echo "push-version=false" >> "$GITHUB_OUTPUT"
            echo "tag=dry-run" >> "$GITHUB_OUTPUT"
            echo "action=build" >> "$GITHUB_OUTPUT"
          fi

      - name: Generate extra image matrix
        id: extra-images
        env:
          LOGIN: ${{ steps.plan.outputs.login }}
        run: |
          # Each entry is: base-image,tag1,tag2,...
          # DHI (Docker Hardened Images) require authentication to pull, so they
          # are excluded when login is unavailable (e.g., PRs from forks).
          images=(
            "alpine:3.23,alpine3.23,alpine"
            "alpine:3.22,alpine3.22"
            "debian:trixie-slim,trixie-slim,debian-slim"
            "buildpack-deps:trixie,trixie,debian"
            "python:3.14-alpine3.23,python3.14-alpine3.23,python3.14-alpine"
            "python:3.13-alpine3.23,python3.13-alpine3.23,python3.13-alpine"
            "python:3.12-alpine3.23,python3.12-alpine3.23,python3.12-alpine"
            "python:3.11-alpine3.23,python3.11-alpine3.23,python3.11-alpine"
            "python:3.10-alpine3.23,python3.10-alpine3.23,python3.10-alpine"
            "python:3.9-alpine3.22,python3.9-alpine3.22,python3.9-alpine"
            "python:3.14-trixie,python3.14-trixie"
            "python:3.13-trixie,python3.13-trixie"
            "python:3.12-trixie,python3.12-trixie"
            "python:3.11-trixie,python3.11-trixie"
            "python:3.10-trixie,python3.10-trixie"
            "python:3.9-trixie,python3.9-trixie"
            "python:3.14-slim-trixie,python3.14-trixie-slim"
            "python:3.13-slim-trixie,python3.13-trixie-slim"
            "python:3.12-slim-trixie,python3.12-trixie-slim"
            "python:3.11-slim-trixie,python3.11-trixie-slim"
            "python:3.10-slim-trixie,python3.10-trixie-slim"
            "python:3.9-slim-trixie,python3.9-trixie-slim"
          )

          if [ "${LOGIN}" == "true" ]; then
            images+=(
              "dhi.io/alpine-base:3.23,alpine3.23-dhi,alpine-dhi"
              "dhi.io/debian-base:trixie-debian13,trixie-dhi,debian-dhi"
              "dhi.io/python:3.14,python3.14-dhi"
              "dhi.io/python:3.13,python3.13-dhi"
              "dhi.io/python:3.12,python3.12-dhi"
              "dhi.io/python:3.11,python3.11-dhi"
              "dhi.io/python:3.10,python3.10-dhi"
            )
          fi

          json=$(printf '%s\n' "${images[@]}" | jq -R . | jq -sc '{"image-mapping": .}')
          echo "matrix=${json}" >> "$GITHUB_OUTPUT"

  docker-publish-base:
    name: ${{ needs.docker-plan.outputs.action }} uv
    needs:
      - docker-plan
    runs-on: ubuntu-latest
    timeout-minutes: 20
    permissions:
      contents: read
      id-token: write # for Depot OIDC and GHCR signing
      packages: write # for GHCR image pushes
      attestations: write # for GHCR attestations
    environment:
      name: ${{ needs.docker-plan.outputs.push-version == 'true' && 'release' || (needs.docker-plan.outputs.push == 'true' && 'release-test' || '') }}
      deployment: ${{ needs.docker-plan.outputs.push-version == 'true' }}
    outputs:
      image-tags: ${{ steps.meta.outputs.tags }}
      image-annotations: ${{ steps.meta.outputs.annotations }}
      image-digest: ${{ steps.build.outputs.digest }}
      image-version: ${{ steps.meta.outputs.version }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          submodules: recursive
          persist-credentials: false

      # Login to DockerHub (when not pushing, it's to avoid rate-limiting)
      - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        if: ${{ needs.docker-plan.outputs.login == 'true' }}
        with:
          username: ${{ needs.docker-plan.outputs.push == 'true' && 'astral' || 'astralshbot' }}
          password: ${{ needs.docker-plan.outputs.push == 'true' && secrets.DOCKERHUB_TOKEN_RW || secrets.DOCKERHUB_TOKEN_RO }}

      - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        if: ${{ needs.docker-plan.outputs.push == 'true' }}
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - uses: depot/setup-action@15c09a5f77a0840ad4bce955686522a257853461 # v1.7.1

      - name: Check tag consistency
        if: ${{ needs.docker-plan.outputs.push == 'true' }}
        run: |
          if [ "${PUSH_DEV}" == "true" ]; then
            echo "Building at $(git rev-parse HEAD)"
            exit 0
          fi

          version=$(grep "version = " pyproject.toml | sed -e 's/version = "\(.*\)"/\1/g')
          if [ "${TAG}" != "${version}" ]; then
            echo "The input tag does not match the version from pyproject.toml:" >&2
            echo "${TAG}" >&2
            echo "${version}" >&2
            exit 1
          fi
          echo "Releasing ${version}"
        env:
          TAG: ${{ needs.docker-plan.outputs.tag }}
          PUSH_DEV: ${{ inputs.push-dev }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
        env:
          DOCKER_METADATA_ANNOTATIONS_LEVELS: index
        with:
          images: |
            ${{ env.UV_GHCR_IMAGE }}
            ${{ env.UV_DOCKERHUB_IMAGE }}
          # Defining this makes sure the org.opencontainers.image.version OCI label becomes the actual release version and not the branch name
          tags: |
            type=raw,value=dry-run,enable=${{ needs.docker-plan.outputs.push == 'false' }}
            type=pep440,pattern={{ version }},value=${{ needs.docker-plan.outputs.tag }},enable=${{ needs.docker-plan.outputs.push-version == 'true' }}
            type=pep440,pattern={{ major }}.{{ minor }},value=${{ needs.docker-plan.outputs.tag }},enable=${{ needs.docker-plan.outputs.push-version == 'true' }}
            type=sha,enable=${{ inputs.push-dev }}

      - name: Build and push by digest
        id: build
        uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1.17.0
        with:
          project: 7hd4vdzmw5 # astral-sh/uv
          context: .
          platforms: linux/amd64,linux/arm64
          push: ${{ needs.docker-plan.outputs.push }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          provenance: mode=max
          sbom: true
          # TODO(zanieb): Annotations are not supported by Depot yet and are ignored
          annotations: ${{ steps.meta.outputs.annotations }}

      - name: Generate artifact attestation for base image
        if: ${{ needs.docker-plan.outputs.push == 'true' }}
        uses: actions/attest-build-provenance@a2bbfa25375fe432b6a289bc6b6cd05ecd0c4c32 # v4.1.0
        with:
          subject-name: ${{ env.UV_GHCR_IMAGE }}
          subject-digest: ${{ steps.build.outputs.digest }}

  docker-publish-extra:
    name: ${{ needs.docker-plan.outputs.action }} ${{ matrix.image-mapping }}
    runs-on: ubuntu-latest
    timeout-minutes: 5
    environment:
      name: ${{ needs.docker-plan.outputs.push-version == 'true' && 'release' || (needs.docker-plan.outputs.push == 'true' && 'release-test' || '') }}
      deployment: ${{ needs.docker-plan.outputs.push-version == 'true' }}
    needs:
      - docker-plan
      - docker-publish-base
    permissions:
      id-token: write # for Depot OIDC and GHCR signing
      packages: write # for GHCR image pushes
      attestations: write # for GHCR attestations
    strategy:
      fail-fast: false
      matrix: ${{ fromJson(needs.docker-plan.outputs.extra-images) }}
    steps:
      # Login to DockerHub (when not pushing, it's to avoid rate-limiting)
      - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        if: ${{ needs.docker-plan.outputs.login == 'true' }}
        with:
          username: ${{ needs.docker-plan.outputs.push == 'true' && 'astral' || 'astralshbot' }}
          password: ${{ needs.docker-plan.outputs.push == 'true' && secrets.DOCKERHUB_TOKEN_RW || secrets.DOCKERHUB_TOKEN_RO }}

      - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        if: ${{ needs.docker-plan.outputs.login == 'true' }}
        with:
          registry: dhi.io
          username: astralshbot
          password: ${{ secrets.DOCKERHUB_TOKEN_RO }}

      - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - uses: depot/setup-action@15c09a5f77a0840ad4bce955686522a257853461 # v1.7.1

      - name: Generate Dynamic Dockerfile Tags
        shell: bash
        run: |
          set -euo pipefail

          # Extract the image and tags from the matrix variable
          IFS=',' read -r BASE_IMAGE BASE_TAGS <<< "${IMAGE_MAPPING}"

          # Generate Dockerfile content
          cat <<EOF > Dockerfile
          FROM ${BASE_IMAGE}
          COPY --from=${UV_GHCR_IMAGE}:${UV_BASE_TAG} /uv /uvx /usr/local/bin/
          ENV UV_TOOL_BIN_DIR="/usr/local/bin"
          ENTRYPOINT []
          CMD ["/usr/local/bin/uv"]
          EOF

          # Initialize a variable to store all tag docker metadata patterns
          TAG_PATTERNS=""

          # Loop through all base tags and append its docker metadata pattern to the list
          # Order is on purpose such that the label org.opencontainers.image.version has the first pattern with the full version
          IFS=','; for TAG in ${BASE_TAGS}; do
            if [ "${PUSH_DEV}" == "true" ]; then
              TAG_PATTERNS="${TAG_PATTERNS}type=sha,suffix=-${TAG}\n"
            else
              TAG_PATTERNS="${TAG_PATTERNS}type=pep440,pattern={{ version }},suffix=-${TAG},value=${VERSION}\n"
              TAG_PATTERNS="${TAG_PATTERNS}type=pep440,pattern={{ major }}.{{ minor }},suffix=-${TAG},value=${VERSION}\n"
              TAG_PATTERNS="${TAG_PATTERNS}type=raw,value=${TAG}\n"
            fi
          done

          # Remove the trailing newline from the pattern list
          TAG_PATTERNS="${TAG_PATTERNS%\\n}"

          # Export tag patterns using the multiline env var syntax
          {
            echo "TAG_PATTERNS<<EOF"
            echo -e "${TAG_PATTERNS}"
            echo EOF
          } >> $GITHUB_ENV
        env:
          VERSION: ${{ needs.docker-plan.outputs.tag }}
          PUSH_DEV: ${{ inputs.push-dev }}
          # Use the tag from the base image we just pushed; fall back to `latest` on dry-runs
          # since the base image isn't pushed to the registry.
          UV_BASE_TAG: ${{ needs.docker-plan.outputs.push == 'true' && needs.docker-publish-base.outputs.image-version || 'latest' }}
          IMAGE_MAPPING: ${{ matrix.image-mapping }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
        # ghcr.io prefers index level annotations
        env:
          DOCKER_METADATA_ANNOTATIONS_LEVELS: index
        with:
          images: |
            ${{ env.UV_GHCR_IMAGE }}
            ${{ env.UV_DOCKERHUB_IMAGE }}
          flavor: |
            latest=false
          tags: |
            ${{ env.TAG_PATTERNS }}

      - name: Build and push
        id: build-and-push
        uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1.17.0
        with:
          context: .
          project: 7hd4vdzmw5 # astral-sh/uv
          platforms: linux/amd64,linux/arm64
          push: ${{ needs.docker-plan.outputs.push }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          provenance: mode=max
          sbom: true
          # TODO(zanieb): Annotations are not supported by Depot yet and are ignored
          annotations: ${{ steps.meta.outputs.annotations }}

      - name: Generate artifact attestation
        if: ${{ needs.docker-plan.outputs.push == 'true' }}
        uses: actions/attest-build-provenance@a2bbfa25375fe432b6a289bc6b6cd05ecd0c4c32 # v4.1.0
        with:
          subject-name: ${{ env.UV_GHCR_IMAGE }}
          subject-digest: ${{ steps.build-and-push.outputs.digest }}

      # Push annotations manually.
      # See `docker-annotate-base` for details.
      - name: Add annotations to images
        if: ${{ needs.docker-plan.outputs.push == 'true' }}
        env:
          IMAGES: "${{ env.UV_GHCR_IMAGE }} ${{ env.UV_DOCKERHUB_IMAGE }}"
          DIGEST: ${{ steps.build-and-push.outputs.digest }}
          TAGS: ${{ steps.meta.outputs.tags }}
          ANNOTATIONS: ${{ steps.meta.outputs.annotations }}
        run: |
          set -x
          readarray -t lines <<< "$ANNOTATIONS"; annotations=(); for line in "${lines[@]}"; do annotations+=(--annotation "$line"); done
          for image in $IMAGES; do
            readarray -t lines < <(grep "^${image}:" <<< "$TAGS"); tags=(); for line in "${lines[@]}"; do tags+=(-t "$line"); done
            docker buildx imagetools create \
              "${annotations[@]}" \
              "${tags[@]}" \
              "${image}@${DIGEST}"
          done

      # See `docker-annotate-base` for details.
      - name: Export manifest digest
        id: manifest-digest
        if: ${{ needs.docker-plan.outputs.push == 'true' }}
        env:
          IMAGE: ${{ env.UV_GHCR_IMAGE }}
          VERSION: ${{ steps.meta.outputs.version }}
        run: |
          digest="$(
            docker buildx imagetools inspect \
              "${IMAGE}:${VERSION}" \
              --format '{{json .Manifest}}' \
            | jq -r '.digest'
          )"
          echo "digest=${digest}" >> "$GITHUB_OUTPUT"

      # See `docker-annotate-base` for details.
      - name: Generate artifact attestation
        if: ${{ needs.docker-plan.outputs.push == 'true' }}
        uses: actions/attest-build-provenance@a2bbfa25375fe432b6a289bc6b6cd05ecd0c4c32 # v4.1.0
        with:
          subject-name: ${{ env.UV_GHCR_IMAGE }}
          subject-digest: ${{ steps.manifest-digest.outputs.digest }}

  # Annotate the base image
  docker-annotate-base:
    name: annotate uv
    runs-on: ubuntu-latest
    timeout-minutes: 2
    permissions:
      contents: read
      id-token: write # for GHCR signing
      packages: write # for GHCR image pushes
      attestations: write # for GHCR attestations
    environment:
      name: ${{ needs.docker-plan.outputs.push-version == 'true' && 'release' || (needs.docker-plan.outputs.push == 'true' && 'release-test' || '') }}
      deployment: ${{ needs.docker-plan.outputs.push-version == 'true' }}
    needs:
      - docker-plan
      - docker-publish-base
      - docker-publish-extra
    if: ${{ needs.docker-plan.outputs.push == 'true' }}
    steps:
      - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        with:
          username: astral
          password: ${{ secrets.DOCKERHUB_TOKEN_RW }}

      - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      # Depot doesn't support annotating images, so we need to do so manually
      # afterwards. Mutating the manifest is desirable regardless, because we
      # want to bump the base image to appear at the top of the list on GHCR.
      # However, once annotation support is added to Depot, this step can be
      # minimized to just touch the GHCR manifest.
      - name: Add annotations to images
        env:
          IMAGES: "${{ env.UV_GHCR_IMAGE }} ${{ env.UV_DOCKERHUB_IMAGE }}"
          DIGEST: ${{ needs.docker-publish-base.outputs.image-digest }}
          TAGS: ${{ needs.docker-publish-base.outputs.image-tags }}
          ANNOTATIONS: ${{ needs.docker-publish-base.outputs.image-annotations }}
        # The readarray part is used to make sure the quoting and special characters are preserved on expansion (e.g. spaces)
        # The final command becomes `docker buildx imagetools create --annotation 'index:foo=1' --annotation 'index:bar=2' ... -t tag1 -t tag2 ... <IMG>@sha256:<sha256>`
        run: |
          set -x
          readarray -t lines <<< "$ANNOTATIONS"; annotations=(); for line in "${lines[@]}"; do annotations+=(--annotation "$line"); done
          for image in $IMAGES; do
            readarray -t lines < <(grep "^${image}:" <<< "$TAGS"); tags=(); for line in "${lines[@]}"; do tags+=(-t "$line"); done
            docker buildx imagetools create \
              "${annotations[@]}" \
              "${tags[@]}" \
              "${image}@${DIGEST}"
          done

      # Now that we've modified the manifest, we need to attest it again.
      # Note we only generate an attestation for GHCR.
      - name: Export manifest digest
        id: manifest-digest
        env:
          IMAGE: ${{ env.UV_GHCR_IMAGE }}
          VERSION: ${{ needs.docker-publish-base.outputs.image-version }}
        # To sign the manifest, we need it's digest. Unfortunately "docker
        # buildx imagetools create" does not (yet) have a clean way of sharing
        # the digest of the manifest it creates (see docker/buildx#2407), so
        # we use a separate command to retrieve it.
        # imagetools inspect [TAG] --format '{{json .Manifest}}' gives us
        # the machine readable JSON description of the manifest, and the
        # jq command extracts the digest from this. The digest is then
        # sent to the Github step output file for sharing with other steps.
        run: |
          digest="$(
            docker buildx imagetools inspect \
              "${IMAGE}:${VERSION}" \
              --format '{{json .Manifest}}' \
            | jq -r '.digest'
          )"
          echo "digest=${digest}" >> "$GITHUB_OUTPUT"

      - name: Generate artifact attestation
        uses: actions/attest-build-provenance@a2bbfa25375fe432b6a289bc6b6cd05ecd0c4c32 # v4.1.0
        with:
          subject-name: ${{ env.UV_GHCR_IMAGE }}
          subject-digest: ${{ steps.manifest-digest.outputs.digest }}
