---
name: "Build Deb Packages"

"on":
  push:
  pull_request:

env:
  SCCACHE_GHA_ENABLED: "true"
  RUSTC_WRAPPER: "sccache"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
jobs:
  build-deb-package:
    name: Build deb packages
    strategy:
      matrix:
        image:
          - name: ubuntu
            version: 22.04
          - name: debian
            version: 12
      fail-fast: false
    runs-on: ubuntu-latest
    container:
      image: "${{ matrix.image.name }}:${{ matrix.image.version }}"
      env:
        DEBIAN_FRONTEND: noninteractive
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: install curl
        run: |
          apt-get update && apt-get install -y curl
      - name: Setup sccache
        uses: mozilla-actions/sccache-action@v0.0.4
        with:
          version: "v0.4.2"
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Install dependencies
        run: |
          scripts/install_ubuntu_dependencies.sh
      - name: Install wasm-pack
        run: cargo install wasm-pack
      - name: Build packages (kanidm-unixd)
        run: make -f platform/debian/Makefile debs/kanidm-unixd
      - name: Build packages (kanidm)
        run: make -f platform/debian/Makefile debs/kanidm

      - name: Upload debs
        uses: actions/upload-artifact@v4
        with:
          name: "${{ matrix.image.name }}${{ matrix.image.version }}"
          path: |
            target/*.deb
  upload-to-releases:
    permissions:
      # https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs
      contents: write # allows the action to create a release

    name: Upload to releases
    needs: build-deb-package
    runs-on: ubuntu-latest
    steps:
      - name: Download previously built debs
        uses: actions/download-artifact@v4
        with:
          path: debs
          merge-multiple: true
      - name: List packages
        run: |
          find $(pwd) -name '*.deb'
      # TODO: This action is old and falling apart and will soon stop working.
      # Context: https://github.com/marvinpinto/action-automatic-releases/pull/2
      - uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: "debs"
          prerelease: true
          title: ".deb Packages"
          files: "debs/*.deb"
        if: ${{ github.ref == 'refs/heads/master' && github.repository == 'kanidm/kanidm' }}
