Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

3.9 KB
Raw Blame
#!/usr/bin/env bash
set -euo pipefail

# Only confirmed absence permits an upload. Checksums are advisory once a file exists.
registry_status() {
  curl --silent --show-error --location --retry 2 --max-time 30 \
    --user-agent 'boxlite-release (https://github.com/boxlite-ai/boxlite)' \
    --output "$2" --write-out '%{http_code}' "$1"
}

# Markdown code spans in these printf formats use literal backticks.
# shellcheck disable=SC2016
compare_existing() {
  local label=$1 local_digest=$2 remote_digest=$3
  if [[ -z "$local_digest" || -z "$remote_digest" ]]; then
    printf -- '- Skipped `%s` (already published); could not compare checksums.\n' "$label" >> "$GITHUB_STEP_SUMMARY"
  elif [[ "$local_digest" != "$remote_digest" ]]; then
    printf -- '- Skipped `%s` (different bytes). Local `%s`, registry `%s`.\n' \
      "$label" "$local_digest" "$remote_digest" >> "$GITHUB_STEP_SUMMARY"
  fi
}

compare_npm_tarball() {
  local file=$1 package=$2 metadata=$3 remote='' local_digest=''
  remote=$(jq -er '."dist.integrity" | strings | select(test("^sha512-[A-Za-z0-9+/]{86}==$"))' "$metadata") || remote=''
  if [[ -n "$remote" ]] && local_digest=$(openssl dgst -sha512 -binary "$file" | openssl base64 -A); then
    local_digest="sha512-$local_digest"
  else
    local_digest=''
  fi
  compare_existing "$package" "$local_digest" "$remote"
}

select_wheels() {
  local metadata="$RUNNER_TEMP/pypi-version.json" file name remote local_digest status has_missing=false
  status=$(registry_status "https://pypi.org/pypi/boxlite/${TAG#v}/json" "$metadata")
  case "$status" in
    200) jq -e '.urls | arrays | all(.[]; (.filename | type) == "string" and (.filename | length) > 0)' "$metadata" >/dev/null ;;
    404) printf '{"urls":[]}\n' > "$metadata" ;;
    *) echo "PyPI version lookup returned HTTP $status" >&2; return 1 ;;
  esac
  mkdir dist
  for file in "$RELEASE_ASSET_DIR"/*.whl; do
    name=${file##*/}
    if jq -e --arg file "$name" 'any(.urls[]; .filename == $file)' "$metadata" >/dev/null; then
      remote=$(jq -er --arg file "$name" '.urls[] | select(.filename == $file) | .digests.sha256 | strings | select(test("^[a-f0-9]{64}$"))' "$metadata") || remote=''
      local_digest=$(sha256sum < "$file" | cut -d' ' -f1) || local_digest=''
      compare_existing "$name" "$local_digest" "$remote"
    else
      cp -- "$file" dist/
      has_missing=true
    fi
  done
  echo "has_missing=$has_missing" >> "$GITHUB_OUTPUT"
}

compare_crate() {
  local crate=$1 version=$2 metadata=$3 remote='' local_digest=''
  remote=$(jq -er '.version.checksum | strings | select(test("^[a-f0-9]{64}$"))' "$metadata") || remote=''
  local target="$RUNNER_TEMP/crate-comparison"
  if [[ -n "$remote" ]] && CARGO_TARGET_DIR="$target" cargo package -p "$crate" --no-verify; then
    local_digest=$(sha256sum < "$target/package/$crate-$version.crate" | cut -d' ' -f1) || local_digest=''
  fi
  compare_existing "$crate@$version" "$local_digest" "$remote"
}

publish_crate() {
  local crate=$1 version=${RELEASE_TAG#v}
  shift
  local url="https://crates.io/api/v1/crates/$crate/$version" metadata="$RUNNER_TEMP/crate-version.json"
  local status log="$RUNNER_TEMP/publish-$crate.log"
  status=$(registry_status "$url" "$metadata")
  case "$status" in
    200) compare_crate "$crate" "$version" "$metadata"; return ;;
    404) ;;
    *) echo "crates.io lookup for $crate@$version returned HTTP $status" >&2; return 1 ;;
  esac
  if cargo publish -p "$crate" "$@" 2>&1 | tee "$log"; then
    return
  fi
  if grep -Fq 'already uploaded' "$log" || grep -Fq "crate $crate@$version already exists on " "$log"; then
    if [[ "$(registry_status "$url" "$metadata")" == 200 ]]; then
      compare_crate "$crate" "$version" "$metadata"
    else
      compare_existing "$crate@$version" '' ''
    fi
    return
  fi
  return 1
}

case "$1" in
  npm) shift; compare_npm_tarball "$@" ;;
  wheels) select_wheels ;;
  crate) shift; publish_crate "$@" ;;
  *) exit 2 ;;
esac