From c0bc579c138d1f4944090c2021f9a7c2e473184b Mon Sep 17 00:00:00 2001 From: chenwj113 Date: Wed, 21 Aug 2024 15:50:42 +0800 Subject: [PATCH] init project --- README.md | 95 +++++++++++++++++++++++++++ action.yml | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++ rust.json | 33 ++++++++++ 3 files changed, 311 insertions(+) create mode 100644 README.md create mode 100644 action.yml create mode 100644 rust.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..e6a6634 --- /dev/null +++ b/README.md @@ -0,0 +1,95 @@ +# 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 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: + - uses: actions/checkout@v4 + - uses: actions-rust-lang/setup-rust-toolchain@v1 + - run: cargo test --all-features + + # Check formatting with rustfmt + formatting: + name: cargo fmt + runs-on: ubuntu-latest + steps: + - 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 +``` + +## Inputs + +All inputs are optional. +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`. +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. + +| Name | Description | Default | +|--------------------|----------------------------------------------------------------------------------------|---------------| +| `toolchain` | Rustup toolchain specifier e.g. `stable`, `nightly`, `1.42.0`. | 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-workspaces` | Propagates the value to [`Swatinem/rust-cache`] | | +| `cache-on-failure` | Propagates the value to [`Swatinem/rust-cache`] | true | +| `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" | + +[`Swatinem/rust-cache`]: https://github.com/Swatinem/rust-cache + +### 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`. + +* 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`. + +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]. + +## 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 | + +## 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 \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..55a7bca --- /dev/null +++ b/action.yml @@ -0,0 +1,183 @@ +name: Setup Rust Toolchain for GitHub CI +description: | + Setup specific Rust versions with caching pre-configured. + It provides problem matchers for cargo and rustfmt issues. + +branding: + icon: "play" + color: "gray-dark" + +# Add option to install directly from rust-toolchain file +# Blocked on rustup support: https://github.com/rust-lang/rustup/issues/2686 +# +# The action is heavily inspired by https://github.com/dtolnay/rust-toolchain +inputs: + toolchain: + description: "Rust toolchain specification -- see https://rust-lang.github.io/rustup/concepts/toolchains.html#toolchain-specification" + required: false + target: + description: "Target triple to install for this toolchain" + required: false + components: + description: "Comma-separated list of components to be additionally installed" + required: false + cache: + description: "Automatically configure Rust cache" + required: false + default: "true" + cache-workspaces: + description: "Paths to multiple Cargo workspaces and their target directories, separated by newlines." + required: false + cache-on-failure: + description: "Also cache on workflow failures" + default: "true" + required: false + matcher: + description: "Enable the Rust problem matcher" + required: false + default: "true" + rustflags: + description: "set RUSTFLAGS environment variable, set to empty string to avoid overwriting build.rustflags" + required: false + default: "-D warnings" + +outputs: + rustc-version: + description: "Version as reported by `rustc --version`" + value: ${{steps.versions.outputs.rustc-version}} + cargo-version: + description: "Version as reported by `cargo --version`" + value: ${{steps.versions.outputs.cargo-version}} + rustup-version: + description: "Version as reported by `rustup --version`" + value: ${{steps.versions.outputs.rustup-version}} + cachekey: + description: A short hash of the rustc version, appropriate for use as a cache key. "20220627a831" + value: ${{steps.versions.outputs.cachekey}} + +runs: + using: composite + steps: + # The later code uses "newer" bash features, which are not available in the ancient bash 3 (from 2014) of macOS + - name: Unbork mac + if: runner.os == 'macOS' + run: | + brew install bash + shell: bash + + - id: flags + env: + targets: ${{inputs.target}} + components: ${{inputs.components}} + shell: bash + run: | + : construct rustup command line + echo "targets=$(for t in ${targets//,/ }; do echo -n ' --target' $t; done)" >> $GITHUB_OUTPUT + echo "components=$(for c in ${components//,/ }; do echo -n ' --component' $c; done)" >> $GITHUB_OUTPUT + echo "downgrade=${{inputs.toolchain == 'nightly' && inputs.components && ' --allow-downgrade' || ''}}" >> $GITHUB_OUTPUT + + # The environment variables always need to be set before the caching action + - name: Setting Environment Variables + env: + NEW_RUSTFLAGS: ${{inputs.rustflags}} + shell: bash + run: | + if [[ ! -v CARGO_INCREMENTAL ]]; then + echo "CARGO_INCREMENTAL=0" >> $GITHUB_ENV + fi + if [[ ! -v CARGO_PROFILE_DEV_DEBUG ]]; then + echo "CARGO_PROFILE_DEV_DEBUG=0" >> $GITHUB_ENV + fi + if [[ ! -v CARGO_TERM_COLOR ]]; then + echo "CARGO_TERM_COLOR=always" >> $GITHUB_ENV + fi + if [[ ! -v RUST_BACKTRACE ]]; then + echo "RUST_BACKTRACE=short" >> $GITHUB_ENV + fi + if [[ ( ! -v RUSTFLAGS ) && $NEW_RUSTFLAGS != "" ]]; then + echo "RUSTFLAGS=$NEW_RUSTFLAGS" >> $GITHUB_ENV + fi + # Enable faster sparse index on nightly + # The value is ignored on stable and causes no problems + # https://internals.rust-lang.org/t/call-for-testing-cargo-sparse-registry/16862 + if [[ ! -v CARGO_UNSTABLE_SPARSE_REGISTRY ]]; then + echo "CARGO_UNSTABLE_SPARSE_REGISTRY=true" >> $GITHUB_ENV + fi + if [[ ! -v CARGO_REGISTRIES_CRATES_IO_PROTOCOL ]]; then + echo "CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse" >> $GITHUB_ENV + fi + + - name: Install Rust Problem Matcher + if: inputs.matcher == 'true' + shell: bash + run: echo "::add-matcher::${{ github.action_path }}/rust.json" + + - name: Install rustup, if needed + if: runner.os != 'Windows' + shell: bash + run: | + if ! command -v rustup &> /dev/null ; then + curl --proto '=https' --tlsv1.2 --retry 10 --retry-connrefused -fsSL "https://sh.rustup.rs" | sh -s -- --default-toolchain none -y + echo "${CARGO_HOME:-$HOME/.cargo}/bin" >> $GITHUB_PATH + fi + + - name: rustup toolchain install ${{inputs.toolchain || 'stable'}} + env: + toolchain: ${{inputs.toolchain}} + targets: ${{inputs.target}} + components: ${{inputs.components}} + shell: bash + run: | + if [[ -z "$toolchain" && ( -f "rust-toolchain" || -f "rust-toolchain.toml" ) ]] + then + # Install the toolchain as specified in the file + # Might break at some point: https://github.com/rust-lang/rustup/issues/1397 + rustup show + if [[ -n $components ]]; then + rustup component add ${components//,/ } + fi + if [[ -n $targets ]]; then + rustup target add ${targets//,/ } + fi + else + if [[ -z "$toolchain" ]] + then + toolchain=stable + fi + rustup toolchain install $toolchain${{steps.flags.outputs.targets}}${{steps.flags.outputs.components}} --profile minimal${{steps.flags.outputs.downgrade}} --no-self-update + rustup override set $toolchain + fi + + - id: versions + name: Print installed versions + shell: bash + run: | + echo "rustc-version=$(rustc --version)" >> $GITHUB_OUTPUT + rustc --version --verbose + echo "cargo-version=$(cargo --version)" >> $GITHUB_OUTPUT + cargo --version --verbose + echo "rustup-version=$(rustup --version)" >> $GITHUB_OUTPUT + rustup --version + + DATE=$(rustc --version --verbose | sed -ne 's/^commit-date: \(20[0-9][0-9]\)-\([01][0-9]\)-\([0-3][0-9]\)$/\1\2\3/p') + HASH=$(rustc --version --verbose | sed -ne 's/^commit-hash: //p') + echo "cachekey=$(echo $DATE$HASH | head -c12)" >> $GITHUB_OUTPUT + + - name: Downgrade registry access protocol when needed + shell: bash + run: | + # Not all versions support setting CARGO_REGISTRIES_CRATES_IO_PROTOCOL + # On versions 1.66, 1.67, and 1.68.0-nightly the value "sparse" is still unstable. + # https://github.com/dtolnay/rust-toolchain/pull/69#discussion_r1107268108 + # If we detect an incompatible value, set it to "git" which is always supported. + if [[ "${{steps.versions.outputs.rustc-version}}" =~ ^rustc\ (1\.6[67]\.|1\.68\.0-nightly) && "${CARGO_REGISTRIES_CRATES_IO_PROTOCOL}" == "sparse" ]]; then + echo "Downgrade cargo registry protocol to git" + echo "CARGO_REGISTRIES_CRATES_IO_PROTOCOL=git" >> $GITHUB_ENV + fi + + - name: Setup Rust Caching + if: inputs.cache == 'true' + uses: Swatinem/rust-cache@v2 + with: + workspaces: ${{inputs.cache-workspaces}} + cache-on-failure: ${{inputs.cache-on-failure}} \ No newline at end of file diff --git a/rust.json b/rust.json new file mode 100644 index 0000000..0b58b7b --- /dev/null +++ b/rust.json @@ -0,0 +1,33 @@ +{ + "problemMatcher": [ + { + "owner": "rustfmt", + "severity": "warning", + "pattern": [ + { + "regexp": "^(Diff in (.+)) at line (\\d+):$", + "message": 1, + "file": 2, + "line": 3 + } + ] + }, + { + "owner": "clippy", + "pattern": [ + { + "regexp": "^(?:\\x1b\\[[\\d;]+m)*(warning|warn|error)(?:\\x1b\\[[\\d;]+m)*(\\[(.*)\\])?(?:\\x1b\\[[\\d;]+m)*:(?:\\x1b\\[[\\d;]+m)* ([^\\x1b]*)(?:\\x1b\\[[\\d;]+m)*$", + "severity": 1, + "message": 4, + "code": 3 + }, + { + "regexp": "^(?:\\x1b\\[[\\d;]+m)*\\s*(?:\\x1b\\[[\\d;]+m)*\\s*--> (?:\\x1b\\[[\\d;]+m)*(.*):(\\d*):(\\d*)(?:\\x1b\\[[\\d;]+m)*$", + "file": 1, + "line": 2, + "column": 3 + } + ] + } + ] +} \ No newline at end of file