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.
This commit is contained in:
Dave Rolsky 2024-12-24 11:09:14 -06:00
parent 718071590b
commit a183497a0a
No known key found for this signature in database
4 changed files with 27 additions and 5 deletions

View File

@ -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. 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 - 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 the `target` parameter as part of the cache key automatically, as well as the OS version when
Zhang). GH #23. 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. - 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 - When compiling for `musl` targets, this action will not try to reinstall the `musl-tools` package
if it's already installed. if it's already installed.

View File

@ -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 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. `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 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` is only possible for builds that are not done via `cross`. In addition, Windows builds for `aarch64`
cannot be stripped either. cannot be stripped either.

View File

@ -133,9 +133,14 @@ runs:
set -e set -e
set -x set -x
set -o pipefail 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 # 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. # 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: env:
RUST_CACHE_PARAMETERS: ${{ inputs.rust-cache-parameters }} RUST_CACHE_PARAMETERS: ${{ inputs.rust-cache-parameters }}
if: inputs.use-rust-cache == 'true' if: inputs.use-rust-cache == 'true'

View File

@ -4,11 +4,23 @@ import json
import os import os
import sys import sys
target = sys.argv[1]
build_command = sys.argv[2]
os_version = sys.argv[3]
parameters = json.loads(os.environ["RUST_CACHE_PARAMETERS"]) parameters = json.loads(os.environ["RUST_CACHE_PARAMETERS"])
if "key" not in parameters: if "key" not in parameters:
parameters["key"] = sys.argv[1] parameters["key"] = target
else: 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"] file = os.environ["GITHUB_OUTPUT"]
with open(file, "w") as f: with open(file, "w") as f: