image-optimizer 1.5.0

CLI tool for optimizing images (JPEG, PNG, WebP, SVG)
Documentation
name: Build Binaries

on:
  push:
    branches: [master, main]
  pull_request:
    branches: [master, main]
  release:
    types: [published]

env:
  CARGO_TERM_COLOR: always

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt

      - name: Setup cache
        uses: Swatinem/rust-cache@v2

      - name: Check formatting
        run: cargo fmt -- --check

      - name: Run clippy
        run: cargo lint

  build:
    needs: lint
    strategy:
      fail-fast: false
      matrix:
        include:
          # Linux x86_64
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            binary_suffix: ""
            cross: false

          # Linux ARM64
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            binary_suffix: ""
            cross: true

          # macOS x86_64
          - os: macos-13
            target: x86_64-apple-darwin
            binary_suffix: ""
            cross: false

          # macOS ARM64
          - os: macos-14
            target: aarch64-apple-darwin
            binary_suffix: ""
            cross: false

          # Windows x86_64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            binary_suffix: ".exe"
            cross: false

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross
        if: matrix.cross
        run: cargo install cross --git https://github.com/cross-rs/cross

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

      - name: Build binary
        run: |
          if [ "${{ matrix.cross }}" = "true" ]; then
            cross build --release --target ${{ matrix.target }}
          else
            cargo build --release --target ${{ matrix.target }}
          fi
        shell: bash

      - name: Prepare binary for upload
        run: |
          mkdir -p artifacts
          if [ "${{ matrix.os }}" = "windows-latest" ]; then
            cp target/${{ matrix.target }}/release/image-optimizer${{ matrix.binary_suffix }} artifacts/image-optimizer-${{ matrix.target }}${{ matrix.binary_suffix }}
          else
            cp target/${{ matrix.target }}/release/image-optimizer artifacts/image-optimizer-${{ matrix.target }}
          fi
        shell: bash

      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: image-optimizer-${{ matrix.target }}
          path: artifacts/image-optimizer-${{ matrix.target }}${{ matrix.binary_suffix }}
          retention-days: 30

  release:
    if: github.event_name == 'release'
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Upload release assets
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          for dir in artifacts/*/; do
            if [ -d "$dir" ]; then
              for file in "$dir"*; do
                if [ -f "$file" ]; then
                  gh release upload ${{ github.event.release.tag_name }} "$file"
                fi
              done
            fi
          done

  publish-crates:
    if: github.event_name == 'release'
    needs: lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Setup cache
        uses: Swatinem/rust-cache@v2

      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_TOKEN }}
        run: cargo publish