From 6a336f0167393318639b6bc1c9ccf0484219c970 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 8 Apr 2020 14:16:48 +0200 Subject: Rewrite the build-docker action --- lib/actions/build-docker.bash | 108 ++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 47 deletions(-) (limited to 'lib/actions/build-docker.bash') diff --git a/lib/actions/build-docker.bash b/lib/actions/build-docker.bash index f7f6241..86060df 100644 --- a/lib/actions/build-docker.bash +++ b/lib/actions/build-docker.bash @@ -1,76 +1,90 @@ -#!/usr/bin/bash +#!/usr/bin/env bash action() { local OPTIND - local base_image - local base_tag - local output_tag + local name + local version + local description + local tag local dockerfile - local template - local install_options + local target - while getopts ":B:b:f:T:t:" opt + while getopts ":T:b:d:ln:t:" opt do case "$opt" in + T) tag=$OPTARG ;; b) RSTAR_BACKEND=$OPTARG ;; - B) base_image=$OPTARG ;; - T) base_tag=$OPTARG ;; - t) output_tag=$OPTARG ;; - f) template=$OPTARG ;; + d) description=$OPTARG ;; + l) tag_latest=1 ;; + n) name=$OPTARG ;; + t) version=$OPTARG ;; *) emerg "Invalid option specified: $opt" ;; esac done - # Base image must be specified - if [[ -z $base_image ]] + shift $(( OPTIND - 1 )) + + SOURCE_DATE_EPOCH="$(git log -1 --pretty=format:%at)" + + if (( $# < 1 )) + then + alert "You must specify a base image to build for" + action_build_docker_list + return 2 + fi + + target=$1 + + if (( 1 < $# )) then - emerg "Must specify a base image with -B" - exit 2 + warn "Only $target will be built, additional arguments are being ignored!" fi - # Tag of base image defaults to 'latest' - [[ -z $base_tag ]] && base_tag="latest" + # Set defaults for the Docker tag value + [[ -z $name ]] && name="$USER/rakudo-star" - # Set a default tag for the built image with relevant information - if [[ -z "$output_tag" ]] + # Build up a nice tag if none was explicitly defined + if [[ -z $tag ]] then - output_tag="rakudo:$base_image-$base_tag" - if [[ ! -z $RSTAR_BACKEND ]] - then - output_tag="$output_tag-$RSTAR_BACKEND" - fi + [[ -z $version ]] && version="$(datetime %Y.%m)" + [[ -z $description ]] && description="$target" + + tag="$version-$description" fi - dockerfile=$(tempfile) + dockerfile="$BASEDIR/lib/docker/$target.Dockerfile" + + debug "Using $dockerfile" - # We use the Dockerfile template for this image + tag pair if it exists, - # or use the image default template - if [[ -z $template ]] && [[ -f "$BASEDIR/lib/docker/$base_image/$base_tag" ]] + if [[ ! -f $dockerfile ]] then - template="$BASEDIR/lib/docker/$base_image/$base_tag" - else - if [[ -f "$BASEDIR/lib/docker/$base_image" ]] - then - template="$BASEDIR/lib/docker/$base_image" - else - # Die if we have not found a template to use - emerg "No Dockerfile template found for '$base_image:$base_tag'!" - exit 2 - fi + alert "Target '$target' is not supported" + action_build_docker_list + return 2 fi - # Fill in template placeholders - if [[ ! -z $RSTAR_BACKEND ]] + # Build the image + docker build \ + -t "$name:$tag" \ + -f "$dockerfile" \ + --build-arg SOURCE_DATE_EPOCH="$SOURCE_DATE_EPOCH" \ + --build-arg RSTAR_BACKEND="$RSTAR_BACKEND" \ + "$BASEDIR" + + # Also tag the image as "latest" + if [[ $tag_latest ]] then - install_options="$install_options -b "'"'"$RSTAR_BACKEND"'"' + docker tag "$name:$tag" "$name:latest-$target" fi +} - sed < "$template" > "$dockerfile" \ - -e "s/{{INSTALL_OPTIONS}}/$install_options/" \ - -e "s/{{TAG}}/$base_tag/" +action_build_docker_list() { + chgdir "$BASEDIR/lib/docker" > /dev/null - # Build the image with the generated Dockerfile - docker build -t "$output_tag" -f "$dockerfile" "$BASEDIR" + info "Available targets are:" - shift $(( OPTIND -1 )) + for target in *.Dockerfile + do + info " ${target%.*}" + done } -- cgit v1.1