name: 'Build binary file and bundle packages'
author: 'Neshura'

on:
  pull_request:
    branches:
      - main

jobs:
  test:
    runs-on: docker
    container: forgejo.neshweb.net/ci-docker-images/rust-node:latest
    steps:
      -
          name: Add Clippy
          run: rustup component add clippy
      -
          name: Checking Out Repository Code
          uses: https://code.forgejo.org/actions/checkout@v3
      -
        name: Set Up Cargo Cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      -
        name: Run Clippy
        run: cargo clippy

  build:
    needs: test
    if: success()
    runs-on: docker
    container: forgejo.neshweb.net/ci-docker-images/rust-node:latest
    steps:
      -
        name: Checking Out Repository Code
        uses: https://code.forgejo.org/actions/checkout@v3
      -
        name: Prepare build environment
        run: mkdir dist
      -
        name: Compiling To Linux Target
        run: |
          cargo build -r
          mv target/release/${{ github.event.repository.name }} dist/${{ github.event.repository.name }}-linux-amd64
      -
        name: Bundle .deb package
        run: |
          cargo deb
          DEBIAN_REF=$(cat Cargo.toml | grep -E "(^|\|)version =" | cut -f2- -d= | tr -d \" | tr -d " " | tr - \~)
          echo "DEBIAN_REF=$DEBIAN_REF" >> dist/build.env
          DEBIAN_REV=-$(cat Cargo.toml | grep -E "(^|\|)revision =" | cut -f2- -d= | tr -d \" | tr -d " ")
          echo "DEBIAN_REV=$DEBIAN_REV" >> dist/build.env
          mv target/debian/${{ github.event.repository.name }}_"$DEBIAN_REF""$DEBIAN_REV"_amd64.deb dist/${{ github.event.repository.name }}_"$DEBIAN_REF""$DEBIAN_REV"_amd64.deb
      -
        name: Uploading Build Artifact
        uses: actions/upload-artifact@v3
        with:
          name: release_blobs
          path: dist
          if-no-files-found: error