name: Rust on: push: branches: - main pull_request: types: [opened, synchronize, reopened] branches: - main env: CARGO_TERM_COLOR: always SQLX_VERSION: 0.8.6 SQLX_FEATURES: "rustls,postgres" DATABASE_URL: postgres://postgres:password@postgres:5432/newsletter APP_DATABASE__HOST: postgres APP_KV_STORE__HOST: redis jobs: # This job builds and caches artifacts for other jobs to use build: name: Build runs-on: ubuntu-latest steps: - name: Check out repository code uses: actions/checkout@v4 - name: Install mold linker run: | sudo apt-get update sudo apt-get install -y mold clang # LONG-TERM CACHE: Cargo registry (shared across workflows) - name: Cache cargo registry uses: actions/cache@v3 with: path: | ~/.cargo/registry/index ~/.cargo/registry/cache ~/.cargo/git/db key: cargo-registry-${{ hashFiles('**/Cargo.lock') }} restore-keys: | cargo-registry- # LONG-TERM CACHE: sqlx-cli binary (shared across workflows) - name: Cache sqlx-cli id: cache-sqlx uses: actions/cache@v3 with: path: | ~/.cargo/bin/sqlx ~/.cargo/bin/cargo-sqlx key: sqlx-cli-${{ env.SQLX_VERSION }} # LONG-TERM CACHE: Rust toolchain (shared across workflows) # Note: Keep cache: false if it times out, the toolchain itself is cached by rustup - name: Install the Rust toolchain uses: actions-rust-lang/setup-rust-toolchain@v1 with: cache: false - name: Install sqlx-cli if: steps.cache-sqlx.outputs.cache-hit != 'true' run: cargo install sqlx-cli --version=${{ env.SQLX_VERSION }} --features ${{ env.SQLX_FEATURES }} --no-default-features --locked # Build the project - name: Build run: cargo build --release # WORKFLOW-ONLY CACHE: Upload build artifacts for other jobs - name: Upload build artifacts uses: actions/upload-artifact@v3 with: name: build-artifacts path: | target/ ~/.cargo/bin/ test: name: Test runs-on: ubuntu-latest needs: build # Wait for build to complete services: postgres: image: postgres env: POSTGRES_USER: postgres POSTGRES_PASSWORD: password POSTGRES_DB: newsletter ports: - 15432:5432 redis: image: redis ports: - 16379:6379 steps: - name: Check out repository code uses: actions/checkout@v4 - name: Install mold linker run: | sudo apt-get update sudo apt-get install -y mold clang # Download build artifacts from the build job - name: Download build artifacts uses: actions/download-artifact@v3 with: name: build-artifacts - name: Install the Rust toolchain uses: actions-rust-lang/setup-rust-toolchain@v1 with: cache: false - name: Migrate database run: cargo sqlx migrate run - name: Run tests run: TEST_LOG=true cargo test - name: Check that queries are fresh run: cargo sqlx prepare --check --workspace fmt: name: Rustfmt runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 # LONG-TERM CACHE: Cargo registry - name: Cache cargo registry uses: actions/cache@v3 with: path: | ~/.cargo/registry/index ~/.cargo/registry/cache ~/.cargo/git/db key: cargo-registry-${{ hashFiles('**/Cargo.lock') }} restore-keys: | cargo-registry- - name: Install the Rust toolchain uses: actions-rust-lang/setup-rust-toolchain@v1 with: components: rustfmt cache: false - name: Enforce formatting run: cargo fmt --check clippy: name: Clippy runs-on: ubuntu-latest needs: build # Use artifacts from build env: SQLX_OFFLINE: true steps: - name: Check out repository code uses: actions/checkout@v4 - name: Install mold linker run: | sudo apt-get update sudo apt-get install -y mold clang # Download build artifacts - name: Download build artifacts uses: actions/download-artifact@v3 with: name: build-artifacts - name: Install the Rust toolchain uses: actions-rust-lang/setup-rust-toolchain@v1 with: components: clippy cache: false - name: Linting run: cargo clippy -- -D warnings coverage: name: Code coverage runs-on: ubuntu-latest services: postgres: image: postgres env: POSTGRES_USER: postgres POSTGRES_PASSWORD: password POSTGRES_DB: newsletter ports: - 15432:5432 redis: image: redis ports: - 16379:6379 steps: - uses: actions/checkout@v4 - name: Install mold linker run: | sudo apt-get update sudo apt-get install -y mold clang # LONG-TERM CACHE: Cargo registry - name: Cache cargo registry uses: actions/cache@v3 with: path: | ~/.cargo/registry/index ~/.cargo/registry/cache ~/.cargo/git/db key: cargo-registry-${{ hashFiles('**/Cargo.lock') }} restore-keys: | cargo-registry- # LONG-TERM CACHE: sqlx-cli - name: Cache sqlx-cli id: cache-sqlx-cov uses: actions/cache@v3 with: path: | ~/.cargo/bin/sqlx ~/.cargo/bin/cargo-sqlx key: sqlx-cli-${{ env.SQLX_VERSION }} # LONG-TERM CACHE: cargo-llvm-cov - name: Cache cargo-llvm-cov id: cache-llvm-cov uses: actions/cache@v3 with: path: ~/.cargo/bin/cargo-llvm-cov key: cargo-llvm-cov-0.6.0 # Pin to a version - name: Install the Rust toolchain uses: actions-rust-lang/setup-rust-toolchain@v1 with: components: llvm-tools-preview cache: false - name: Install sqlx-cli if: steps.cache-sqlx-cov.outputs.cache-hit != 'true' run: cargo install sqlx-cli --version=${{ env.SQLX_VERSION }} --features ${{ env.SQLX_FEATURES }} --no-default-features --locked - name: Migrate database run: cargo sqlx migrate run - name: Install cargo-llvm-cov if: steps.cache-llvm-cov.outputs.cache-hit != 'true' uses: taiki-e/install-action@cargo-llvm-cov - name: Generate code coverage run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info - name: Generate report run: cargo llvm-cov report --html --output-dir coverage