Skip to content

Instantly share code, notes, and snippets.

@LukeMathWalker
Last active March 29, 2024 16:13
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save LukeMathWalker/6153b07c4528ca1db416f24b09038fca to your computer and use it in GitHub Desktop.
Save LukeMathWalker/6153b07c4528ca1db416f24b09038fca to your computer and use it in GitHub Desktop.
CircleCI - Rust setup
version: 2
jobs:
build-and-test:
docker:
- image: cimg/rust:1.69
environment:
# Fail the build if there are warnings
RUSTFLAGS: '-D warnings'
steps:
- checkout
- run:
name: Version information
command: rustc --version; cargo --version; rustup --version
# If you have committed your Cargo.lock file to version control
# delete this step.
- run:
name: Calculate dependencies
command: cargo generate-lockfile
- restore_cache:
keys:
- v1-cargo-cache-{{ arch }}-{{ checksum "Cargo.lock" }}
- run:
name: Build all targets
command: cargo build
- save_cache:
paths:
- /home/circleci/.cargo/registry
- target/debug/.fingerprint
- target/debug/build
- target/debug/deps
key: v1-cargo-cache-{{ arch }}-{{ checksum "Cargo.lock" }}
- run:
name: Run all tests
command: cargo test
security:
docker:
- image: cimg/rust:1.69
steps:
- checkout
- run:
name: Version information
command: rustc --version; cargo --version; rustup --version
- run:
name: Cache permission
command: |
sudo chown -R $(whoami):$(id -ng) /home/circleci/.cargo/bin/cargo
- restore_cache:
keys:
- v1-cargo-audit-{{ arch }}
- run:
name: Install dependency auditing tool
command: cargo install cargo-audit
- save_cache:
paths:
- /home/circleci/.cargo/bin/cargo
key: v1-cargo-audit-{{ arch }}
- run:
name: Check for known security issues in dependencies
command: cargo audit
format:
docker:
- image: cimg/rust:1.69
steps:
- checkout
- run:
name: Version information
command: rustc --version; cargo --version; rustup --version
- run:
name: Install formatter
command: rustup component add rustfmt
- run:
name: Formatting
command: cargo fmt --all -- --check
lint:
docker:
- image: cimg/rust:1.69
steps:
- checkout
- run:
name: Version information
command: rustc --version; cargo --version; rustup --version
- run:
name: Install Clippy
command: rustup component add clippy
- run:
name: Linting
command: cargo clippy -- -D warnings
coverage:
machine: true
steps:
- checkout
- run:
name: Coverage with docker
command: docker run --security-opt seccomp=unconfined -v "${PWD}:/volume" xd009642/tarpaulin cargo tarpaulin --ignore-tests
workflows:
version: 2
build-test:
jobs:
- build-and-test:
filters:
tags:
only: /.*/
- security:
filters:
tags:
only: /.*/
- format:
filters:
tags:
only: /.*/
- lint:
filters:
tags:
only: /.*/
- coverage:
filters:
tags:
only: /.*/
@BurntNail
Copy link

Hey LMW,

I’m reading z2p right now and loving it! One quick question - is there an updated version of this for when sqlx is added?

@LukeMathWalker
Copy link
Author

No, there isn't! There is only the GitHub Actions version for the follow-up.

@FabijanC
Copy link

FabijanC commented Nov 27, 2023

Thanks for sharing this, found it useful. It was my 4th highest result of googling circleci caching rust.

A few notes if anyone else ever reads this:

  • Since our project is using cargo build --release in one step, we are caching directories in both target/debug/ and target/release/.

  • I considered saving the cache after the testing step, but IMO that just took too long to upload the cache.

  • To avoid having the value of the caching key specified in multiple places (i.e. checksum "Cargo.lock"), I relied on one of CircleCI's reusability mechanisms (namely the parameters one).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment