add support for multiple tags and push.

This commit is contained in:
Miroslav Šedivý 2025-03-28 16:07:14 +01:00
parent f9868aa92e
commit 1e5110b8c6

72
build
View file

@ -14,12 +14,13 @@ function help() {
echo "Usage: $0" echo "Usage: $0"
echo " -p, --platform : The platform (default: linux/amd64)" echo " -p, --platform : The platform (default: linux/amd64)"
echo " -i, --image : The image name prefix (default: ghcr.io/m1k1o/neko)" echo " -i, --image : The image name prefix (default: ghcr.io/m1k1o/neko)"
echo " -v, --version : The version (default: latest)" echo " -t, --tag : The image tag (default: latest)"
echo " -f, --flavor : The flavor, if not specified, builds without flavor" echo " -f, --flavor : The flavor, if not specified, builds without flavor"
echo " -b, --base : The base image name (default: ghcr.io/m1k1o/neko/[<flavor>-]base:<version>)" echo " -b, --base : The base image name (default: ghcr.io/m1k1o/neko/[<flavor>-]base:<tag>)"
echo " -a, --app : The app to build, if not specified, builds the base image" echo " -a, --app : The app to build, if not specified, builds the base image"
echo " -y, --yes : Skip confirmation prompts" echo " -y, --yes : Skip confirmation prompts"
echo " --no-cache : Build without docker cache" echo " --no-cache : Build without docker cache"
echo " --push : Push the image to the registry after building"
echo " -h, --help : Show this help message" echo " -h, --help : Show this help message"
} }
@ -28,31 +29,37 @@ while [[ "$#" -gt 0 ]]; do
case $1 in case $1 in
--platform|-p) PLATFORM="$2"; shift ;; --platform|-p) PLATFORM="$2"; shift ;;
--image|-i) IMAGE="$2"; shift ;; --image|-i) IMAGE="$2"; shift ;;
--version|-v) VERSION="$2"; shift ;; --tag|-t)
# comma separated list of TAGS
IFS=',' read -r -a TAGS <<< "$2"
# default tag is the first one
TAG="${TAGS[0]}"
shift ;;
--flavor|-f) FLAVOR="$2"; shift ;; --flavor|-f) FLAVOR="$2"; shift ;;
--base|-b) BASE_IMAGE="$2"; shift ;; --base|-b) BASE_IMAGE="$2"; shift ;;
--app|-a) APPLICATION="$2"; shift ;; --app|-a) APPLICATION="$2"; shift ;;
--yes|-y) YES=1 ;; --yes|-y) YES=1 ;;
--no-cache) NO_CACHE="--no-cache" log "Building without cache" ;; --no-cache) NO_CACHE="--no-cache" log "Building without cache" ;;
--push) PUSH=1 ;;
--help|-h) help; exit 0 ;; --help|-h) help; exit 0 ;;
-*) log "Unknown parameter passed: $1"; help; exit 1 ;; -*) log "Unknown parameter passed: $1"; help; exit 1 ;;
*) *)
if [ -z "$FULL_IMAGE" ]; then if [ -z "$FULL_IMAGE" ]; then
FULL_IMAGE="$1" FULL_IMAGE="$1"
# extracts image, flavor, app and version from the full image name # extracts image, flavor, app and tag from the full image name
# example: # example:
# ghcr.io/m1k1o/neko/nvidia-firefox:latest # ghcr.io/m1k1o/neko/nvidia-firefox:latest
# will be split into: # will be split into:
# IMAGE=ghcr.io/m1k1o/neko # IMAGE=ghcr.io/m1k1o/neko
# FLAVOR=nvidia # FLAVOR=nvidia
# APPLICATION=firefox # APPLICATION=firefox
# VERSION=latest # TAG=latest
# remove the tag from the image name # remove the tag from the image name
if [[ "$FULL_IMAGE" == *":"* ]]; then if [[ "$FULL_IMAGE" == *":"* ]]; then
# removes everything before the last : # removes everything before the last :
VERSION="${FULL_IMAGE##*:}" # will be latest TAG="${FULL_IMAGE##*:}" # will be latest
# removes everything after the last : # removes everything after the last :
FULL_IMAGE="${FULL_IMAGE%:*}" # will be ghcr.io/m1k1o/neko/nvidia-firefox FULL_IMAGE="${FULL_IMAGE%:*}" # will be ghcr.io/m1k1o/neko/nvidia-firefox
fi fi
@ -114,6 +121,36 @@ function build_image() {
docker build --platform $PLATFORM $NO_CACHE $@ docker build --platform $PLATFORM $NO_CACHE $@
} }
function tag_image() {
local IMAGE="$1"
local IMAGE_NO_TAG="${IMAGE%:*}"
local IMAGE_TAG="${IMAGE##*:}"
for TAG in "${TAGS[@]}"; do
# skip if the tag is the same as the image tag
if [ "$TAG" == "$IMAGE_TAG" ]; then
continue
fi
log "Tagging $IMAGE with tag $TAG"
docker tag $IMAGE $IMAGE_NO_TAG:$TAG
done
}
function push_image() {
if [ -z "$PUSH" ]; then
return 0
fi
local IMAGE="$1"
local IMAGE_NO_TAG="${IMAGE%:*}"
for TAG in "${TAGS[@]}"; do
log "Pushing $IMAGE_NO_TAG:$TAG to registry"
docker push $IMAGE_NO_TAG:$TAG
done
}
# -------------------------------------------------------------------- # --------------------------------------------------------------------
if [ -z "$PLATFORM" ]; then if [ -z "$PLATFORM" ]; then
@ -126,10 +163,14 @@ if [ -z "$IMAGE" ]; then
fi fi
log "Using image: $IMAGE" log "Using image: $IMAGE"
if [ -z "$VERSION" ]; then if [ -z "$TAG" ]; then
VERSION="latest" TAG="latest"
TAGS=($TAG)
fi fi
log "Using version: $VERSION"
for TAG in "${TAGS[@]}"; do
log "Using tag: $TAG"
done
if [ -z "$FLAVOR" ]; then if [ -z "$FLAVOR" ]; then
log "No flavor specified, building without flavor" log "No flavor specified, building without flavor"
@ -139,9 +180,9 @@ fi
if [ -z "$BASE_IMAGE" ]; then if [ -z "$BASE_IMAGE" ]; then
if [ -z "$FLAVOR" ]; then if [ -z "$FLAVOR" ]; then
BASE_IMAGE="$IMAGE/base:$VERSION" BASE_IMAGE="$IMAGE/base:$TAG"
else else
BASE_IMAGE="$IMAGE/$FLAVOR-base:$VERSION" BASE_IMAGE="$IMAGE/$FLAVOR-base:$TAG"
fi fi
fi fi
@ -159,7 +200,7 @@ if [ ! -z "$APPLICATION" ]; then
fi fi
# flavor is specified, append it to the image name and Dockerfile # flavor is specified, append it to the image name and Dockerfile
APPLICATION_IMAGE="$IMAGE/$APPLICATION:$VERSION" APPLICATION_IMAGE="$IMAGE/$APPLICATION:$TAG"
APPLICATION_DOCKERFILE="apps/$APPLICATION/Dockerfile" APPLICATION_DOCKERFILE="apps/$APPLICATION/Dockerfile"
if [ ! -z "$FLAVOR" ]; then if [ ! -z "$FLAVOR" ]; then
APPLICATION_IMAGE="$IMAGE/$FLAVOR-$APPLICATION" APPLICATION_IMAGE="$IMAGE/$FLAVOR-$APPLICATION"
@ -177,6 +218,10 @@ if [ ! -z "$APPLICATION" ]; then
-t $APPLICATION_IMAGE \ -t $APPLICATION_IMAGE \
-f $APPLICATION_DOCKERFILE \ -f $APPLICATION_DOCKERFILE \
$APPLICATION_DIR $APPLICATION_DIR
tag_image $APPLICATION_IMAGE
push_image $APPLICATION_IMAGE
exit 0 exit 0
fi fi
@ -220,3 +265,6 @@ build_image -t $BASE_IMAGE -f - . <<EOF
COPY config.yml /etc/neko/neko.yaml COPY config.yml /etc/neko/neko.yaml
EOF EOF
tag_image $BASE_IMAGE
push_image $BASE_IMAGE