Last active
June 29, 2024 12:20
-
-
Save LukeMathWalker/6153b07c4528ca1db416f24b09038fca to your computer and use it in GitHub Desktop.
CircleCI - Rust setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: /.*/ | |
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 bothtarget/debug/
andtarget/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
No, there isn't! There is only the GitHub Actions version for the follow-up.