Skip to content

Commit

Permalink
Add support for adding to cache key
Browse files Browse the repository at this point in the history
When using this action in multiple jobs in the same workflow, the
generated cache key is the same for all of them. This means that all
apart from the first job are unable to save the cache, and subsequent
runs might restore the wrong cache.

The `Swatinem/rust-cache` action which we use for caching has a `key`
input which it puts in its cache key. (It doesn't override the key, just
adds to it.) Providing this as an input here will allow us to generate a
unique cache key for each job.
  • Loading branch information
iainlane committed Jul 12, 2024
1 parent 1fbea72 commit 9ed8cf5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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.
The action is heavily inspired by _dtolnay_'s <https://github.com/dtolnay/rust-toolchain> and extends it with further features.

## Example workflow

Expand Down Expand Up @@ -49,13 +49,14 @@ 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 |
| `key` | Propagates the value to [`Swatinem/rust-cache`] | |
| `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" |

Expand All @@ -66,10 +67,10 @@ Afterward, the `components` and `target` specified via inputs are installed in a
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`.
- 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.
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ inputs:
description: "Also cache on workflow failures"
default: "true"
required: false
key:
description: "An additional cache key that is added alongside the automatic `job`-based cache key and can be used to further differentiate jobs."
required: false
matcher:
description: "Enable the Rust problem matcher"
required: false
Expand Down Expand Up @@ -181,3 +184,4 @@ runs:
with:
workspaces: ${{inputs.cache-workspaces}}
cache-on-failure: ${{inputs.cache-on-failure}}
key: ${{inputs.key}}

0 comments on commit 9ed8cf5

Please sign in to comment.