name: CI (Release)

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  test-again:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    steps:
    - uses: actions/checkout@v3
    - uses: dtolnay/rust-toolchain@stable
      with:
        toolchain: stable
    - uses: actions/cache@v3
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target/release
          target/debug
          target/.rustc_info.json
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
    - name: Test 
      run: cargo test --verbose --all-features
  release:
    runs-on: ubuntu-latest
    needs:
      - test-again
    steps:
    - uses: actions/checkout@v3
    - name: Create a Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        # The name of the tag. This should come from the webhook payload, `github.GITHUB_REF` when a user pushes a new tag
        tag_name: ${{ github.ref }}
        # The name of the release. For example, `Release v1.0.1`
        release_name: Release ${{ github.ref }}
    - name: Get the version
      id: get_version
      run: |
        echo VERSION=${GITHUB_REF#refs/tags/} >> $GITHUB_OUTPUT
        echo DEB_NAME=$(basename $(ls ${{ runner.temp }}/deb-package/*.deb | tail -n 1)) >> $GITHUB_OUTPUT
    - uses: actions/cache@v3
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target/release
          target/debug
          target/.rustc_info.json
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
    - uses: dtolnay/rust-toolchain@stable
      with:
        toolchain: stable
    - name: Upload crate
      run: cargo publish --token ${{ secrets.CARGO_TOKEN }}
