on 3218e4e help sign in
177 lines 4.7 kB history raw
1#!/usr/bin/env bash
3set -eu
4cd "$(dirname "$0")"
6#
7# Constants and defaults
8#
10DOCKER_REPO=fpco/stack-build
11PUSH=false
12DRY=false
13VARIANT=build
16# Functions
19# Print usage information and exit with failure status. First argument is an
20# error message.
21usage() {
22 echo "$0: $1" >&2
23 echo
24 echo "Usage: $0 [--push] [--dry-run] [--small] lts-X.Y]"
25 echo "See README.md for more information."
26 echo
27 exit 1
30# Print a command, and if --dry-run disabled also run it
31dry() {
32 echo ">>> $*"
33 [[ $DRY = true ]] || "$@"
36# Push an image if --push is enabled (otherwise do nothing)
37push() {
38 [[ $PUSH = false ]] || dry docker push "$1"
41# Tag an image, and then push it if --push is enabled
42tagpush() {
43 dry docker tag "$1" "$2"
44 push "$2"
48# Parse command-line
51LTS_SLUG_ARG=
52while [[ $# -gt 0 ]]; do
53 case "$1" in
54 --push)
55 PUSH=true
56 shift
57 ;;
58 --dry-run)
59 DRY=true
60 shift
61 ;;
62 --small)
63 VARIANT=small
64 DOCKER_REPO=fpco/stack-build-small
65 shift
66 ;;
67 -*)
68 usage "Unknown option: $1"
69 ;;
70 *)
71 if [[ -n "$LTS_SLUG_ARG" ]]; then
72 usage "Cannot specify multiple snaphots: $1"
73 fi
74 LTS_SLUG_ARG="$1"
75 shift
76 ;;
77 esac
78done
80case "$LTS_SLUG_ARG" in
81 "")
82 usage "Missing argument: snapshot"
83 ;;
84 lts-*.*)
85 LTS_SLUG="$LTS_SLUG_ARG"
86 ;;
87 *)
88 echo "$0: Wrong snapshot format: $LTS_SLUG_ARG" >&2
89 exit 1
90 ;;
91esac
94# Determine if lts slug is latest
97SNAPSHOTS="$(mktemp "lts-snapshots.json.XXXXXX")"
98trap "rm -f \"$SNAPSHOTS\"" EXIT
99wget -qO- https://www.stackage.org/download/lts-snapshots.json >"$SNAPSHOTS"
101LTS_VERSION="${LTS_SLUG#lts-}"
102LTS_MAJOR="${LTS_VERSION%.*}"
103LTS_MINOR="${LTS_VERSION#*.}"
106# Determine latest LTS version
109if [[ ! -x "$HOME/.local/bin/jq" ]]; then
110 mkdir -p $HOME/.local/bin
111 if [[ "$(uname)" = "Darwin" ]]; then
112 curl -o $HOME/.local/bin/jq -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64
113 else
114 curl -o $HOME/.local/bin/jq -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
115 fi
116 chmod +x $HOME/.local/bin/jq
117fi
119LATEST_LTS_SLUG=$($HOME/.local/bin/jq -r ".[\"lts\"]" $SNAPSHOTS)
120LATEST_LTS_VERSION="${LATEST_LTS_SLUG#lts-}"
121LATEST_LTS_MAJOR="${LATEST_LTS_VERSION%.*}"
122LATEST_LTS_MINOR="${LATEST_LTS_VERSION#*.}"
125# Determine latest minor version of the selected major version
128MAJOR_LATEST_LTS_SLUG=$(jq -r ".[\"lts-$LTS_MAJOR\"]" $SNAPSHOTS)
129MAJOR_LATEST_LTS_VERSION="${MAJOR_LATEST_LTS_SLUG#lts-}"
130MAJOR_LATEST_LTS_MAJOR="${MAJOR_LATEST_LTS_VERSION%.*}"
131MAJOR_LATEST_LTS_MINOR="${MAJOR_LATEST_LTS_VERSION#*.}"
134# Find the Dockerfile for the selected snapshot
137if [[ -s "$LTS_SLUG/Dockerfile" ]]; then
139 # If there is an exact match, build and push that image
141 sed "s/\\\$DOCKER_REPO/$(echo $DOCKER_REPO|sed 's/\//\\\//')/g" "$LTS_SLUG/Dockerfile" >"$LTS_SLUG/Dockerfile.sub"
142 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"
143 rm -f "$LTS_SLUG/Dockerfile.sub"
144 push "$DOCKER_REPO:$LTS_SLUG"
145else
147 # If no exact match, find a dockerfile for any earlier minor version of the
148 # selected major version, and just create a new tag from version's image with the selected
149 # minor version (assuming that nothing needs to change), and push it.
151 minor=$(( LTS_MINOR - 1 ))
152 while [[ ! -s "lts-$LTS_MAJOR.$minor/Dockerfile" && $minor -ge 0 ]]; do
153 minor=$(( minor - 1 ))
154 done
155 if [[ $minor -lt 0 ]]; then
156 echo "$0: Cannot find any Dockerfile for LTS major version" >&2
157 exit 1
158 fi
159 dry docker pull "$DOCKER_REPO:lts-$LTS_MAJOR.$minor" || true
160 tagpush "$DOCKER_REPO:lts-$LTS_MAJOR.$minor" "$DOCKER_REPO:$LTS_SLUG"
161fi
164# Create and push additional tags
167# If we select the latest minor version for the selected major version, then
168# also create and push an 'lts-X' tag.
169if [[ $LTS_MINOR -ge $MAJOR_LATEST_LTS_MINOR ]]; then
170 tagpush "$DOCKER_REPO:$LTS_SLUG" "$DOCKER_REPO:lts-$LTS_MAJOR"
171fi
173# If we selected the latest LTS snapshot, also create and push the 'lts' and 'latest' tags.
174if [[ "$LTS_MAJOR" = "$LATEST_LTS_MAJOR" ]] && [[ $LTS_MINOR -ge $LATEST_LTS_MINOR ]]; then
175 tagpush "$DOCKER_REPO:$LTS_SLUG" "$DOCKER_REPO:lts"
176 tagpush "$DOCKER_REPO:$LTS_SLUG" "$DOCKER_REPO:latest"
177fi