Skip to content

Commit

Permalink
Include OS version in cache key when using cargo for builds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
autarch committed Dec 24, 2024
1 parent 7180715 commit a183497
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 14 additions & 2 deletions parse-rust-cache-parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
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:
Expand Down

0 comments on commit a183497

Please sign in to comment.