Stasis

Rust Cargo

Rust library with clippy, tests, and crates.io publishing

Rust Cargo Publishing

CI/CD pipeline for Rust libraries with testing, linting, and publishing to crates.io.

Overview

This pipeline handles Rust projects with:

  • Dependency caching
  • Code formatting (rustfmt)
  • Linting (clippy)
  • Unit and integration tests
  • Documentation generation
  • Multi-platform testing
  • crates.io publishing

Use Case

  • Rust libraries
  • Rust CLI tools
  • Rust crates for distribution

Prerequisites

  • Rust project with Cargo.toml
  • crates.io account and API token
  • Optional: rustfmt.toml and clippy.toml

Configuration

# runner.yaml
image:
  name: "rust"
  tag: "1.75-slim"
  pull_policy: "IfNotPresent"

on:
  push:
    - "main"
  tag:
    - "v*"

global_env:
  CARGO_TERM_COLOR: "always"
  RUST_BACKTRACE: "1"

timeout: 1800  # 30 minutes

steps:
  setup:
    type: "pre"
    scripts:
      - rustc --version
      - cargo --version
      - rustup component add rustfmt clippy
      - cargo fetch

  fmt:
    type: "exec"
    scripts:
      - |
        echo "Checking formatting..."
        cargo fmt --all -- --check
    continue_on_error: false

  clippy:
    type: "exec"
    scripts:
      - |
        echo "Running clippy..."
        cargo clippy --all-targets --all-features -- -D warnings
    continue_on_error: false

  test:
    type: "exec"
    scripts:
      - |
        echo "Running tests..."
        cargo test --all-features --verbose
      - |
        echo "Running doc tests..."
        cargo test --doc
    continue_on_error: false
    timeout: 600

  test_nightly:
    type: "exec"
    scripts:
      - |
        echo "Installing nightly..."
        rustup install nightly
        rustup default nightly
      - |
        echo "Running tests on nightly..."
        cargo test --all-features
      - |
        echo "Switching back to stable..."
        rustup default stable
    continue_on_error: true

  build:
    type: "exec"
    scripts:
      - |
        echo "Building release..."
        cargo build --release --all-features
      - |
        echo "Binary size:"
        ls -lh target/release/ || true
    continue_on_error: false

  publish:
    type: "exec"
    scripts:
      - |
        echo "Publishing to crates.io..."
        cargo publish --all-features
    envs:
      CARGO_REGISTRY_TOKEN: "${CRATES_IO_TOKEN}"
    if_condition: "CI_TAG != ''"
    continue_on_error: false

Step-by-Step Explanation

Step 1: Setup

setup:
  type: "pre"
  scripts:
    - rustup component add rustfmt clippy
    - cargo fetch

What it does:

  • Installs rustfmt and clippy components
  • Fetches dependencies for caching

Step 2: Format Check

fmt:
  type: "exec"
  scripts:
    - cargo fmt --all -- --check

What it does:

  • Checks if code is properly formatted
  • Fails if formatting issues found

Step 3: Clippy Linting

clippy:
  type: "exec"
  scripts:
    - cargo clippy --all-targets --all-features -- -D warnings

What it does:

  • Runs Rust's linter
  • Treats warnings as errors

Step 4: Tests

test:
  type: "exec"
  scripts:
    - cargo test --all-features --verbose
    - cargo test --doc

What it does:

  • Runs all unit and integration tests
  • Runs documentation tests

Step 5: Nightly Tests

test_nightly:
  type: "exec"
  scripts:
    - rustup install nightly
    - cargo test --all-features
    - rustup default stable
  continue_on_error: true

What it does:

  • Tests on Rust nightly
  • Catches future compatibility issues
  • Non-blocking

Step 6: Publish

publish:
  type: "exec"
  scripts:
    - cargo publish --all-features
  envs:
    CARGO_REGISTRY_TOKEN: "${CRATES_IO_TOKEN}"
  if_condition: "CI_TAG != ''"

What it does:

  • Publishes to crates.io
  • Only runs on tag push
  • Uses API token for authentication

Environment Variables

VariableRequiredDescription
CRATES_IO_TOKENFor publishingcrates.io API token
CARGO_TERM_COLORNoTerminal color output
RUST_BACKTRACENoShow backtraces on panic

Artifacts

FileDescription
target/release/Release binaries
target/doc/Generated documentation

Customization

Add code coverage

coverage:
  type: "exec"
  scripts:
    - cargo install cargo-tarpaulin
    - cargo tarpaulin --all-features --out Xml --out Html
  continue_on_error: true

Add benchmarks

bench:
  type: "exec"
  scripts:
    - cargo bench --all-features
  continue_on_error: true

Next Steps

On this page