|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -Eeo pipefail |
| 3 | + |
| 4 | +function pre_build_image() |
| 5 | +{ |
| 6 | + image=$1 |
| 7 | + |
| 8 | + tempImage=($(echo $image | tr "/" " ")) |
| 9 | + if [ ${#tempImage[@]} == 1 ]; then |
| 10 | + tempImage="$namespace/$image" |
| 11 | + fi |
| 12 | + # get imageName and imageTag |
| 13 | + temp=($(echo $tempImage | tr ":" " ")) |
| 14 | + imageName=${temp[0]} |
| 15 | + imageTag=${temp[1]} |
| 16 | + # check local and repository image |
| 17 | + repositoryImageExists=$(curl --silent -f --head -lL https://hub.docker.com/v2/repositories/$imageName/tags/$imageTag/ > /dev/null && echo "success" || echo "failed") |
| 18 | + localImageExists=$( docker image ls --format "{{.Repository}}:{{.Tag}}" | awk -v aaa=$image '{print $0} END{print aaa}' | grep -c $image ) |
| 19 | + |
| 20 | + # check local image |
| 21 | + if [ "$localImageExists" -ne 1 ]; then |
| 22 | + if [ "$forcebuildimage" ]; then |
| 23 | + echo "docker image $image exists, rebuilding the image ..." |
| 24 | + else |
| 25 | + echo "docker image $image exists, skiping ..." |
| 26 | + return 255 |
| 27 | + fi |
| 28 | + fi |
| 29 | + |
| 30 | + # check repository image |
| 31 | + if [ "$repositoryImageExists" == "success" ]; then |
| 32 | + if [ "$forcebuildimage" != "remote" ]; then |
| 33 | + echo "docker image $imageName:$imageTag exists on dockerhub, skiping ... " |
| 34 | + return 255 |
| 35 | + else |
| 36 | + echo "docker image $imageName:$imageTag exists on dockerhub, rebuild image ..." |
| 37 | + fi |
| 38 | + fi |
| 39 | + |
| 40 | + echo "pre_build_image success!" |
| 41 | + return 0 |
| 42 | +} |
| 43 | + |
| 44 | +function get_build_cmd() { |
| 45 | + image=$1 |
| 46 | + platform=$2 |
| 47 | + |
| 48 | + build_cmd="docker buildx build --no-cache" |
| 49 | + #docker_version=$(docker version --format '{{index (split .Server.Version ".") 0}}') |
| 50 | + if [ $platform == "all" ]; then |
| 51 | + builder_exists=$(docker buildx ls | awk '{if ($1=="multi-platform") print $1}') |
| 52 | + if [ "$builder_exists" ]; then |
| 53 | + docker buildx rm multi-platform |
| 54 | + fi |
| 55 | + # create a new builder instance |
| 56 | + docker buildx create --use --name multi-platform --platform=linux/amd64,linux/arm64 |
| 57 | + |
| 58 | + temp=($(echo $image | tr "/" " ")) |
| 59 | + if [ ${#temp[@]} == 1 ]; then |
| 60 | + image="$namespace/$image" |
| 61 | + fi |
| 62 | + |
| 63 | + build_cmd="$build_cmd --push --platform linux/amd64,linux/arm64" |
| 64 | + else |
| 65 | + build_cmd="$build_cmd -o type=docker --platform linux/${platform}" |
| 66 | + fi |
| 67 | + |
| 68 | + echo "$build_cmd -t $image ." |
| 69 | + |
| 70 | + # remove builder instance |
| 71 | +# if [ $platform == "all" ]; then |
| 72 | +# docker buildx rm multi-platform |
| 73 | +# fi |
| 74 | +} |
0 commit comments