name: Docker Build

on:
  push:
    branches: [main]
    tags: ["v*"]
  pull_request:
    branches: [main]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ '{{' }} github.repository {{ '}}' }}

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install baker-cli
        run: pip install baker-cli

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

      - name: Log in to Container Registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ${{ '{{' }} env.REGISTRY {{ '}}' }}
          username: ${{ '{{' }} github.actor {{ '}}' }}
          password: ${{ '{{' }} secrets.GITHUB_TOKEN {{ '}}' }}

      - name: Build and push
        if: github.event_name != 'pull_request'
        run: |
          baker build \
            --check registry \
            --push=on \
            --targets all

      - name: Build (PR - no push)
        if: github.event_name == 'pull_request'
        run: |
          baker build \
            --check local \
            --push=off \
            --targets all

