|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# (re)build the CI container image |
| 4 | +# |
| 5 | +# run with --push to push the images. |
| 6 | + |
| 7 | +# set to disable cache |
| 8 | +NO_CACHE="${NO_CACHE:-}" |
| 9 | + |
| 10 | +DEBIAN_TAG="buster-20240612-slim" |
| 11 | +CONTAINER_PREFIX="ghcr.io/cbs228" |
| 12 | +CONTAINER_FQNAME="${CONTAINER_PREFIX}/sameold/builder/%s" |
| 13 | + |
| 14 | +RUST_VERSIONS=("1.84.0") |
| 15 | + |
| 16 | +usage() { |
| 17 | + cat <<EOF |
| 18 | +Usage: $0 [--push] |
| 19 | +
|
| 20 | +Build container images for the CI environment. To select |
| 21 | +a particular container platform tool like podman, set |
| 22 | +
|
| 23 | + BUILDER=podman |
| 24 | +
|
| 25 | +Prior to pushing, make sure to |
| 26 | +
|
| 27 | + podman login "$CONTAINER_PREFIX" |
| 28 | +
|
| 29 | +or equivalent. |
| 30 | +EOF |
| 31 | +} |
| 32 | + |
| 33 | +run() { |
| 34 | + # Echo and run |
| 35 | + echo >&2 "$@" |
| 36 | + "$@" |
| 37 | +} |
| 38 | + |
| 39 | +container_builder() { |
| 40 | + # Usage: automatically detect container platform command |
| 41 | + |
| 42 | + if [ -x "${BUILDER:-}" ]; then |
| 43 | + echo "${BUILDER}" |
| 44 | + elif [ -n "${BUILDER:-}" ]; then |
| 45 | + command -v "${BUILDER}" |
| 46 | + else |
| 47 | + { |
| 48 | + command -v podman || \ |
| 49 | + command -v docker |
| 50 | + } 2>/dev/null || { |
| 51 | + echo >&2 "FATAL: container platform tools not found" |
| 52 | + return 1; |
| 53 | + } |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +container_name() { |
| 58 | + # Usage: container_name SUFFIX |
| 59 | + |
| 60 | + #shellcheck disable=SC2059 |
| 61 | + printf "$CONTAINER_FQNAME" "$1" |
| 62 | +} |
| 63 | + |
| 64 | +buildcontainer() { |
| 65 | + # Usage: buildcontainer ARGS |
| 66 | + # |
| 67 | + # Run the $BUILDER to build a container image. Some standardized |
| 68 | + # arguments are passed to every build. |
| 69 | + |
| 70 | + if [[ $BUILDER =~ podman$ ]]; then |
| 71 | + run "$BUILDER" build \ |
| 72 | + --build-arg SOURCE_DATE_EPOCH="$SOURCE_DATE_EPOCH" \ |
| 73 | + --timestamp "$SOURCE_DATE_EPOCH" \ |
| 74 | + ${NO_CACHE:+--no-cache} \ |
| 75 | + "$@" |
| 76 | + else |
| 77 | + # docker mode; 100% untested |
| 78 | + run "$BUILDER" buildx build \ |
| 79 | + --build-arg SOURCE_DATE_EPOCH="$SOURCE_DATE_EPOCH" \ |
| 80 | + --output type=docker,rewrite-timestamp=true \ |
| 81 | + ${NO_CACHE:+--no-cache} \ |
| 82 | + "$@" |
| 83 | + fi |
| 84 | +} |
| 85 | + |
| 86 | +tagall() { |
| 87 | + # Usage: tagall SHORTNAME TAG0 TAG1 ... |
| 88 | + # |
| 89 | + # Apply all tags to the given image, which must already be tagged as |
| 90 | + # "current/container/prefix/$SHORTNAME:$TAG0" |
| 91 | + |
| 92 | + local prefix |
| 93 | + prefix="$(container_name "${1?}")" |
| 94 | + shift |
| 95 | + |
| 96 | + run "$BUILDER" tag "${@/#/"$prefix:"}" |
| 97 | +} |
| 98 | + |
| 99 | +pushall() { |
| 100 | + # Usage: pushall SHORTNAME TAG0 TAG1 ... |
| 101 | + # |
| 102 | + # Push the given image "current/container/prefix/$SHORTNAME" and |
| 103 | + # all the tags specified on the command line. The pushes are not |
| 104 | + # atomic and will happen sequentially. |
| 105 | + |
| 106 | + local prefix |
| 107 | + prefix="$(container_name "${1?}")" |
| 108 | + shift |
| 109 | + |
| 110 | + local tag |
| 111 | + for tag in "$@"; do |
| 112 | + run "$BUILDER" push "${prefix}:${tag}" |
| 113 | + done |
| 114 | +} |
| 115 | + |
| 116 | +# return if sourced |
| 117 | +(return 0 2>/dev/null) && return 0 |
| 118 | + |
| 119 | +set -euo pipefail |
| 120 | + |
| 121 | +if ! options="$(getopt -o 'hp' --long help,push -- "$@")"; then |
| 122 | + usage >&2 |
| 123 | + exit 1 |
| 124 | +fi |
| 125 | + |
| 126 | +eval set -- "${options:-}" |
| 127 | +push_images='' |
| 128 | +while true; do |
| 129 | + case "${1:-}" in |
| 130 | + (-h | --help) |
| 131 | + usage |
| 132 | + exit 0 ;; |
| 133 | + (-p | --push) |
| 134 | + push_images=y ;; |
| 135 | + ('') ;; |
| 136 | + esac |
| 137 | + shift || break |
| 138 | +done |
| 139 | + |
| 140 | +BUILDER="$(container_builder)" |
| 141 | +selfdir="$(dirname "$(realpath -e "${0?}")")" |
| 142 | + |
| 143 | +# set SOURCE_DATE_EPOCH if possible |
| 144 | +SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(git log -1 --pretty=%ct -- "$selfdir" || date +%s)}" |
| 145 | +export SOURCE_DATE_EPOCH |
| 146 | + |
| 147 | +# tag with "latest" and SOURCE_DATE_EPOCH |
| 148 | +CONTAINER_TAGS=("latest" "$(date --date '@'"$SOURCE_DATE_EPOCH" +'%Y-%m-%d')") |
| 149 | + |
| 150 | +# build the base image |
| 151 | +base_tag="$(container_name base):${CONTAINER_TAGS[0]}" |
| 152 | + |
| 153 | +buildcontainer \ |
| 154 | + --build-arg DEBIAN_TAG="$DEBIAN_TAG" \ |
| 155 | + --tag "$base_tag" \ |
| 156 | + "${selfdir?}/base" |
| 157 | + |
| 158 | +# add rust to the base image |
| 159 | +rust_tag="$(container_name rust):${CONTAINER_TAGS[0]}" |
| 160 | + |
| 161 | +buildcontainer \ |
| 162 | + --from "$base_tag" \ |
| 163 | + --build-arg RUST_VERSIONS="${RUST_VERSIONS[*]}" \ |
| 164 | + --tag "$rust_tag" \ |
| 165 | + "${selfdir?}/rust" |
| 166 | + |
| 167 | +# build architecture-specific images |
| 168 | +for containerdir in "${selfdir?}/"*-*-*; do |
| 169 | + [ -d "$containerdir" ] || continue |
| 170 | + |
| 171 | + platform_triple="$(basename ${containerdir})" |
| 172 | + cur_tag="$(container_name "$platform_triple"):${CONTAINER_TAGS[0]}" |
| 173 | + |
| 174 | + buildcontainer \ |
| 175 | + --from "$rust_tag" \ |
| 176 | + --tag "${cur_tag}" \ |
| 177 | + "${containerdir}" |
| 178 | +done |
| 179 | + |
| 180 | +# if all builds succeed, apply remaining tags... |
| 181 | +tagall base "${CONTAINER_TAGS[@]}" |
| 182 | +tagall rust "${CONTAINER_TAGS[@]}" |
| 183 | +for containerdir in "${selfdir?}/"*-*-*; do |
| 184 | + [ -d "$containerdir" ] || continue |
| 185 | + |
| 186 | + tagall "$(basename "$containerdir")" "${CONTAINER_TAGS[@]}" |
| 187 | +done |
| 188 | + |
| 189 | +[ -n "${push_images:-}" ] || exit 0 |
| 190 | + |
| 191 | +# ... and push |
| 192 | +for containerdir in "${selfdir?}/"*; do |
| 193 | + [ -d "$containerdir" ] || continue |
| 194 | + |
| 195 | + pushall "$(basename "$containerdir")" "${CONTAINER_TAGS[@]}" |
| 196 | +done |
0 commit comments