sql-splitter 1.20.0

High-performance CLI tool for splitting large SQL dump files into individual table files
Documentation
name: Docker

# Builds and publishes the sql-splitter image to Docker Hub and GHCR.
#
# Triggers:
#   - push to a version tag (v*): builds that tag
#   - workflow_dispatch with `tag` input: builds the given ref (used to backfill
#     old releases, or to re-publish a tag whose build was cancelled)
#
# Each architecture builds on a runner of its own architecture and pushes an
# untagged image by digest; the `merge` job then stitches those digests into one
# multi-arch manifest per registry. Previously a single runner built both arches
# with QEMU, which meant compiling Rust + DuckDB under emulation for arm64 and
# took ~3h per release (v1.18.1 took 3h55m). Native runners do it in minutes.
#
# Required secrets:
#   - DOCKERHUB_USERNAME: Docker Hub account (e.g. helgesverre)
#   - DOCKERHUB_TOKEN:    Docker Hub access token with read/write/delete on the repo
#
# GHCR uses the workflow's GITHUB_TOKEN -- no extra secret needed.

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Git tag to build (e.g. v1.13.5). Required for backfill runs.'
        required: true
        type: string

permissions:
  contents: read
  packages: write

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: linux/amd64
            runner: ubuntu-latest
          - platform: linux/arm64
            runner: ubuntu-24.04-arm
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Resolve ref to build
        id: ref
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "ref=${{ inputs.tag }}" >> "$GITHUB_OUTPUT"
          else
            echo "ref=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
          fi
          platform="${{ matrix.platform }}"
          echo "slug=${platform//\//-}" >> "$GITHUB_OUTPUT"
          # A Docker ref must be lowercase and the owner is "HelgeSverre".
          # metadata-action lowercases for us in `merge`; the raw ref below doesn't.
          owner="${{ github.repository_owner }}"
          echo "owner=${owner,,}" >> "$GITHUB_OUTPUT"

      - uses: actions/checkout@v4
        with:
          ref: ${{ steps.ref.outputs.ref }}
          fetch-depth: 0

      - name: Overlay Dockerfile from main (backfill runs)
        # Tags published before Docker support was added don't have a Dockerfile.
        # On workflow_dispatch backfills, copy the current Dockerfile + .dockerignore
        # from main on top of the tag's source tree so old releases can still be built.
        if: github.event_name == 'workflow_dispatch'
        run: |
          git fetch origin main --depth=1
          git checkout origin/main -- Dockerfile .dockerignore

      - name: Set up Buildx
        uses: docker/setup-buildx-action@v4

      - name: Log in to Docker Hub
        uses: docker/login-action@v4
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Log in to GHCR
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push by digest
        id: build
        uses: docker/build-push-action@v7
        with:
          context: .
          platforms: ${{ matrix.platform }}
          # Push the same untagged image to both registries; `merge` applies the
          # tags. Quoting keeps the comma-separated name list inside one CSV field.
          outputs: type=image,"name=docker.io/${{ secrets.DOCKERHUB_USERNAME }}/sql-splitter,ghcr.io/${{ steps.ref.outputs.owner }}/sql-splitter",push-by-digest=true,name-canonical=true,push=true
          # Scope the cache per arch so the two runners don't overwrite each other.
          cache-from: type=gha,scope=${{ steps.ref.outputs.slug }}
          cache-to: type=gha,mode=max,scope=${{ steps.ref.outputs.slug }}

      - 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-${{ steps.ref.outputs.slug }}
          path: /tmp/digests/*
          if-no-files-found: error
          retention-days: 1

  merge:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Resolve ref
        id: ref
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "ref=${{ inputs.tag }}" >> "$GITHUB_OUTPUT"
          else
            echo "ref=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
          fi

      - name: Download digests
        uses: actions/download-artifact@v4
        with:
          path: /tmp/digests
          pattern: digests-*
          merge-multiple: true

      - name: Set up Buildx
        uses: docker/setup-buildx-action@v4

      - name: Log in to Docker Hub
        uses: docker/login-action@v4
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Log in to GHCR
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Compute image metadata
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: |
            docker.io/${{ secrets.DOCKERHUB_USERNAME }}/sql-splitter
            ghcr.io/${{ github.repository_owner }}/sql-splitter
          # Strip any leading "v" so we get both `v1.13.5` and `1.13.5`, plus partial semver tags.
          # `latest` is applied automatically when the ref is the highest semver tag.
          tags: |
            type=semver,pattern={{version}},value=${{ steps.ref.outputs.ref }}
            type=semver,pattern={{major}}.{{minor}},value=${{ steps.ref.outputs.ref }}
            type=semver,pattern={{major}},value=${{ steps.ref.outputs.ref }}
            type=raw,value=${{ steps.ref.outputs.ref }}

      - name: Create manifest list and push
        working-directory: /tmp/digests
        # Sources are read from Docker Hub; the arch images were pushed to both
        # registries above, so imagetools only has to write the manifest lists.
        run: |
          # SC2046 is deliberate: both substitutions must word-split, into
          # `-t <tag>` pairs and into one argument per digest.
          # shellcheck disable=SC2046
          docker buildx imagetools create \
            $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
            $(printf 'docker.io/${{ secrets.DOCKERHUB_USERNAME }}/sql-splitter@sha256:%s ' *)

      - name: Inspect published manifest
        run: |
          docker buildx imagetools inspect \
            "docker.io/${{ secrets.DOCKERHUB_USERNAME }}/sql-splitter:${{ steps.ref.outputs.ref }}"