Add support for Swatinem/rust-cache@v2

This commit is contained in:
Dave Rolsky 2024-12-14 13:42:02 -06:00
parent 9ea5352c0f
commit 7ad7c57bed
No known key found for this signature in database
4 changed files with 70 additions and 16 deletions

View File

@ -1,3 +1,12 @@
## 1.0.0-beta1 - 2024-12-21
The addition of caching is a significant behavior change for this action, so the version has been
bumped to v1.0.0 because of this change.
- This action will now configure and use `Swatinem/rust-cache` by default for you. It will include
the `target` parameter as part of the cache key automatically. Suggested by @jennydaman (Jennings
Zhang). GH #23.
## 0.0.17 - 2024-11-23
- Added support for running `cargo bench` or `cross bench`. Implemented by @RaulTrombin (Raul Victor

View File

@ -68,7 +68,7 @@ Ubuntu x86-64, Windows, and macOS runners.
This action takes the following parameters:
| Key | Type | Required? | Description |
| ------------------- | ---------------------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ----------------------- | ---------------------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `command` | string (one of `build`, `test`, or `both`) | no | The command(s) to run. The default is `build`. Running the `test` command will fail with \*BSD targets and non-x86 Windows. |
| `target` | string | yes | The target triple to compile for. This should be one of the targets found by running `rustup target list`. |
| `working-directory` | string | no | The working directory in which to run the `cargo` or `cross` commands. Defaults to the current directory (`.`). |
@ -77,6 +77,8 @@ This action takes the following parameters:
| `args` | string | no | A string-separated list of arguments to be passed to `cross build`, like `--release --locked`. |
| `strip` | boolean (`true` or `false`) | no | If this is true, then the resulting binaries will be stripped if possible. This is only possible for binaries which weren't cross-compiled. |
| `cross-version` | string | no | This can be used to set the version of `cross` to use. If specified, it should be a specific `cross` release tag (like `v0.2.3`) or a git ref (commit hash, `HEAD`, etc.). If this is not set then the latest released version will always be used. If this is set to a git ref then the version corresponding to that ref will be installed. |
| `use-rust-cache` | boolean | no | Whether or not to use [the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache). This defaults to true. |
| `rust-cache-parameters` | string (containing JSON) | no | This must be a string containing valid JSON. The JSON should be an object where the keys are the parameters for [the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache). |
## How it Works
@ -94,12 +96,13 @@ build `cross`.
When compiling on Windows, it will do so in a Powershell environment, which can matter in some
corner cases, like compiling the `openssl` crate with the `vendored` feature.
By default, it will use
[the `Swatinem/rust-cache@v2` action](https://github.com/Swatinem/rust-cache) to cache compiled
dependencies for this crate. Note that per the documentation for this action, it has fairly limited
value for crates without a `Cargo.lock` file. The `key` parameter passed to this action will always
include the value of the `target` input. If you specify a `key` parameter in
`rust-cache-parameters`, then the `target` input will be appended to the value you specify.
Finally, it will run `strip` to strip the binaries if the `strip` parameter is true. This is only
possible for builds that are not done via `cross`. In addition, Windows builds for `aarch64` cannot
be stripped either.
## Caching Rust Compilation Output
You can use the [Swatinem/rust-cache](https://github.com/Swatinem/rust-cache) action with this one
seamlessly, whether or not a specific build target needs `cross`. There is no special configuration
that you need for this. It just works.

View File

@ -41,6 +41,15 @@ inputs:
Cache the cross binary if one is installed. This is primarily for use in
tests of this action.
default: true
use-rust-cache:
description: |
Use `Swatinem/rust-cache@v2`. Defaults to true.
default: true
rust-cache-parameters:
description: |
A JSON string containing parameters to pass to `Swatinem/rust-cache@v2`. You can use the
`toJSON()` function in your action to make passing this easier.
default: "{}"
runs:
using: composite
steps:
@ -95,6 +104,23 @@ runs:
id: determine-cargo-commands
shell: bash
run: determine-cargo-commands.sh ${{ inputs.command }}
- name: Parse `rust-cache-parameters` and set inputs for `Swatinem/rust-cache@v2`
id: parse-rust-cache-parameters
shell: bash
run: |
set -e
set -x
set -o pipefail
# This will get the inputs JSON from the `RUST_CACHE_PARAMETERS` env var. This avoids
# any string interpolation issues, since the inputs will contain quotes.
parse-rust-cache-parameters.py "${{ inputs.target }}"
env:
RUST_CACHE_PARAMETERS: ${{ inputs.rust-cache-parameters }}
if: inputs.use-rust-cache == 'true'
- name: Cache cargo & target directories
uses: Swatinem/rust-cache@v2
with: ${{ steps.parse-rust-cache-parameters.outputs }}
if: inputs.use-rust-cache == 'true'
- name: Run tests (*nix)
working-directory: ${{ inputs.working-directory }}
shell: bash

16
parse-rust-cache-parameters.py Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env python3
import json
import os
import sys
parameters = json.loads(os.environ["RUST_CACHE_PARAMETERS"])
if "key" not in parameters:
parameters["key"] = sys.argv[1]
else:
parameters["key"] = "{}-{}".format(parameters["key"], sys.argv[1])
file = os.environ["GITHUB_OUTPUT"]
with open(file, "w") as f:
for key, value in parameters.items():
f.write(f"{key}={value}")