name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  schedule:
    - cron: '0 0 * * 0' # at midnight of each sunday
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  skip-check:
    permissions:
      actions: write
      contents: read
    runs-on: ubuntu-latest
    outputs:
      should_skip: ${{ steps.skip_check.outputs.should_skip }}
    steps:
      - id: skip_check
        uses: fkirc/skip-duplicate-actions@v5
        with:
          concurrent_skipping: 'same_content_newer'
          cancel_others: true
          paths_ignore: '["*.md"]'

  # Quality checks for pull requests
  pr-checks:
    name: Pull Request Quality Checks
    if: github.event_name == 'pull_request'
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup

      - name: Format Check
        run: cargo fmt --all --check

      - name: Lint Check
        run: cargo check --all-targets

      - name: Clippy Check
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Unit Tests
        run: cargo test --all --exclude e2e_test

  develop:
    needs: skip-check
    if: needs.skip-check.outputs.should_skip != 'true'
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup

      - name: Format
        run: cargo fmt --all --check

      - name: Lint
        run: cargo check --all-targets

      - name: Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Test
        run: cargo test --all --exclude e2e_test

      - name: Build debug
        run: |
          touch rustfs/build.rs
          cargo build -p rustfs --bins

      - name: Pack artifacts
        run: |
          mkdir -p ./target/artifacts
          cp target/debug/rustfs    ./target/artifacts/rustfs-debug

      - uses: actions/upload-artifact@v4
        with:
          name: rustfs
          path: ./target/artifacts/*

  s3s-e2e:
    name: E2E (s3s-e2e)
    needs:
      - skip-check
      - develop
    if: needs.skip-check.outputs.should_skip != 'true'
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
        with:
          cache-on-failure: true
          cache-all-crates: true

      - name: Install s3s-e2e
        run: |
          cargo install s3s-e2e --git https://github.com/Nugine/s3s.git
          s3s-e2e --version

      - uses: actions/download-artifact@v4
        with:
          name: rustfs
          path: ./target/artifacts

      - name: Run s3s-e2e
        timeout-minutes: 10
        run: |
          ./scripts/e2e-run.sh ./target/artifacts/rustfs-debug /tmp/rustfs

      - uses: actions/upload-artifact@v4
        with:
          name: s3s-e2e.logs
          path: /tmp/rustfs.log
