setup-rust-toolchain/README.md

109 lines
5.8 KiB
Markdown
Raw Permalink Normal View History

2022-04-17 21:51:59 +02:00
# Install Rust Toolchain
This GitHub Action installs a Rust toolchain using rustup.
It further integrates into the ecosystem.
Caching for Rust tools and build artifacts is enabled.
Environment variables are set to optimize the cache hits.
[Problem Matchers] are provided for build messages (cargo, clippy) and formatting (rustfmt).
The action is heavily inspired by *dtolnay*'s <https://github.com/dtolnay/rust-toolchain> and extends it with further features.
## Example workflow
```yaml
name: "Test Suite"
on:
push:
pull_request:
jobs:
test:
name: cargo test
runs-on: ubuntu-latest
steps:
2023-10-03 18:55:16 +02:00
- uses: actions/checkout@v4
2022-04-17 21:51:59 +02:00
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: cargo test --all-features
2023-10-31 13:58:53 +01:00
# Check formatting with rustfmt
formatting:
name: cargo fmt
runs-on: ubuntu-latest
steps:
2023-10-03 18:55:16 +02:00
- uses: actions/checkout@v4
# Ensure rustfmt is installed and setup problem matcher
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
components: rustfmt
- name: Rustfmt Check
uses: actions-rust-lang/rustfmt@v1
2022-04-17 21:51:59 +02:00
```
## Inputs
All inputs are optional.
2023-10-31 13:58:53 +01:00
If a [toolchain file](https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file) (i.e., `rust-toolchain` or `rust-toolchain.toml`) is found in the root of the repository and no `toolchain` value is provided, all items specified in the toolchain file will be installed.
If a `toolchain` value is provided, the toolchain file will be ignored.
If no `toolchain` value or toolchain file is present, it will default to `stable`.
2023-05-20 18:03:52 +02:00
First, all items specified in the toolchain file are installed.
Afterward, the `components` and `target` specified via inputs are installed in addition to the items from the toolchain file.
2022-04-17 21:51:59 +02:00
| Name | Description | Default |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------ | ------------- |
| `toolchain` | Comma-separated list of Rustup toolchain specifier e.g. `stable`, `nightly`, `1.42.0`. The last version is the default. | stable |
| `target` | Additional target support to install e.g. `wasm32-unknown-unknown` | |
| `components` | Comma-separated string of additional components to install e.g. `clippy, rustfmt` | |
| `cache` | Automatically configure Rust cache (using [`Swatinem/rust-cache`]) | true |
| `cache-directories` | Propagates the value to [`Swatinem/rust-cache`] | |
| `cache-workspaces` | Propagates the value to [`Swatinem/rust-cache`] | |
| `cache-on-failure` | Propagates the value to [`Swatinem/rust-cache`] | true |
| `cache-key` | Propagates the value to [`Swatinem/rust-cache`] as `key` | |
| `matcher` | Enable problem matcher to surface build messages and formatting issues | true |
| `rustflags` | Set the value of `RUSTFLAGS` (set to empty string to avoid overwriting existing flags) | "-D warnings" |
| `override` | Setup the last installed toolchain as the default via `rustup override` | true |
2023-05-19 12:00:54 +02:00
[`Swatinem/rust-cache`]: https://github.com/Swatinem/rust-cache
2023-05-19 12:00:54 +02:00
### RUSTFLAGS
By default, this action sets the `RUSTFLAGS` environment variable to `-D warnings`.
However, rustflags sources are mutually exclusive, so setting this environment variable omits any configuration through `target.*.rustflags` or `build.rustflags`.
2023-05-19 12:00:54 +02:00
* If `RUSTFLAGS` is already set, no modifications of the variable are made and the original value remains.
* If `RUSTFLAGS` is unset and the `rustflags` input is empty (i.e., the empty string), then it will remain unset.
Use this, if you want to prevent the value from being set because you make use of `target.*.rustflags` or `build.rustflags`.
* Otherwise, the environment variable `RUSTFLAGS` is set to the content of `rustflags`.
2023-05-19 12:00:54 +02:00
To prevent this from happening, set the `rustflags` input to an empty string, which will
prevent the action from setting `RUSTFLAGS` at all, keeping any existing preferences.
You can read more rustflags, and their load order, in the [Cargo reference].
2022-04-17 21:51:59 +02:00
## Outputs
| Name | Description |
| ---------------- | ------------------------------------------- |
| `rustc-version` | Version as reported by `rustc --version` |
| `cargo-version` | Version as reported by `cargo --version` |
| `rustup-version` | Version as reported by `rustup --version` |
| `cachekey` | A short hash of the installed rustc version |
2022-04-17 21:51:59 +02:00
2024-10-21 23:54:51 +02:00
## Dependencies
The action works best on the GitHub-hosted runners, but can work on self-hosted ones too, provided the necessary dependencies are available.
PRs to add support for more environments are welcome.
* bash 5
* brew (macOS only)
* rustup or curl (Linux and macOS)
* using other node actions
2022-04-17 21:51:59 +02:00
## License
The scripts and documentation in this project are released under the [MIT
License].
[MIT License]: LICENSE
[Problem Matchers]: https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md
[Cargo reference]: https://doc.rust-lang.org/cargo/reference/config.html?highlight=unknown#buildrustflags