publish-builder.sh (1865B)
1 #!/bin/sh 2 # mk/publish-builder.sh - build and push the UNOS builder image. 3 # 4 # ./mk/publish-builder.sh [--dry-run] 5 # 6 # Tags are immutable: an existing tag is left alone and the task reports 7 # skipped, so a toolchain change needs a builder/VERSION bump to take effect. 8 # Forgetting that bump is caught by `ci-setup.sh --check` in the check task, 9 # which runs inside the image this pushed. 10 set -eu 11 12 HERE=$(cd "$(dirname "$0")" && pwd) 13 ROOT=$(cd "${HERE}/.." && pwd) 14 15 IMAGE="${UNOS_BUILDER_IMAGE:-finwo/unos-builder}" 16 DRY=0 17 [ "${1:-}" = "--dry-run" ] && DRY=1 18 19 msg() { printf '==> %s\n' "$*"; } 20 die() { printf 'publish-builder.sh: error: %s\n' "$*" >&2; exit 1; } 21 22 [ -f "${ROOT}/builder/VERSION" ] || die "no builder/VERSION" 23 read -r VERSION < "${ROOT}/builder/VERSION" 24 [ -n "${VERSION}" ] || die "builder/VERSION is empty" 25 TAG="${IMAGE}:${VERSION}" 26 27 if [ "${DRY}" = "1" ]; then 28 msg "would build and push ${TAG}" 29 exit 0 30 fi 31 32 command -v docker >/dev/null 2>&1 || die "no docker; task needs requires: [docker]" 33 34 published() { docker manifest inspect "${TAG}" >/dev/null 2>&1; } 35 36 skip() { 37 msg "${TAG} already published" 38 echo "::set-state skipped" 39 exit 0 40 } 41 42 # Prevent building if already published 43 published && skip 44 45 for v in REGISTRY_USERNAME REGISTRY_TOKEN; do 46 eval "val=\${${v}:-}" 47 [ -n "${val}" ] || die "${v} is not set" 48 done 49 50 logout() { docker logout ${REGISTRY_HOST:+"${REGISTRY_HOST}"} >/dev/null 2>&1 || true; } 51 trap logout EXIT INT TERM 52 53 printf '%s' "${REGISTRY_TOKEN}" \ 54 | docker login --username "${REGISTRY_USERNAME}" --password-stdin ${REGISTRY_HOST:+"${REGISTRY_HOST}"} \ 55 || die "docker login failed" 56 57 published && skip 58 59 msg "building ${TAG}" 60 docker build -f "${ROOT}/builder/Dockerfile" -t "${TAG}" "${ROOT}" \ 61 || die "docker build failed" 62 63 msg "pushing ${TAG}" 64 docker push "${TAG}" || die "docker push failed" 65 66 msg "published ${TAG}"