Skip to content

Instantly share code, notes, and snippets.

@tsibley
Created June 5, 2023 22:42
Show Gist options
  • Save tsibley/dfc53095abc3660d6843e52f33b72ae8 to your computer and use it in GitHub Desktop.
Save tsibley/dfc53095abc3660d6843e52f33b72ae8 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Extracts the cache date from the registry images and provides them into the
# environment so local images may be built with caching even on the first time.
#
set -euo pipefail
main() {
local registry tag
# Set default values.
registry=docker.io
tag=latest
# Read command-line arguments.
while getopts "r:t:" opt; do
case "$opt" in
r) registry="$OPTARG";;
t) tag="$OPTARG";;
*) echo "Usage: $0 [-r <registry>] [-t <tag>]" 1>&2; exit 1;;
esac
done
image="docker://$registry/nextstrain/base-builder:$tag"
log "Looking up CACHE_DATE for $image"
date="$(cache-date "$image")"
log "Exporting CACHE_DATE=$date"
export CACHE_DATE="$date"
log "Running your command now…"
exec "$@"
}
cache-date() {
local image="$1"
skopeo inspect --config "$image" | jq --raw-output '
.history
| map(.created_by)
| map(match("\\bCACHE_DATE=(\\S+)\\b") | .captures[0].string)
| .[0]
'
}
skopeo() {
docker run \
--rm \
--network host \
--volume "$HOME"/.docker/config.json:/auth.json:ro \
quay.io/skopeo/stable "$@"
}
log() {
echo "==> $*" >&2
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment