Last active
November 12, 2025 07:19
-
-
Save rossmacarthur/da07fd8915c1c9493a9478285327c942 to your computer and use it in GitHub Desktop.
setup-crate action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: install crate binary | |
| description: Install a Rust crate from a GitHub release asset | |
| inputs: | |
| repo: | |
| description: The GitHub repository containing the crate (e.g., "casey/just") | |
| required: true | |
| tag-prefix: | |
| description: The prefix to match the release tag (e.g., "name/" for tags like "name/v1.2.3") | |
| required: false | |
| default: "" | |
| bin: | |
| description: The name of the binary to install (defaults to the repo name) | |
| required: false | |
| runs: | |
| using: composite | |
| steps: | |
| - run: | | |
| : install crate from GitHub release asset | |
| tag_regex='^${{inputs.tag-prefix}}v?[0-9]+.[0-9]+.[0-9]+$' | |
| tag=$(gh release list --repo "$REPO" --json tagName \ | |
| | jq -r --arg regex "$tag_regex" \ | |
| 'map(.tagName) | map(select(test($regex))) | first // empty') | |
| echo "Found release tag: $tag" | |
| target=$(rustc -Vv | grep host | cut -d' ' -f2) | |
| echo "Detected target: $target" | |
| url=$(gh release view --repo "$REPO" "$tag" --json assets \ | |
| | jq -r --arg target "$target" \ | |
| '.assets | map(select(.name | contains($target))) | first | .url') | |
| echo "Downloading release asset: $url" | |
| td=$(mktemp -d || mktemp -d -t tmp) | |
| curl -fsSL --proto '=https' --tlsv1.2 "$url" | tar -xz -C "$td" | |
| # Kinda annoying, but some archives contain a single top-level directory, | |
| # others contain the binary directly. Here we try to detect a single top-level | |
| # directory and skip over it if so. | |
| dirs=() | |
| while IFS= read -r -d '' d; do | |
| dirs+=("$d") | |
| (( ${#dirs[@]} >= 2 )) && break # we only care if there's >1 | |
| done < <(find -L -- "$td" -mindepth 1 -maxdepth 1 -type d -print0) | |
| (( ${#dirs[@]} == 1 )) && td="${dirs[0]%/}" | |
| bin=$BIN | |
| [ -z "$bin" ] && bin="${REPO##*/}" | |
| install -m 755 "$td/$bin" "$HOME/.cargo/bin/$bin" | |
| echo "Installed $bin to $HOME/.cargo/bin/$bin!" | |
| shell: bash | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| REPO: ${{ inputs.repo }} | |
| BIN: ${{ inputs.bin }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment