autocommit-rs 0.1.2

Tool that generates conventional commits from staged changes and executes the commit in one go.
name: Release

on:
  push:
    branches: [main]
    paths:
      - 'Cargo.toml'

concurrency:
  group: release-${{ github.ref }}
  cancel-in-progress: false

permissions:
  contents: write

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest

    outputs:
      version: ${{ steps.version.outputs.version }}
      tag: ${{ steps.version.outputs.tag }}
      is_new: ${{ steps.check_tag.outputs.is_new }}

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Extract version from Cargo.toml
        id: version
        run: |
          VERSION=$(grep -A 100 '^\[package\]' Cargo.toml | grep -m1 '^version = ' | sed 's/version = "\(.*\)"/\1/')
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "tag=v$VERSION" >> "$GITHUB_OUTPUT"

      - name: Check if tag already exists
        id: check_tag
        run: |
          if git rev-parse "${{ steps.version.outputs.tag }}" >/dev/null 2>&1; then
            echo "is_new=false" >> "$GITHUB_OUTPUT"
            echo "Tag ${{ steps.version.outputs.tag }} already exists — skipping release."
          else
            echo "is_new=true" >> "$GITHUB_OUTPUT"
          fi

      - name: Create and push tag
        if: steps.check_tag.outputs.is_new == 'true'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "${{ steps.version.outputs.tag }}" -m "Release ${{ steps.version.outputs.tag }}"
          git push origin "${{ steps.version.outputs.tag }}"

      - name: Install Rust toolchain
        if: steps.check_tag.outputs.is_new == 'true'
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable

      - name: Build and validate
        if: steps.check_tag.outputs.is_new == 'true'
        run: cargo build --release

  build:
    name: Build (${{ matrix.target }})
    needs: release
    if: needs.release.outputs.is_new == 'true'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            artifact_name: autocommit
          - target: x86_64-apple-darwin
            os: macos-latest
            artifact_name: autocommit
          - target: aarch64-apple-darwin
            os: macos-latest
            artifact_name: autocommit
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            artifact_name: autocommit.exe

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: .
          key: ${{ matrix.target }}

      - name: Build release binary
        run: cargo build --release --target ${{ matrix.target }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: autocommit-${{ matrix.target }}
          path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}

  create-release:
    name: Create GitHub Release
    needs: [release, build]
    if: needs.release.outputs.is_new == 'true'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Package artifacts
        shell: bash
        run: |
          cd artifacts
          for dir in */; do
            target="${dir%/}"
            if [ "$target" = "autocommit-x86_64-pc-windows-msvc" ]; then
              cd "$target"
              zip -r "../autocommit-${target}.zip" .
              cd ..
            else
              cd "$target"
              tar czf "../autocommit-${target}.tar.gz" .
              cd ..
            fi
          done

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ needs.release.outputs.tag }}
          files: |
            artifacts/autocommit-*.tar.gz
            artifacts/autocommit-*.zip
          generate_release_notes: true