summaryrefslogtreecommitdiff
path: root/_build.sh
blob: 35da7a348d18889626c7d3771a0ac5ca3b0970af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash

img_name="$1"
shift
build_dir="$1"
shift

function print_usage {
  echo ""
  echo "Usage: $0 <image-name> <build-dir> [ .. <additional args to docker build> ]"
  echo ""
  echo "  for example:"
  echo "    $0 docker.helsinki.at/debian:stretch debian/stretch/ --no-cache"
  echo "  will run:"
  echo "    docker build -t docker.helsinki.at/debian:stretch --no-cache debian/stretch/"
  echo ""
}

if [ -z "$img_name" ] || [ -z "$build_dir" ]; then
  print_usage
  exit 1
fi

if [ ! -d "$build_dir" ]; then
  echo "Error: '$build_dir' is not a directory"
  exit 1
fi

set -e

BASE_D="${BASH_SOURCE%/*}"
STAGING_D=$(mktemp -d -t rh-docker-build.XXXX)

function cleanup {
  rm -rf "$STAGING_D"
}
trap cleanup EXIT

cp -r "$build_dir" "$STAGING_D"
BUILD_D="$STAGING_D"/$(basename "$build_dir")
cp -r "$BASE_D/common" "$BUILD_D/"


if [ "$UID" -ne 0 ]; then
  exec sudo docker build -t "$img_name" "$@" "$BUILD_D"
fi
exec docker build -t "$img_name" "$@" "$BUILD_D"