From 7ad7c57bed040be30fcea406c16eeacf7f8e842c Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Sat, 14 Dec 2024 13:42:02 -0600 Subject: [PATCH] Add support for `Swatinem/rust-cache@v2` --- Changes.md | 9 +++++++++ README.md | 35 ++++++++++++++++++---------------- action.yml | 26 +++++++++++++++++++++++++ parse-rust-cache-parameters.py | 16 ++++++++++++++++ 4 files changed, 70 insertions(+), 16 deletions(-) create mode 100755 parse-rust-cache-parameters.py diff --git a/Changes.md b/Changes.md index b9094c0..977c851 100644 --- a/Changes.md +++ b/Changes.md @@ -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 diff --git a/README.md b/README.md index e231e87..6c90bb1 100644 --- a/README.md +++ b/README.md @@ -67,16 +67,18 @@ 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 (`.`). | -| `toolchain` | string (one of `stable`, `beta`, or `nightly`) | no | The Rust toolchain version to install. The default is `stable`. | -| `GITHUB_TOKEN` | string | no | Defaults to the value of `${{ github.token }}`. | -| `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. | +| 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 (`.`). | +| `toolchain` | string (one of `stable`, `beta`, or `nightly`) | no | The Rust toolchain version to install. The default is `stable`. | +| `GITHUB_TOKEN` | string | no | Defaults to the value of `${{ github.token }}`. | +| `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. diff --git a/action.yml b/action.yml index ef29c70..e08386d 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/parse-rust-cache-parameters.py b/parse-rust-cache-parameters.py new file mode 100755 index 0000000..516c73e --- /dev/null +++ b/parse-rust-cache-parameters.py @@ -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}")