mostro-core 0.8.3

Mostro Core library
Documentation
name: Build and Test for all targets

on:
  push:
    tags: ['v*.*.*']          # run only when a tag like v1.2.3 is pushed
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  # Generate changelog (runs once)
  changelog:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      content: ${{ steps.changelog.outputs.content }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Ensure all tags are present
        run: git fetch --tags --force --prune

      - name: Generate changelog
        uses: orhun/git-cliff-action@v4
        id: changelog
        with:
          config: cliff.toml
          # Use the latest tag vs previous tag automatically
          args: --latest --strip header --output CHANGELOG.md
        env:
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REF: ${{ github.ref }}

  # Test the code
  test:
    runs-on: ubuntu-latest
    needs: changelog
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
        with: { cache-on-failure: true }
      - name: cargo test
        run: cargo test

  # Build the library (single target)
  build:
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
        with: { cache-on-failure: true }

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

  # Publish to crates.io (only if all builds succeed).
  # Tag pushes only: workflow_dispatch must not publish an arbitrary branch to crates.io.
  # Version bumps and tags come from local `cargo release` (see Cargo.toml [package.metadata.release]).
  publish:
    runs-on: ubuntu-latest
    needs: [test, build]
    if: success() && startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
        with: { cache-on-failure: true }
      - name: Publish to crates.io
        run: cargo publish --locked
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}

  # Create GitHub Release for the pushed version tag (same guard as publish).
  release:
    runs-on: ubuntu-latest
    needs: [changelog, build, publish]
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write
    steps:
      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          name: Release ${{ github.ref_name }}
          body: ${{ needs.changelog.outputs.content }}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}