# SPDX-License-Identifier: EUPL-1.2
name: Build .deb package

env:
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
  RUSTFLAGS: "-D warnings -W unreachable-pub -W rust-2021-compatibility"
  RUSTUP_MAX_RETRIES: 10
  RUST_BACKTRACE: short

on:
  workflow_dispatch:
  push:
    tags:
      - v*

jobs:
  build:
    name: Package for debian on ${{ matrix.arch }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        build: [linux-amd64, ]
        include:
          - build: linux-amd64
            arch: amd64
            os: ubuntu-latest
            rust: stable
            artifact_name: 'linux-amd64'
            target: x86_64-unknown-linux-gnu
    steps:
      - uses: actions/checkout@v3
      - id: os-deps
        name: install OS dependencies
        run: |
          apt-get update
          apt-get install -y mandoc debhelper quilt build-essential
      - id: rustup-setup
        name: Install rustup and toolchains
        shell: bash
        run: |
          if ! command -v rustup &>/dev/null; then
            curl --proto '=https' --tlsv1.2 --retry 10 --retry-connrefused --location --silent --show-error --fail "https://sh.rustup.rs" | sh -s -- --default-toolchain none -y
            source "${HOME}/.cargo/env"
            echo "${CARGO_HOME:-$HOME/.cargo}/bin" >> $GITHUB_PATH
            echo "CARGO_HOME=${CARGO_HOME:-$HOME/.cargo}" >> $GITHUB_ENV
            rustup toolchain install --profile minimal ${{ matrix.rust }} --target ${{ matrix.target }}
            rustup default ${{ matrix.rust }}
          fi
      - name: Build binary
        run: |
          VERSION=$(grep -m1 version meli/Cargo.toml | head -n1 | cut -d'"' -f 2 | head -n1)
          make deb-dist
          mkdir artifacts
          echo "VERSION=${VERSION}" >> $GITHUB_ENV
          mv ../meli_*.deb artifacts/meli-${VERSION}-${{ matrix.artifact_name }}.deb
      - name: Upload Artifacts
        uses: actions/upload-artifact@v3
        with:
          name: meli-${{env.VERSION}}-${{ matrix.artifact_name }}.deb
          path: artifacts/meli-${{env.VERSION}}-${{ matrix.artifact_name }}.deb
          if-no-files-found: error
          retention-days: 30
