0
0
#!/usr/bin/env bash
# scripts/oci-push.sh <file-or-dir> [ttl=1h]: pushes a file, or a whole directory tree, to https://ttl.sh (an anonymous, ephemeral OCI
# registry: the tag is the TTL, 24h at most) under an unguessable name, and prints
# ttl.sh/<name>@sha256:<digest of the manifest>
# the one thing a consumer pins (scripts/oci-pull.sh: `oras pull <that>` verifies every blob against it; a tag is never trusted). No credential, so
# a fork's run can use it. Each push is time-boxed (OCI_PUSH_TIMEOUT, 900 s) and tried twice. Exit 1 when the registry refuses or is down: the
# caller keeps its local build.
#
# A FILE goes up as parts of at most OCI_PART_MB (75) MB, each its own monolithic blob upload (POST, then a single PUT with its Content-Length), several
# at once (OCI_PARALLEL, 8), under one manifest with a layer per part, titled <name>.part-0000, .part-0001, ...: scripts/oci-pull.sh (oras pull, which checks
# every layer against the manifest) concatenates them back. ttl.sh's behaviour, measured 2026-11-14: a 200 MB upload takes about 30 s from hat-8, a 589 MB one about
# 12 minutes (0.8 MB/s; from a rented CI VM 200 MB alone took 4 minutes), and oras push, which streams a blob with no length, did 100 MB in 121 s where a monolithic PUT
# or skopeo copy did it in 19 s: a big or streamed blob is slow there, smaller ones in parallel are not. From a rented VM, the 589 MB binary in 4 parts of 150 MB took 214 s, in
# 8 parts of 75 MB 115 s (the connection is limited, not the VM: its upload to our DO bucket does 22 MB/s). A directory tree goes with `oras push` (the whole tree, as it is).
set -euo pipefail
src=$1 ttl=${2:-1h}
host=ttl.sh name="swc-$(cat /proc/sys/kernel/random/uuid)"
limit=${OCI_PUSH_TIMEOUT:-900}
if [ -d "$src" ]; then
oras=$(bash "$(dirname "${BASH_SOURCE[0]}")/ci-oras.sh")
cd "$(dirname "$src")"
for attempt in 1 2; do
if ref=$(timeout "$limit" "$oras" push --no-tty --artifact-type application/vnd.swc.ci-output "$host/$name:$ttl" "$(basename "$src")" --format go-template='{{.reference}}'); then
echo "$ref"; exit 0
fi
echo "oci-push: attempt $attempt failed" >&2
done
exit 1
fi
base="https://$host/v2/$name"
work=$(mktemp -d); trap 'rm -rf "$work"' EXIT
blob() { # <file> <digest>: one monolithic upload
local loc sep
loc=$(curl -fsS --max-time 60 -X POST -D - -o /dev/null "$base/blobs/uploads/" | tr -d '\r' | awk 'tolower($1) == "location:" {print $2}')
case "$loc" in http*) ;; *) loc="https://$host$loc" ;; esac
case "$loc" in *\?*) sep='&' ;; *) sep='?' ;; esac
curl -fsS --max-time "$limit" -X PUT -H 'Content-Type: application/octet-stream' -T "$1" "$loc${sep}digest=sha256:$2" -o /dev/null
}
split -b "${OCI_PART_MB:-75}M" -d -a 4 "$src" "$work/part."
layers="" n=0 files=() sums=()
for p in "$work"/part.*; do
d=$(sha256sum "$p" | cut -d' ' -f1)
files+=("$p"); sums+=("$d")
layers="$layers${layers:+,}{\"mediaType\":\"application/octet-stream\",\"digest\":\"sha256:$d\",\"size\":$(stat -c %s "$p"),\"annotations\":{\"org.opencontainers.image.title\":\"$(basename "$src").part-$(printf %04d "$n")\"}}"
n=$((n + 1))
done
printf '{}' > "$work/config"; cd=$(sha256sum "$work/config" | cut -d' ' -f1)
printf '{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","artifactType":"application/vnd.swc.ci-output","config":{"mediaType":"application/vnd.oci.empty.v1+json","digest":"sha256:%s","size":2},"layers":[%s]}' "$cd" "$layers" > "$work/manifest"
md=$(sha256sum "$work/manifest" | cut -d' ' -f1)
push_all() {
local i pids=() bad=0
blob "$work/config" "$cd" || return 1
for i in "${!files[@]}"; do
while [ "$(jobs -rp | wc -l)" -ge "${OCI_PARALLEL:-8}" ]; do sleep 0.2; done
blob "${files[$i]}" "${sums[$i]}" & pids+=($!)
done
for i in "${pids[@]}"; do wait "$i" || bad=1; done
[ "$bad" = 0 ] || return 1
curl -fsS --max-time 60 -X PUT -H 'Content-Type: application/vnd.oci.image.manifest.v1+json' --data-binary "@$work/manifest" "$base/manifests/$ttl" -o /dev/null
}
for attempt in 1 2; do
if push_all; then echo "$host/$name@sha256:$md"; exit 0; fi
echo "oci-push: attempt $attempt failed" >&2
done
exit 1