From b1e766fe8f74c2c0895adcfd8d21bd5c565a6f7a Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Sat, 22 Jul 2023 09:43:41 -0500 Subject: [PATCH] Add caching of the installed cross binary --- Changes.md | 6 ++++++ action.yml | 27 ++++++++++++++++++++++++--- determine-latest-cross-version.sh | 11 +++++++++++ install-cross-nix.sh | 4 +++- set-build-command.sh | 5 +++-- set-cross-dir.sh | 4 ++++ 6 files changed, 51 insertions(+), 6 deletions(-) create mode 100755 determine-latest-cross-version.sh create mode 100755 set-cross-dir.sh diff --git a/Changes.md b/Changes.md index 432ba8e..94587ae 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,9 @@ +## 0.0.8 + +- For builds that need the `cross` binary, this binary is now cached. A cache + hit saves about 20 seconds in my tests. Suggested by @timon-schelling. GH + #4. + ## 0.0.7 - 2023-04-21 - The toolchain argument was (probably) not being respected with cross builds, diff --git a/action.yml b/action.yml index 45b38e0..9389b96 100644 --- a/action.yml +++ b/action.yml @@ -44,16 +44,37 @@ runs: with: targets: ${{ inputs.target }} toolchain: ${{ inputs.toolchain }} + - name: Determine latest cross version + id: determine-latest-cross-version + shell: bash + run: determine-latest-cross-version.sh + env: + GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }} + if: ${{ steps.determine-cross-compile.outputs.needs-cross == 'true' }} + # We need to accesss this in both this YAML config and shell scripts. It + # doesn't seem like using ${{ env.RUNNER_TEMP }} works in the YAML config. + - name: Set directory for installing cross + id: set-cross-dir + shell: bash + run: set-cross-dir.sh + if: ${{ steps.determine-cross-compile.outputs.needs-cross == 'true' }} + - name: Cache cross + id: cache-cross + uses: actions/cache@v3 + with: + path: ${{ steps.set-cross-dir.outputs.cross-dir }}/cross + key: ${{ runner.os }}-${{ steps.determine-latest-cross-version.outputs.cross-version }} + if: ${{ steps.determine-cross-compile.outputs.needs-cross == 'true' }} - name: Install cross if cross-compiling (*nix) shell: bash - run: install-cross-nix.sh - if: ${{ steps.determine-cross-compile.outputs.needs-cross == 'true' }} + run: install-cross-nix.sh ${{ steps.set-cross-dir.outputs.cross-dir }} + if: ${{ steps.determine-cross-compile.outputs.needs-cross == 'true' && steps.cache-cross.outputs.cache-hit != 'true' }} env: GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }} - name: Set build command id: set-build-command shell: bash - run: set-build-command.sh + run: set-build-command.sh ${{ steps.set-cross-dir.outputs.cross-dir }} - name: Run tests (*nix) shell: bash run: | diff --git a/determine-latest-cross-version.sh b/determine-latest-cross-version.sh new file mode 100755 index 0000000..90278de --- /dev/null +++ b/determine-latest-cross-version.sh @@ -0,0 +1,11 @@ +set -e +set -x +set -o pipefail + +JSON=$( curl \ + --request GET \ + --header "Authorization: Bearer $GITHUB_TOKEN" \ + https://api.github.com/repos/cross-rs/cross/releases/latest ) +VERSION=$( echo "$JSON" | jq -r ".tag_name") + +echo "cross-version=$VERSION" >> $GITHUB_OUTPUT diff --git a/install-cross-nix.sh b/install-cross-nix.sh index d7f52c6..fb1dc86 100755 --- a/install-cross-nix.sh +++ b/install-cross-nix.sh @@ -1,7 +1,9 @@ set -e set -x +set -o pipefail -cd "$RUNNER_TEMP" +CROSS_DIR="$1" +cd "$CROSS_DIR" export TARGET=. curl --silent --location \ https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh | diff --git a/set-build-command.sh b/set-build-command.sh index 9b471a6..953cb6c 100755 --- a/set-build-command.sh +++ b/set-build-command.sh @@ -1,8 +1,9 @@ set -e set -x -if [ -f "$RUNNER_TEMP/cross" ]; then - echo "build-command=$RUNNER_TEMP/cross" >> $GITHUB_OUTPUT +CROSS_DIR="$1" +if [ -f "$CROSS_DIR/cross" ]; then + echo "build-command=$CROSS_DIR/cross" >> $GITHUB_OUTPUT else echo "build-command=cargo" >> $GITHUB_OUTPUT fi diff --git a/set-cross-dir.sh b/set-cross-dir.sh new file mode 100755 index 0000000..f67a0af --- /dev/null +++ b/set-cross-dir.sh @@ -0,0 +1,4 @@ +set -e +set -x + +echo "cross-dir=$RUNNER_TEMP" >> $GITHUB_OUTPUT