name:Rust CI/CD Pipelineon:push:branches:["main"]pull_request:branches:["main"]env:CARGO_TERM_COLOR:alwaysRUST_BACKTRACE:1jobs:# Job 1: Format and Lintformat-lint:runs-on:ubuntu-lateststeps:- uses:actions/checkout@v4- uses:actions-rs/toolchain@v1with:toolchain:nightlycomponents:rustfmt, clippyoverride:true- name:Format checkrun:cargo +nightly fmt -- --check- name:Clippyrun:>- cargo clippy --all-targets --all-features --workspace
-- -D warnings# Job 2: Build (original)build:runs-on:ubuntu-lateststeps:- uses:actions/checkout@v4- uses:actions-rs/toolchain@v1with:toolchain:stableoverride:true- name:Build releaserun:cargo build --release- name:Upload artifactsuses:actions/upload-artifact@v4with:name:binarypath:target/release/smarthouse# NEW JOB 3: Security Audit (Cargo Audit)# Checks if any dependencies have known vulnerabilitiessecurity-audit:runs-on:ubuntu-lateststeps:- uses:actions/checkout@v4# Install and run cargo-audit- name:Install cargo-auditrun:cargo install cargo-audit- name:Run security auditrun:cargo audit# This will fail the build if any vulnerabilities found# Exit code 1 if vulnerabilities detected# NEW JOB 4: Property-based Testing (Proptest)# Runs extended tests with randomly generated dataproptest:runs-on:ubuntu-lateststeps:- uses:actions/checkout@v4- uses:actions-rs/toolchain@v1with:toolchain:stableoverride:true# Run tests including proptest (slower than regular tests)- name:Run property-based testsrun:cargo test --release --lib -- --include-ignored proptest# --include-ignored runs proptest tests marked with #[ignore]# Proptest generates thousands of random inputs to find edge cases# Alternative: Run all tests with proptest enabled (no ignore)- name:Run all tests including proptest (alternative)run:cargo test --release -- --nocapture# This runs ALL tests, including proptest tests# Remove this if you prefer the filtered version above# NEW JOB 5: Unsafe Code Verification (Miri)# Checks for Undefined Behavior in unsafe Rust code# WARNING: Miri is VERY slow (100-1000x) but catches critical bugsmiri-check:runs-on:ubuntu-latest# Only run if you actually have unsafe code# Add a condition to avoid slow checks on simple projectssteps:- uses:actions/checkout@v4- uses:actions-rs/toolchain@v1with:toolchain:nightly # Miri requires nightly Rustcomponents:mirioverride:true# Install Miri component- name:Install Mirirun:| rustup +nightly component add miri
cargo +nightly miri setup# Run Miri on tests (catches UB in unsafe code)- name:Run Miri on testsrun:cargo +nightly miri test# This will find:# - Invalid memory access# - Data races in unsafe code# - Violations of Rust's aliasing rules# - Using uninitialized memory# Optional: Run Miri on the main binary- name:Run Miri on binary (optional)run:cargo +nightly miri run# Check if the main program has UBcontinue-on-error:true# Don't fail the build if this fails# Miri is extremely slow, so this might timeout in CI# NEW JOB 6: Memory Analysis (Valgrind)# Finds memory leaks and invalid memory access in compiled binary# NOTE: Only useful if you have C/C++ dependencies or complex unsafe codevalgrind-check:runs-on:ubuntu-latest# Only run on main branch (Valgrind is slow)if:github.ref == 'refs/heads/main'steps:- uses:actions/checkout@v4- uses:actions-rs/toolchain@v1with:toolchain:stableoverride:true# Build with debug symbols for better Valgrind output- name:Build with debug symbolsrun:cargo build# Install Valgrind- name:Install Valgrindrun:sudo apt-get update && sudo apt-get install -y valgrind# Run Valgrind on tests- name:Run Valgrind memory checkrun:| # Run each test binary through Valgrind
for test in target/debug/deps/*; do
if [ -x "$test" ] && [ ! -d "$test" ]; then
valgrind --leak-check=full \
--error-exitcode=1 \
--suppressions=valgrind.supp \
"$test" 2>&1 | tee -a valgrind.log
fi
done# This finds:# - Memory leaks (Rust usually doesn't have them)# - Invalid reads/writes in C/C++ dependencies# - Use of uninitialized memorycontinue-on-error:true# Don't fail the whole pipeline# Alternative: Run just the main binary- name:Run Valgrind on main binary (alternative)run:| valgrind --leak-check=full \
--error-exitcode=1 \
target/debug/smarthouse# Replace 'smarthouse' with your actual binary namecontinue-on-error:true# NEW JOB 7: Coverage Report (Optional)# Shows which parts of code are tested (complements Miri/Proptest)coverage:runs-on:ubuntu-lateststeps:- uses:actions/checkout@v4- name:Install cargo-tarpaulinrun:cargo install cargo-tarpaulin- name:Generate coverage reportrun:cargo tarpaulin --out Html --output-dir ./coverage- name:Upload coverage reportuses:actions/upload-artifact@v4with:name:coverage-reportpath:./coverage
Оптимизация CI для скорости
# Only run expensive jobs when unsafe code changesmiri-check:if:contains(github.event.pull_request.labels.*.name, 'unsafe-changes')# Cache dependencies to speed up builds- name:Cache cargo registryuses:actions/cache@v3with:path:~/.cargo/registrykey:${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}