name: Rust

on:
  push:
    branches: [ "main" ]
    tags:
      - 'v*'  # Trigger the workflow on push to tags like v1.0, v20.15.10
  pull_request:
    branches: [ "main" ]

env:
  CARGO_TERM_COLOR: always

jobs:
  build_test_and_release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
    - uses: actions/checkout@v4.1.1
    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y zlib1g-dev libelf-dev
    - name: Test
      run: cargo test
    - name: clippy
      run: cargo clippy --all --tests --all-features --no-deps
    - name: Build
      run: cargo build --release
    - name: Create Release and Upload Artficat
      uses: ncipollo/release-action@v1.14.0
      if: startsWith(github.ref, 'refs/tags/v')
      with:
        artifacts: "target/release/bpftop"
        draft: true
        allowUpdates: true
        updateOnlyUnreleased: true
