github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/containers/ddev-dbserver/build_image.sh (about)

     1  #!/bin/bash
     2  
     3  # Build a ddev-dbserver image for variety of mariadb/mysql
     4  # and per architecture, optionally push
     5  # By default loads to local docker
     6  
     7  set -eu -o pipefail
     8  
     9  OS=$(uname -s)
    10  if [ ${OS} = "Darwin" ]; then
    11    if ! command -v brew >/dev/null; then
    12      echo "On macOS, homebrew is required to get gnu-getopt" && exit 1
    13    fi
    14    if ! brew info gnu-getopt >/dev/null; then
    15      echo "On macOS, brew install gnu-getopt"
    16      exit 1
    17    fi
    18    PATH="$(brew --prefix gnu-getopt)/bin:$PATH"
    19  fi
    20  
    21  ! getopt --test >/dev/null
    22  if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
    23    echo 'getopt --test` failed in this environment.'
    24    exit 1
    25  fi
    26  
    27  OPTS=-h,-v:,-d:
    28  LONGOPTS=archs:,db-type:,db-major-version:,db-pinned-version:,docker-args:,tag:,push,help
    29  
    30  ! PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "$0" -- "$@")
    31  if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
    32    # e.g. return value is 1
    33    #  then getopt has complained about wrong arguments to stdout
    34    printf "\n\nFailed parsing options:\n"
    35    getopt --longoptions=$LONGOPTS --name "$0" -- "$@"
    36    exit 3
    37  fi
    38  
    39  eval set -- "$PARSED"
    40  
    41  ARCHS=linux/$(../get_arch.sh)
    42  MYARCH=${ARCHS}
    43  PUSH=""
    44  NO_LOAD=""
    45  DB_TYPE=mariadb
    46  DB_MAJOR_VERSION=10.3
    47  IMAGE_TAG=$(git describe --tags --always --dirty)
    48  DOCKER_ARGS=""
    49  
    50  while true; do
    51    case "$1" in
    52    --db-type | -d)
    53      DB_TYPE=$2
    54      shift 2
    55      ;;
    56    --db-major-version | -v)
    57      DB_MAJOR_VERSION=$2
    58      shift 2
    59      ;;
    60    --db-pinned-version)
    61      DB_PINNED_VERSION=$2
    62      shift 2
    63      ;;
    64    --archs)
    65      ARCHS=$2
    66      shift 2
    67      ;;
    68    --push)
    69      PUSH=true
    70      shift 1
    71      ;;
    72    --no-load)
    73      NO_LOAD=true
    74      shift 1
    75      ;;
    76    --docker-args)
    77      DOCKER_ARGS=$2
    78      shift 2
    79      ;;
    80    --tag)
    81      IMAGE_TAG=$2
    82      shift 2
    83      ;;
    84    -h | --help)
    85      echo "Usage: $0 --db-type [mariadb|mysql] --db-major-version <major> --tag <image_tag> --archs <comma-delimited_architectures> --push --no-load"
    86      printf "Examples: $0 ./build_image.sh --db-type mysql --db-major-version 8.0 --tag junker99 --archs linux/amd64 --push
    87    $0 --db-type mariadb --db-major-version 10.3 --tag junker99 --archs linux/amd64,linux/arm64"
    88      exit 0
    89      ;;
    90    --)
    91      break
    92      ;;
    93    esac
    94  done
    95  
    96  set -o nounset
    97  
    98  if [ -z ${DB_PINNED_VERSION:-} ]; then
    99    DB_PINNED_VERSION=${DB_MAJOR_VERSION}
   100  fi
   101  
   102  BASE_IMAGE=${DB_TYPE}
   103  
   104  # For mysql, we have to use our own base images at drud/mysql
   105  set -x
   106  
   107  if [ ${DB_TYPE} = "mysql" ] && [[ "$ARCHS" == *"linux/arm64"* ]]; then
   108    BASE_IMAGE=drud/mysql
   109  fi
   110  printf "\n\n========== Building drud/ddev-dbserver-${DB_TYPE}-${DB_MAJOR_VERSION}:${IMAGE_TAG} from ${BASE_IMAGE} for ${ARCHS} with pinned version ${DB_PINNED_VERSION} ==========\n"
   111  
   112  if [ ! -z ${PUSH:-} ]; then
   113    echo "building/pushing drud/ddev-dbserver-${DB_TYPE}-${DB_MAJOR_VERSION}:${IMAGE_TAG}"
   114    set -x
   115    docker buildx build --push --platform ${ARCHS} ${DOCKER_ARGS} --build-arg="BASE_IMAGE=${BASE_IMAGE}" --build-arg="DB_PINNED_VERSION=${DB_PINNED_VERSION}" --build-arg="DB_VERSION=${DB_MAJOR_VERSION}" -t "drud/ddev-dbserver-${DB_TYPE}-${DB_MAJOR_VERSION}:${IMAGE_TAG}" .
   116    set +x
   117  fi
   118  
   119  # By default, load/import into local docker
   120  set -x
   121  if [ -z "${PUSH:-}" ]; then
   122      echo "Loading to local docker drud/ddev-dbserver-${DB_TYPE}-${DB_MAJOR_VERSION}:${IMAGE_TAG}"
   123      docker buildx build --load ${DOCKER_ARGS} --build-arg="DB_TYPE=${DB_TYPE}" --build-arg="DB_VERSION=${DB_MAJOR_VERSION}" --build-arg="BASE_IMAGE=${BASE_IMAGE}" --build-arg="DB_PINNED_VERSION=${DB_PINNED_VERSION}" -t "drud/ddev-dbserver-${DB_TYPE}-${DB_MAJOR_VERSION}:${IMAGE_TAG}" .
   124  fi