name: Debian CI Images

on:
  push:
    branches: [main, develop]

env:
  REGISTRY: 'git.uls.labskb.ru'
  NAMESPACE: ci
  IMAGE_NAME: debian-ci

jobs:
  quality-check:
    name: Quality Checks
    runs-on: docker-ci
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
      
      - name: Debug
        run: |
          echo "Building Debian CI images"
          echo "Ref: ${{ gitea.ref }}"
      
      - name: Hadolint Dockerfile
        uses: hadolint/hadolint-action@v3.3.0
        with:
          dockerfile: Dockerfile
          failure-threshold: error
  
  build:
    name: Build Debian Images
    runs-on: "x86"
    container:
      image: git.uls.labskb.ru/ci/docker-ci:latest
    needs: quality-check
    if: ${{ gitea.event_name != 'pull_request' }}
    
    strategy:
      matrix:
        debian_version: [12, 13]
        include:
          - debian_version: 12
            base_image: debian:12
            codename: bookworm
            short_name: 12
          - debian_version: 13
            base_image: debian:13
            codename: trixie
            short_name: 13
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      
      - name: Login to registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.CI_USER }}
          password: ${{ secrets.CI_TOKEN }}
      
      - name: Extract metadata
        uses: docker/metadata-action@v5
        id: meta
        with:
          images: ${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}
          tags: |
            type=raw,value=${{ matrix.short_name }}-latest,enable=${{ gitea.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/develop' }}
            type=raw,value=${{ matrix.codename }}-latest,enable=${{ gitea.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/develop' }}

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

      - name: Build and push Debian ${{ matrix.debian_version }}
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          build-args: |
            DEBIAN_VERSION=${{ matrix.debian_version }}
            BASE_IMAGE=${{ matrix.base_image }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: linux/amd64,linux/arm64,linux/arm/v7
