duneci-build-docker-image 940 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#! /bin/bash

set -e
set -u

usage() {
  echo "usage: $0 [docker-options]... <image-name> <directories>..." >&2
  exit 0
}

docker-build() {
  docker build ${docker_opts[@]:+"${docker_opts[@]}"} "${@}"
}

docker_opts=()
16
for var in ftp_proxy FTP_PROXY http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY; do
17 18 19 20 21
  if [[ -n "${!var:-}" ]]; then
    docker_opts+=(--build-arg="${var}=${!var}")
  fi
done

22 23 24 25
if [[ "${DUNECI_DOCKER_CACHE:-1}" = 0 ]]; then
  docker_opts+=(--no-cache)
fi

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
if [[ $# -lt 2 ]]; then
  usage
fi

while :; do
  case "${1}" in
    -*)
      docker_opts+=("${1}")
      shift
      ;;
    *)
      break
      ;;
  esac
done

image="${1}"; shift

builddir="build/${image##*/}"
if [[ -e ${builddir} ]]; then
  echo "E: ${builddir} already exists" >&2
  exit 1
fi

mkdir -p -- "${builddir}"

for d in "$@"; do
  cp -t "${builddir}" -- "${d}"/*
done

docker-build -t "${image}" "${builddir}"

rm -rf -- "${builddir}"