uptime_lib 0.3.1

Multi-platform uptime library
Documentation
name: CI

on:
  push:
    branches:
      - main
    tags:
      - v*
  pull_request:

permissions:
  contents: read

defaults:
  run:
    shell: bash --noprofile --norc -euxo pipefail {0}

jobs:
  build:
    name: Build
    runs-on: ${{ matrix.runs-on }}
    strategy:
      matrix:
        os: [linux, darwin, windows]
        arch: [amd64, arm64]
        include:
          - os: linux
            arch: amd64
            runs-on: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: linux
            arch: arm64
            runs-on: ubuntu-latest
            target: aarch64-unknown-linux-gnu
          - os: darwin
            arch: amd64
            runs-on: macos-latest
            target: x86_64-apple-darwin
          - os: darwin
            arch: arm64
            runs-on: macos-latest
            target: aarch64-apple-darwin
          - os: windows
            arch: amd64
            runs-on: windows-latest
            target: x86_64-pc-windows-msvc
          - os: windows
            arch: arm64
            runs-on: windows-latest
            target: aarch64-pc-windows-msvc
      fail-fast: false
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Install target toolchain
        run: rustup target add ${{ matrix.target }}
      - name: Install cross-compiler for arm64 on Linux
        if: matrix.os == 'linux' && matrix.arch == 'arm64'
        run: |
          sudo apt-get install gcc-aarch64-linux-gnu
          echo "RUSTFLAGS=-C linker=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV"
      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2
      - name: Build
        run: cargo build --release --target=${{ matrix.target }}
      - name: Set executable name
        id: executable
        run: echo name=${{ github.event.repository.name }} >> "$GITHUB_OUTPUT"
      - name: Run
        if: matrix.os == 'darwin' || matrix.arch == 'amd64'
        run: target/${{ matrix.target }}/release/${{ steps.executable.outputs.name }}
      - name: Set artifact name
        id: artifact
        run: echo name=${{ steps.executable.outputs.name }}_${{ startsWith(github.ref, 'refs/tags/v') && github.ref_name ||
               github.event.pull_request.head.sha || github.sha }}_${{ matrix.os }}_${{ matrix.arch }} >> "$GITHUB_OUTPUT"
      - name: Build artifact
        run: |
          mkdir ${{ steps.artifact.outputs.name }}
          cp README.md LICENSE target/${{ matrix.target }}/release/${{ steps.executable.outputs.name }} ${{ steps.artifact.outputs.name }}
          if [[ ${{ matrix.os }} == windows ]]; then
            powershell Compress-Archive -Path ${{ steps.artifact.outputs.name }} -DestinationPath ${{ steps.artifact.outputs.name }}.zip
          else
            zip -r ${{ steps.artifact.outputs.name }}.zip ${{ steps.artifact.outputs.name }}
          fi
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ steps.artifact.outputs.name }}
          path: ${{ steps.artifact.outputs.name }}.zip

  test:
    name: Test
    runs-on: ${{ matrix.runs-on }}
    strategy:
      matrix:
        runs-on: [ubuntu-latest, macos-latest, windows-latest]
      fail-fast: false
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2
      - name: Build
        run: cargo build
      - name: Clippy
        run: cargo clippy
      - name: Test
        run: cargo test
      - name: Run
        run: cargo run

  release:
    name: Release
    needs: [build, test]
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
      - name: Create release
        uses: ncipollo/release-action@v1
        with:
          name: Release ${{ github.ref_name }}
          artifacts: '*/*.zip'