name: Dev Tools

on:
  schedule:
    - cron: "0 3 * * 0"
  push:
    branches: [main]
    paths:
      - ".github/workflows/dev-tools.yml"
      - "**/packages.ini"

jobs:
  docker-dev-image:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - distribution: ubuntu
            release: focal
          - distribution: debian
            release: bullseye
          - distribution: ubuntu
            release: jammy
          - distribution: debian
            release: bookworm

    env:
      REGISTRY: ghcr.io
      IMAGE_NAME: libretime-dev

    steps:
      - name: Checkout
        uses: actions/checkout@v3

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

      - name: Generate packages list
        run: |
          tools/packages.py --dev --format line ${{ matrix.release }} \
            analyzer \
            playout \
            shared \
            > packages.list

      - name: Generate Dockerfile
        run: |
          cat <<EOF >> Dockerfile
          FROM ${{ matrix.distribution }}:${{ matrix.release }}

          COPY packages.list packages.list
          EOF

          [[ "${{ matrix.release }}" == "focal" ]] && \
          cat <<EOF >> Dockerfile
          RUN DEBIAN_FRONTEND=noninteractive apt-get --quiet update && \
              DEBIAN_FRONTEND=noninteractive apt-get --quiet install -y software-properties-common && \
              add-apt-repository -y ppa:libretime/libretime
          EOF

          cat <<EOF >> Dockerfile
          RUN DEBIAN_FRONTEND=noninteractive apt-get --quiet update && \
              DEBIAN_FRONTEND=noninteractive apt-get --quiet install -y \
              git \
              python3 \
              python3-pip \
              python3-venv \
              sudo \
              $(cat packages.list)

          RUN adduser --disabled-password --gecos '' --uid 1001 runner
          RUN adduser runner sudo
          RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
          EOF

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          push: ${{ github.repository_owner == 'libretime' }}
          tags: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ matrix.release }}