name: build-docker

on:
  push:
    branches: [main]
    tags:
      - 'v*'

jobs:
  publish:
    concurrency:
      group: ${{ github.ref }}-docker
      cancel-in-progress: true
    permissions:
      packages: write
      contents: read
      id-token: write

    runs-on: depot-ubuntu-24.04
    steps:
      - uses: actions/checkout@v6

      - name: Get Repo Owner
        id: get_repo_owner
        run: echo "REPO_OWNER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" > $GITHUB_ENV

      - uses: depot/setup-action@v1

      - name: Login to container Registry
        uses: docker/login-action@v3
        with:
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: ghcr.io

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ env.REPO_OWNER }}/atuin
          flavor: |
            latest=false
          tags: |
            type=ref,event=branch
            type=sha,prefix=
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}

      - name: Build and push
        uses: depot/build-push-action@v1
        with:
          push: true
          platforms: linux/amd64,linux/arm64
          file: ./Dockerfile
          context: .
          provenance: false
          build-args: |
            Version=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] || 'dev' }}
            GitCommit=${{ github.sha }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
