name: Container

on:
  push:
    tags: ["[0-9]+.[0-9]+.[0-9]+*"]
    branches: [main]
  pull_request:
    branches: [main]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}

jobs:
  build:
    runs-on: ubuntu-latest

    env:
      REGISTRY: ghcr.io
      NAMESPACE: ${{ github.repository_owner }}

    if: ${{ github.repository_owner == 'libretime' }}
    steps:
      - uses: actions/checkout@v3

      - uses: docker/setup-buildx-action@v2

      - uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Guess LIBRETIME_VERSION
        run: |
          make VERSION
          echo "LIBRETIME_VERSION=$(cat VERSION | tr -d [:blank:])" >> $GITHUB_ENV

      - name: Build python-builder
        uses: docker/build-push-action@v3
        with:
          context: .
          pull: true
          target: python-builder
          cache-from: type=gha,scope=python-builder
          cache-to: type=gha,scope=python-builder,mode=max

      - name: Build python-base
        uses: docker/build-push-action@v3
        with:
          context: .
          pull: true
          target: python-base
          cache-from: type=gha,scope=python-base
          cache-to: type=gha,scope=python-base,mode=max

      - name: Build python-base-ffmpeg
        uses: docker/build-push-action@v3
        with:
          context: .
          pull: true
          target: python-base-ffmpeg
          cache-from: type=gha,scope=python-base-ffmpeg
          cache-to: type=gha,scope=python-base-ffmpeg,mode=max

      - name: Build analyzer
        uses: ./.github/workflows/actions/build-container
        with:
          target: libretime-analyzer

      - name: Build api
        uses: ./.github/workflows/actions/build-container
        with:
          target: libretime-api

      - name: Build playout
        uses: ./.github/workflows/actions/build-container
        with:
          target: libretime-playout

      - name: Build worker
        uses: ./.github/workflows/actions/build-container
        with:
          target: libretime-worker

      - name: Build legacy
        uses: ./.github/workflows/actions/build-container
        with:
          target: libretime-legacy
