imgup 4.2.0

Upload images via APIs
name: Main

on:
  push:
    branches: [main]
    tags: [v*.*.*]
  pull_request:
    branches: [main]

permissions:
  contents: read

jobs:
  prek:
    name: Prek
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Run prek
        uses: j178/prek-action@7b101f8338a08d2bd6fdb3625e4e7692428763a3 # v3.1.1

  lint:
    name: Lint code
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Rust Cache
        uses: swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3

      - name: Lint code
        run: make lint-ci

  test:
    name: Test
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Rust Cache
        uses: swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3

      - uses: taiki-e/install-action@fe3acb53a98b62c772db35de75ce0e4595e7ca8c # v2.90.8
        with:
          tool: nextest,cargo-llvm-cov

      - name: Run tests
        run: make test-ci

      - name: Upload coverage
        uses: deadnews/coverage-badge-action@bc4aaf332f25c52d1e8dd21ef9ed0b2337ad39af # v1.0.0
        if: github.event_name == 'push' && matrix.os == 'ubuntu-latest'
        with:
          file: lcov.info
          type: lcov

  cargo-deploy:
    name: Release to crates.io
    if: github.ref_type == 'tag'
    needs: [lint, test]
    environment: crates.io
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Authenticate to crates.io
        uses: rust-lang/crates-io-auth-action@3268eb413133e2960fa4ae0e963ca76380c54cd9 # v1.0.6
        id: auth

      - name: Set version in Cargo.toml
        run: sed "s|^version.*=.*$|version = '${GITHUB_REF_NAME#v}'|" -i Cargo.toml

      - name: Sync Cargo.lock version
        run: cargo generate-lockfile

      - name: Publishing on crates.io
        run: cargo publish --locked --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}

  pypi-build:
    name: Build wheel
    if: github.ref_type == 'tag'
    needs: [lint, test]
    strategy:
      fail-fast: false
      matrix:
        include:
          - { os: ubuntu-latest, target: x86_64, manylinux: 2_28 }
          - { os: ubuntu-latest, target: aarch64, manylinux: 2_28 }
          - { os: ubuntu-latest, target: x86_64, manylinux: musllinux_1_2 }
          - { os: ubuntu-latest, target: aarch64, manylinux: musllinux_1_2 }
          - { os: macos-latest, target: aarch64 }
          - { os: windows-latest, target: x86_64 }
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Set version
        shell: bash
        run: sed "s|^version.*|version = '${GITHUB_REF_NAME#v}'|" Cargo.toml > t && mv t Cargo.toml

      - name: Build wheel
        uses: pyo3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0
        with:
          command: build
          args: --release --compatibility pypi
          target: ${{ matrix.target }}
          manylinux: ${{ matrix.manylinux || '' }}

      - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
        with:
          name: wheel-${{ matrix.os }}-${{ matrix.target }}-${{ matrix.manylinux || 'native' }}
          path: target/wheels/

  pypi-deploy:
    name: Release to PyPI
    if: github.ref_type == 'tag'
    needs: [pypi-build]
    environment: pypi
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    steps:
      - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
        with:
          pattern: wheel-*
          merge-multiple: true
          path: dist/

      - uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
        with:
          packages-dir: dist

  github-deploy:
    name: Release to GitHub
    if: github.ref_type == 'tag'
    needs: [lint, test]
    environment: github-releases
    permissions:
      contents: write
    env:
      CHANGELOG: https://github.com/${{ github.repository }}/blob/main/CHANGELOG.md
      PRERELEASE: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Create GitHub Release
        run: |
          gh release create "${GITHUB_REF_NAME}" \
            --title "${GITHUB_REF_NAME}" \
            --notes="See [the CHANGELOG](${CHANGELOG}) for more details." \
            --draft="${PRERELEASE}" \
            --prerelease="${PRERELEASE}"
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  github-build:
    name: Build binary (${{ matrix.target }})
    if: github.ref_type == 'tag'
    needs: [github-deploy]
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - { os: ubuntu-latest, target: x86_64-unknown-linux-gnu }
          - { os: ubuntu-latest, target: x86_64-unknown-linux-musl }
          - { os: ubuntu-latest, target: aarch64-unknown-linux-gnu }
          - { os: ubuntu-latest, target: aarch64-unknown-linux-musl }
          - { os: macos-latest, target: aarch64-apple-darwin }
          - { os: windows-latest, target: x86_64-pc-windows-msvc }
          - { os: windows-latest, target: aarch64-pc-windows-msvc }
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Set version
        shell: bash
        run: sed "s|^version.*|version = '${GITHUB_REF_NAME#v}'|" Cargo.toml > t && mv t Cargo.toml

      - name: Build and upload binary
        uses: taiki-e/upload-rust-binary-action@f0d45ae91ee7b8ee928de7a9d04d893a08bcbec6 # v1.30.2
        with:
          bin: imgup
          target: ${{ matrix.target }}

  aur-deploy:
    name: Release to AUR
    if: true && !contains(github.ref, 'alpha') && !contains(github.ref, 'beta')
    needs: [github-build]
    environment:
      name: aur
      url: https://aur.archlinux.org/packages/${{ github.event.repository.name }}-bin
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Update version in PKGBUILD
        run: |
          pkgver="${GITHUB_REF_NAME#v}"
          pkgver="${pkgver//-/_}"
          sed "s|^pkgver=.*$|pkgver=\"${pkgver}\"|" -i PKGBUILD

      - name: Deploy PKGBUILD to the AUR
        uses: ksxgithub/github-actions-deploy-aur@084b0d9b15415bf9cdb65d44dad1efe37a354050 # v4.2.0
        with:
          pkgname: ${{ github.event.repository.name }}-bin
          pkgbuild: ./PKGBUILD
          commit_username: ${{ github.actor }}
          commit_email: ${{ github.actor }}@users.noreply.github.com
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: Upstream release ${{ github.ref_name }}
          updpkgsums: true
          test: true