sticks 0.3.6

A tool for managing C and C++ projects
Documentation
name: Release Packages

on:
  push:
    branches:
      - master
    paths:
      - 'Cargo.toml'
  release:
    types: [created]
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  check-version:
    name: Check Version and Create Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      version: ${{ steps.version.outputs.VERSION }}
      should_release: ${{ steps.check.outputs.SHOULD_RELEASE }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Get version from Cargo.toml
        id: version
        run: |
          VERSION=$(grep '^version' Cargo.toml | head -1 | cut -d'"' -f2)
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"

      - name: Check if tag exists
        id: check
        run: |
          if git rev-parse "v${{ steps.version.outputs.VERSION }}" >/dev/null 2>&1; then
            echo "SHOULD_RELEASE=false" >> $GITHUB_OUTPUT
            echo "Tag v${{ steps.version.outputs.VERSION }} already exists"
          else
            echo "SHOULD_RELEASE=true" >> $GITHUB_OUTPUT
            echo "Tag v${{ steps.version.outputs.VERSION }} does not exist, will create release"
          fi

      - name: Create Git Tag
        if: steps.check.outputs.SHOULD_RELEASE == 'true'
        run: |
          echo "Creating tag v${{ steps.version.outputs.VERSION }}"
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "v${{ steps.version.outputs.VERSION }}" -m "Release v${{ steps.version.outputs.VERSION }}"
          echo "Pushing tag to remote..."
          git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} "v${{ steps.version.outputs.VERSION }}"
          echo "Tag created and pushed successfully"

      - name: Create GitHub Release
        if: steps.check.outputs.SHOULD_RELEASE == 'true'
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ steps.version.outputs.VERSION }}
          name: Release v${{ steps.version.outputs.VERSION }}
          draft: false
          prerelease: false
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  test:
    name: Run Tests
    runs-on: ubuntu-latest
    needs: check-version
    if: needs.check-version.outputs.should_release == 'true' || github.event_name == 'release'
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable

      - name: Run tests
        run: cargo test --verbose

  publish-crate:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: [check-version, test]
    if: needs.check-version.outputs.should_release == 'true'
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable

      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
        continue-on-error: true

  build-deb:
    name: Build Debian Package
    runs-on: ubuntu-latest
    permissions:
      contents: write
    needs: [check-version, test]
    if: needs.check-version.outputs.should_release == 'true' || github.event_name == 'release'
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable

      - name: Install cargo-deb
        run: cargo install cargo-deb

      - name: Build release binary
        run: cargo build --release --verbose

      - name: Build .deb package
        run: cargo deb

      - name: Upload .deb package
        uses: actions/upload-artifact@v4
        with:
          name: sticks-deb
          path: target/debian/*.deb

      - name: Upload to release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ needs.check-version.outputs.version }}
          files: target/debian/*.deb
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build-binary:
    name: Build Generic Linux Binary
    runs-on: ubuntu-latest
    permissions:
      contents: write
    needs: [check-version, test]
    if: needs.check-version.outputs.should_release == 'true' || github.event_name == 'release'

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          target: x86_64-unknown-linux-gnu

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --target x86_64-unknown-linux-gnu

      - name: Strip binary
        run: strip target/x86_64-unknown-linux-gnu/release/sticks

      - name: Rename binary
        run: |
          cp target/x86_64-unknown-linux-gnu/release/sticks sticks-linux-x86_64
          chmod +x sticks-linux-x86_64

      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: sticks-linux-x86_64
          path: sticks-linux-x86_64

      - name: Upload to release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ needs.check-version.outputs.version }}
          files: sticks-linux-x86_64
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}