Example of Rust project
Purpose
This repository contains an example of Rust project structure.
Most of the components can be reusable in your own projects:
- Project configuration
- CI workflow to build, lint, test, check test coverage and run mutation tests
- CD workflow to test on all platforms and publish on crates.io
Github Actions workflows
Jobs
The CI workflow is triggered for any commit or pull request on the main branch and runs the following jobs:
- Test the crate
- Build the project for all targets and with default features to ensure everything compiles
- Run unit, integration and doc tests with default features
- Run test coverage and mutation tests
- Run source-based coverage with grcov
- Upload HTML coverage report in workflow artifacts
- Upload HTML coverage report on Codecov
- Fail if the coverage threshold is not reached
- Run mutation tests with mutagen (note that the job automatically adds
#[mutate]annotations) - Fail if the mutation threshold is not reached
- Run several code checks
- Run
clippy - Run
rustfmtto check code is correctly formatted - Run
cargo-denyto check dependencies - Check encoding of all files is UTF-8
- Check all line endings are LF
- Check there is no TODO left in Rust code
- Run
This workflow is only run on Ubuntu virtual environment.
The CD workflow is triggered manually and runs the following jobs:
- Check version to release in
Cargo.tomlfiles - Test on Ubuntu
- Test on Windows
- Test on MacOS
- Make a publication dry run on crates.io
- Publish on crates.io
- Create the release and prepare the next one in the repository
- Update the latest version and its release date in the
CHANGELOG.mdfile and push the changes - Tag the created commit with the name of the released version
- Create a GitHub release from the created tag and the content of the
CHANGELOG.mdfile - Create a new section for the next release in the
CHANGELOG.mdfile and push the changes
- Update the latest version and its release date in the
Secrets
The CD workflow requires the following secrets:
CRATES_IO_TOKEN: token to publish the crate(s) on crates.ioGIT_TOKEN: GitHub personal access token to allow pushing changes as administrator (if themainbranch is protected, "Include administrators" must be unchecked in settings)
These secrets must be stored in a repository environment called Deployment.
Settings
Settings of the CI workflow can be modified in the file .github/workflows/ci.yml, in the env section:
RUST_VERSION_STABLE: version of the stable Rust compiler to useRUST_VERSION_NIGHTLY: version of the nightly Rust compiler to use when requiredMUTAGEN_COMMIT: commit of the mutagen version to install (from mutagen repository)COV_THRESHOLD: minimum threshold the coverage must reached to succeed the jobMUTAGEN_THRESHOLD: minimum threshold the mutation tests must reached to succeed the jobCRATE_PATHS: package names separated by;in publication order if the repository is a Cargo workspace, else.
Settings of the CD workflow can be modified in the file .github/workflows/cd.yml, in the env section:
RUST_VERSION_STABLE: version of the stable Rust compiler to useCRATE_PATHS: package names separated by;in publication order if the repository is a Cargo workspace, else.
Run locally
act can be used to run the workflows on your own machine.
Once installed, run act -P ubuntu-18.04=nektos/act-environments-ubuntu:18.04 in the repository folder to run all supported jobs.
Workspaces
This repository contains a single crate, but the workflows should also work with a Cargo workspace.
Adaptations for another project
This project example is made to be reused. If you are interested in using this template, you can copy the files in your own project and:
- adapt the settings of the CI workflow in the file
.github/workflows/ci.yml, in theenvsection - adapt the settings of the CD workflow in the file
.github/workflows/cd.yml, in theenvsection - edit the
.lints,deny.toml,rustfmt.toml,Cargo.tomlandREADME.mdfiles as wanted - edit the issue templates in
.github/ISSUE_TEMPLATEas wanted - reset the
CHANGELOG.mdfile (make sure there is at least a section## [Unreleased] - yyyy-mm-dd) - change the copyright notices in the
LICENSE-MITandLICENSE-APACHEfiles if you keep these licenses - in the repository settings, create a GitHub environment named
Deploymentwith theCRATES_IO_TOKENandGIT_TOKENsecrets - if the
mainbranch is configured as protected, make sureInclude administratorsis unchecked in the configuration
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.