name: build-deb

on:
  push:
    branches:
      - master
    tags:
      - v*
  pull_request:
    branches:
      - master

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  deb-package:
    strategy:
      fail-fast: false
      matrix:
        runner:
          - "ubuntu-latest"
          - "ubuntu-24.04-arm"
        os-ver:
          - "ubuntu:18.04"
          - "ubuntu:20.04"
          - "ubuntu:22.04"
          - "ubuntu:24.04"
          - "ubuntu:25.04"
          - "ubuntu:25.10"
          - "debian:bullseye"
          - "debian:bookworm"
          - "debian:trixie"
          - "debian:testing"
          - "debian:unstable"
        arch:
          - "linux/amd64"
          - "linux/arm/v7"
          - "linux/arm64/v8"

        isPR:
          - ${{github.event_name == 'pull_request'}}
        # Run only the amd64 build in pull reqs:
        exclude:
          - runner: "ubuntu-24.04-arm"
            arch: "linux/amd64"
          - runner: "ubuntu-latest"
            arch: "linux/arm64/v8"
          - runner: "ubuntu-24.04-arm"
            arch: "linux/arm/v7"
          - isPR: true
            arch: "linux/arm/v7"
          - isPR: true
            arch: "linux/arm64/v8"

    runs-on: ${{ matrix.runner }}
    name: Compile ${{ matrix.os-ver }} ${{ matrix.arch }}
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0 # We need history/tags to generate version info

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Build and upload deb
        env:
          DEB_UPLOAD_ACCESS_KEY_ID: ${{ secrets.DEB_UPLOAD_ACCESS_KEY_ID }}
          DEB_UPLOAD_ENDPOINT: ${{ secrets.DEB_UPLOAD_ENDPOINT }}
          DEB_UPLOAD_SECRET_ACCESS_KEY: ${{ secrets.DEB_UPLOAD_SECRET_ACCESS_KEY }}
          PKG_SIGNING_KEY: ${{ secrets.PKG_SIGNING_KEY }}
          PKG_SIGNING_KEY_ID: ${{ secrets.PKG_SIGNING_KEY_ID }}
        run: |
          docker run \
            --platform=${{ matrix.arch }} \
            -e TZ=Etc/UTC \
            -e DEB_UPLOAD_ACCESS_KEY_ID \
            -e DEB_UPLOAD_SECRET_ACCESS_KEY \
            -e PKG_SIGNING_KEY \
            -e PKG_SIGNING_KEY_ID \
            -e DEB_UPLOAD_ENDPOINT \
            -e GH_ACTIONS=y \
            -e GH_EVENT="${{ github.event_name }}" \
            -v "$(pwd):/build" \
            -w /build \
          ${{ matrix.os-ver }} \
          bash /build/scripts/debian/build-deb.sh ${{ matrix.os-ver }} ${{ matrix.arch }}

      - uses: frabert/replace-string-action@v2.5
        if: ${{github.event_name != 'pull_request'}}
        id: replace
        with:
          pattern: '(\w+):([\w\.]+):linux/(\w+)(?:/(\w+))?'
          string: '${{ matrix.os-ver }}:${{ matrix.arch }}'
          replace-with: $1-$2-$3$4.deb

      - uses: actions/upload-artifact@v5
        if: ${{github.event_name != 'pull_request'}}
        with:
          name: ${{ steps.replace.outputs.replaced }}
          path: build-deb/*.deb
