aboutsummaryrefslogtreecommitdiff
path: root/lib/actions/build-docker.bash
blob: 8075973512c941d2bc592801e93959aa260c35d4 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash

RSTAR_DEPS_BIN+=(
	docker
)

action() {
	local OPTIND
	local name
	local version
	local description
	local tag
	local dockerfile
	local target

	while getopts ":T:b:d:ln:t:" opt
	do
		case "$opt" in
			T) tag=$OPTARG ;;
			b) RSTAR_BACKEND=$OPTARG ;;
			d) description=$OPTARG ;;
			l) tag_latest=1 ;;
			n) name=$OPTARG ;;
			t) version=$OPTARG ;;
			*) emerg "Invalid option specified: $opt" ;;
		esac
	done

	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
	shift

	# Show warnings
	if [[ -n $tag ]] && [[ -n $tag_latest ]]
	then
		warn "-l is ignored if -T is given"
	fi

	if (( 0 < $# ))
	then
		warn "Only $target will be built, additional arguments are being ignored!"
	fi

	# Set defaults for the Docker tag value
	[[ -z $name ]] && name="$USER/rakudo-star"

	# Build up a nice tag if none was explicitly defined
	if [[ -z $tag ]]
	then
		[[ -z $version ]] && version="$(datetime %Y.%m)"
		[[ -z $description ]] && description="$target"

		tag="$version-$description"
	fi

	dockerfile="$BASEDIR/lib/docker/$target.Dockerfile"

	debug "Using $dockerfile"

	if [[ ! -f $dockerfile ]]
	then
		alert "Target '$target' is not supported"
		action_build_docker_list
		return 2
	fi

	# 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
		docker tag "$name:$tag" "$name:latest-$target"
	fi
}

action_build_docker_list() {
	chgdir "$BASEDIR/lib/docker" > /dev/null

	info "Available targets are:"

	for target in *.Dockerfile
	do
		info "  ${target%.*}"
	done
}