From a183497a0a1132a0b077ed5bc2f00f6aa4a0c61d Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Tue, 24 Dec 2024 11:09:14 -0600 Subject: [PATCH] Include OS version in cache key when using `cargo` for builds If we're running cargo, we need to add the runner name to the cache. Otherwise things that link against system packages, like openssl, can break when we use the same cache across different versions of the runner OS. For example, when going from Ubuntu 20.04 to 22.04, we move from OpenSSL 1.1.x to 3.x. --- Changes.md | 4 ++-- README.md | 5 +++++ action.yml | 7 ++++++- parse-rust-cache-parameters.py | 16 ++++++++++++++-- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Changes.md b/Changes.md index 660399d..4cb4027 100644 --- a/Changes.md +++ b/Changes.md @@ -4,8 +4,8 @@ The addition of caching is a significant behavior change for this action, so the 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. + the `target` parameter as part of the cache key automatically, as well as the OS version when + using `cargo` on Linux. Suggested by @jennydaman (Jennings Zhang). GH #23. - This action now validates its input and will exit early if they are not valid. GH #35. - When compiling for `musl` targets, this action will not try to reinstall the `musl-tools` package if it's already installed. diff --git a/README.md b/README.md index 87708c7..c510515 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,11 @@ value for crates without a `Cargo.lock` file. The `key` parameter passed to this 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. +When running `cargo` on a Linux system, it will also include the output of running +`lsb_release --short --description` in the cache key. This is important for crates that link against +system libraries. If those library versions change across OS versions (e.g. Ubuntu 20.04 to 22.04), +then the cache will be broken for these cases. + Finally, it will run `strip` to strip the binaries it builds 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. diff --git a/action.yml b/action.yml index 303dbd7..fe7650e 100644 --- a/action.yml +++ b/action.yml @@ -133,9 +133,14 @@ runs: set -e set -x set -o pipefail + OS_VERSION="" + if [ -x /usr/bin/lsb_release ]; then + # This will be something like "Ubuntu 22.04.5 LTS" + OS_VERSION="$( lsb_release --short --description )" + fi # 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 }}" + parse-rust-cache-parameters.py "${{ inputs.target }}" "${{ steps.set-build-command.outputs.build-command }}" "$OS_VERSION" env: RUST_CACHE_PARAMETERS: ${{ inputs.rust-cache-parameters }} if: inputs.use-rust-cache == 'true' diff --git a/parse-rust-cache-parameters.py b/parse-rust-cache-parameters.py index 516c73e..bedc822 100755 --- a/parse-rust-cache-parameters.py +++ b/parse-rust-cache-parameters.py @@ -4,11 +4,23 @@ import json import os import sys +target = sys.argv[1] +build_command = sys.argv[2] + +os_version = sys.argv[3] + parameters = json.loads(os.environ["RUST_CACHE_PARAMETERS"]) if "key" not in parameters: - parameters["key"] = sys.argv[1] + parameters["key"] = target else: - parameters["key"] = "{}-{}".format(parameters["key"], sys.argv[1]) + parameters["key"] = "{}-{}".format(parameters["key"], target) + +# If we're running cargo, we need to add the OS version to the cache. Otherwise things that link +# against system packages, like openssl, can break when we use the same cache across different +# versions of the runner OS. For example, when going from Ubuntu 20.04 to 22.04, we move from +# OpenSSL 1.1.x to 3.x. +if build_command == "cargo": + parameters["key"] = "{}-{}".format(parameters["key"], os_version) file = os.environ["GITHUB_OUTPUT"] with open(file, "w") as f: