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:
        os-ver:
          - "ubuntu:18.04"
          - "ubuntu:20.04"
          - "ubuntu:22.04"
          - "ubuntu:24.04"
          - "ubuntu:24.10"
          - "debian:oldstable"
          - "debian:stable"
          - "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:
          - isPR: true
            arch: "linux/arm/v7"
          - isPR: true
            arch: "linux/arm64/v8"
          # ubuntu:20.04 linux/arm/v7 has broken certificate handling
          #- os-ver: "ubuntu:20.04"
          #  arch: "linux/arm/v7"

    runs-on: ubuntu-latest
    name: Compile ${{ matrix.os-ver }} ${{ matrix.arch }}
    steps:
      - uses: actions/checkout@v4
        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@v4
        if: ${{github.event_name != 'pull_request'}}
        with:
          name: ${{ steps.replace.outputs.replaced }}
          path: build-deb/*.deb
