name: Setup Rust
description: Install Rust toolchain, cache cargo artefacts, and optionally install CLI tools via cargo-binstall.
inputs:
toolchain:
description: Rust toolchain to install (e.g. stable, 1.82.0)
required: false
default: stable
components:
description: Space-separated rustup components to add
required: false
default: ""
tools:
description: Space-separated list of crates to install via cargo-binstall (e.g. "typos-cli cargo-nextest git-cliff@2.12.0")
required: false
default: ""
cache-key:
description: Prefix for the cache key to isolate caches between jobs
required: false
default: rust
cache-target:
description: Whether to cache the target/ directory
required: false
default: "true"
runs:
using: composite
steps:
- name: Install Rust toolchain
shell: bash
run: |
set -euo pipefail
rustup toolchain install "${{ inputs.toolchain }}"
rustup default "${{ inputs.toolchain }}"
if [[ -n "${{ inputs.components }}" ]]; then
read -r -a components <<< "${{ inputs.components }}"
rustup component add --toolchain "${{ inputs.toolchain }}" "${components[@]}"
fi
- name: Determine cache paths
id: paths
shell: bash
run: |
set -euo pipefail
cache_paths=(
"$HOME/.cargo/bin"
"$HOME/.cargo/registry/index"
"$HOME/.cargo/registry/cache"
"$HOME/.cargo/git/db"
)
if [[ "${{ inputs.cache-target }}" == "true" ]]; then
cache_paths+=("$GITHUB_WORKSPACE/target")
fi
{
echo "paths<<EOF"
printf '%s\n' "${cache_paths[@]}"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Cache cargo artefacts
uses: actions/cache@v4
with:
path: ${{ steps.paths.outputs.paths }}
key: ${{ inputs.cache-key }}-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ inputs.cache-key }}-${{ runner.os }}-cargo-
- name: Install CLI tools via cargo-binstall
if: inputs.tools != ''
shell: bash
run: |
set -euo pipefail
if ! command -v cargo-binstall &>/dev/null; then
curl -L --proto '=https' --tlsv1.2 -sSf \
https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
fi
read -r -a tools <<< "${{ inputs.tools }}"
cargo binstall --no-confirm "${tools[@]}"