|
| 1 | +name: CI |
| 2 | + |
| 3 | +env: |
| 4 | + RUST_TOOLCHAIN: "1.90.0" |
| 5 | + |
| 6 | +permissions: |
| 7 | + contents: read |
| 8 | + |
| 9 | +on: |
| 10 | + pull_request: |
| 11 | + branches: [main] |
| 12 | + paths-ignore: |
| 13 | + - "**/*.md" |
| 14 | + - LICENSE |
| 15 | + - "**/*.gitignore" |
| 16 | + - .editorconfig |
| 17 | + - docs/** |
| 18 | + |
| 19 | +jobs: |
| 20 | + validate-pr: |
| 21 | + name: Validate PR source and version bump |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - name: Checkout PR head |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Fetch main branch for comparison |
| 30 | + run: git fetch origin main:refs/remotes/origin/main |
| 31 | + |
| 32 | + - name: Read versions (Cargo.toml) |
| 33 | + id: versions |
| 34 | + shell: bash |
| 35 | + run: | |
| 36 | + PR_CRATE_VER=$(grep -m1 '^version\s*=\s*"' Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/') |
| 37 | + MAIN_CRATE_VER=$(git show origin/main:Cargo.toml | grep -m1 '^version\s*=\s*"' | sed -E 's/.*"([^"]+)".*/\1/') |
| 38 | + echo "pr_crate_ver=$PR_CRATE_VER" >> $GITHUB_OUTPUT |
| 39 | + echo "main_crate_ver=$MAIN_CRATE_VER" >> $GITHUB_OUTPUT |
| 40 | + echo "PR Cargo.toml version: $PR_CRATE_VER" |
| 41 | + echo "Main Cargo.toml version: $MAIN_CRATE_VER" |
| 42 | +
|
| 43 | + - name: Ensure workspace/package versions match (if both present) |
| 44 | + if: ${{ github.event.pull_request.base.ref == 'main' }} |
| 45 | + shell: bash |
| 46 | + run: | |
| 47 | + set -euo pipefail |
| 48 | + PKG_VER=$(grep -m1 '^version\s*=\s*"' Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/' || true) |
| 49 | + WS_VER=$(awk '/^\[workspace.package\]/{f=1;next} /^\[/{f=0} f && /^version\s*=/{print; exit}' Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/' || true) |
| 50 | + if [ -n "${WS_VER}" ] && [ -n "${PKG_VER}" ] && [ "${WS_VER}" != "${PKG_VER}" ]; then |
| 51 | + echo "[workspace.package].version (${WS_VER}) does not match [package].version (${PKG_VER}). Please keep them in sync before merging to main." >&2 |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +
|
| 55 | + - name: Ensure version increased |
| 56 | + if: ${{ github.event.pull_request.base.ref == 'main' }} |
| 57 | + shell: bash |
| 58 | + run: | |
| 59 | + cmp_versions () { [ "$1" = "$2" ] && return 1; printf '%s\n%s\n' "$2" "$1" | sort -V | head -n1 | grep -q "^$2$"; } |
| 60 | + if ! cmp_versions "${{ steps.versions.outputs.pr_crate_ver }}" "${{ steps.versions.outputs.main_crate_ver }}"; then |
| 61 | + echo "Cargo.toml version must be greater in the PR than on main (current: ${{ steps.versions.outputs.pr_crate_ver }}, main: ${{ steps.versions.outputs.main_crate_ver }})." >&2 |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | +
|
| 65 | + rust-checks: |
| 66 | + name: Rust checks (${{ matrix.os }}) |
| 67 | + needs: validate-pr |
| 68 | + runs-on: ${{ matrix.os }} |
| 69 | + strategy: |
| 70 | + fail-fast: false |
| 71 | + matrix: |
| 72 | + os: [ubuntu-latest, macos-latest, windows-latest] |
| 73 | + |
| 74 | + steps: |
| 75 | + - uses: actions/checkout@v4 |
| 76 | + |
| 77 | + # --- Rust toolchain --- |
| 78 | + - name: Setup Rust toolchain (pinned) |
| 79 | + uses: actions-rust-lang/setup-rust-toolchain@v1 |
| 80 | + with: |
| 81 | + toolchain: ${{ env.RUST_TOOLCHAIN }} |
| 82 | + components: clippy,rustfmt |
| 83 | + |
| 84 | + - name: Cache cargo |
| 85 | + uses: actions/cache@v4 |
| 86 | + with: |
| 87 | + path: | |
| 88 | + ~/.cargo/registry/index/ |
| 89 | + ~/.cargo/registry/cache/ |
| 90 | + ~/.cargo/git/db/ |
| 91 | + target/ |
| 92 | + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} |
| 93 | + |
| 94 | + - name: Show versions |
| 95 | + shell: bash |
| 96 | + run: | |
| 97 | + rustc -Vv |
| 98 | + cargo -V |
| 99 | + echo "Running on: $(uname -a || echo windows)" |
| 100 | +
|
| 101 | + - name: Test |
| 102 | + run: cargo test --all-features |
| 103 | + |
| 104 | + - name: Clippy |
| 105 | + run: cargo clippy --all-targets --all-features -- -D warnings |
| 106 | + |
| 107 | + - name: Fmt |
| 108 | + run: cargo fmt --all -- --check |
0 commit comments