@@ -0,0 +1,170 @@
7+# Constants and defaults
10+DOCKER_REPO=fpco/stack-build
19+# Print usage information and exit with failure status. First argument is an
24+ echo "Usage: $0 [--push] [--dry-run] [--small] lts-X.Y|lts-X|lts]"
25+ echo "See README.md for more information."
30+# Print a command, and if --dry-run disabled also run it
33+ [[ $DRY = true ]] || "$@"
36+# Push an image if --push is enabled (otherwise do nothing)
38+ [[ $PUSH = false ]] || dry docker push "$1"
41+# Tag an image, and then push it if --push is enabled
43+ dry docker tag "$1" "$2"
52+while [[ $# -gt 0 ]]; do
64+ DOCKER_REPO=fpco/stack-build-small
68+ usage "Unknown option: $1"
71+ if [[ -n "$LTS_SLUG_ARG" ]]; then
72+ usage "Cannot specify multiple snaphots: $1"
81+# Determine actual snapshot version from aliases
84+SNAPSHOTS="$(mktemp "lts-snapshots.json.XXXXXX")"
85+trap "rm -f \"$SNAPSHOTS\"" EXIT
86+wget -qO- https://www.stackage.org/download/lts-snapshots.json >"$SNAPSHOTS"
88+case "$LTS_SLUG_ARG" in
90+ usage "Missing argument: snapshot or alias"
93+ LTS_SLUG="$LTS_SLUG_ARG"
96+ LTS_SLUG=$(jq -r ".[\"$LTS_SLUG_ARG\"]" "$SNAPSHOTS")
97+ if [[ -z "$LTS_SLUG" || "$LTS_SLUG" = "null" ]]; then
98+ echo "$0: Cannot find LTS version for slug: $LTS_SLUG_ARG" >&2
104+LTS_VERSION="${LTS_SLUG#lts-}"
105+LTS_MAJOR="${LTS_VERSION%.*}"
106+LTS_MINOR="${LTS_VERSION#*.}"
109+# Determine latest LTS version
112+LATEST_LTS_SLUG=$(jq -r ".[\"lts\"]" $SNAPSHOTS)
113+LATEST_LTS_VERSION="${LATEST_LTS_SLUG#lts-}"
114+LATEST_LTS_MAJOR="${LATEST_LTS_VERSION%.*}"
115+LATEST_LTS_MINOR="${LATEST_LTS_VERSION#*.}"
118+# Determine latest minor version of the selected major version
121+MAJOR_LATEST_LTS_SLUG=$(jq -r ".[\"lts-$LTS_MAJOR\"]" $SNAPSHOTS)
122+MAJOR_LATEST_LTS_VERSION="${MAJOR_LATEST_LTS_SLUG#lts-}"
123+MAJOR_LATEST_LTS_MAJOR="${MAJOR_LATEST_LTS_VERSION%.*}"
124+MAJOR_LATEST_LTS_MINOR="${MAJOR_LATEST_LTS_VERSION#*.}"
127+# Find the Dockerfile for the selected snapshot
130+if [[ -s "$LTS_SLUG/Dockerfile" ]]; then
132+ # If there is an exact match, build and push that image
134+ sed "s/\\\$DOCKER_REPO/$(echo $DOCKER_REPO|sed 's/\//\\\//')/g" "$LTS_SLUG/Dockerfile" >"$LTS_SLUG/Dockerfile.sub"
135+ dry docker build -t "$DOCKER_REPO:$LTS_SLUG" --build-arg "DOCKER_REPO=$DOCKER_REPO" --build-arg "LTS_SLUG=$LTS_SLUG" --build-arg "VARIANT=$VARIANT" -f "$LTS_SLUG/Dockerfile.sub" "$LTS_SLUG"
136+ rm -f "$LTS_SLUG/Dockerfile.sub"
137+ push "$DOCKER_REPO:$LTS_SLUG"
140+ # If no exact match, find a dockerfile for any earlier minor version of the
141+ # selected major version, and just create a new tag from version's image with the selected
142+ # minor version (assuming that nothing needs to change), and push it.
144+ minor=$(( LTS_MINOR - 1 ))
145+ while [[ ! -s "lts-$LTS_MAJOR.$minor/Dockerfile" && $minor -ge 0 ]]; do
146+ minor=$(( minor - 1 ))
148+ if [[ $minor -lt 0 ]]; then
149+ echo "$0: Cannot find any Dockerfile for LTS major version" >&2
152+ dry docker pull "$DOCKER_REPO:lts-$LTS_MAJOR.$minor" || true
153+ tagpush "$DOCKER_REPO:lts-$LTS_MAJOR.$minor" "$DOCKER_REPO:$LTS_SLUG"
157+# Create and push additional tags
160+# If we select the latest minor version for the selected major version, then
161+# also create and push an 'lts-X' tag.
162+if [[ "$MAJOR_LATEST_LTS_VERSION" = "$LTS_VERSION" ]]; then
163+ tagpush "$DOCKER_REPO:$LTS_SLUG" "$DOCKER_REPO:lts-$LTS_MAJOR"
166+# If we selected the latest LTS snapshot, also create and push the 'lts' and 'latest' tags.
167+if [[ "$LATEST_LTS_VERSION" = "$LTS_VERSION" ]]; then
168+ tagpush "$DOCKER_REPO:$LTS_SLUG" "$DOCKER_REPO:lts"
169+ tagpush "$DOCKER_REPO:$LTS_SLUG" "$DOCKER_REPO:latest"