aboutsummaryrefslogtreecommitdiff
path: root/lib/actions/build-docker.bash
blob: f7f62415be882a1f04c60be0ad666b839c217825 (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
#!/usr/bin/bash

action() {
	local OPTIND
	local base_image
	local base_tag
	local output_tag
	local dockerfile
	local template
	local install_options

	while getopts ":B:b:f:T:t:" opt
	do
		case "$opt" in
			b) RSTAR_BACKEND=$OPTARG ;;
			B) base_image=$OPTARG ;;
			T) base_tag=$OPTARG ;;
			t) output_tag=$OPTARG ;;
			f) template=$OPTARG ;;
			*) emerg "Invalid option specified: $opt" ;;
		esac
	done

	# Base image must be specified
	if [[ -z $base_image ]]
	then
		emerg "Must specify a base image with -B"
		exit 2
	fi

	# Tag of base image defaults to 'latest'
	[[ -z $base_tag ]] && base_tag="latest"

	# Set a default tag for the built image with relevant information
	if [[ -z "$output_tag" ]]
	then
		output_tag="rakudo:$base_image-$base_tag"
		if [[ ! -z $RSTAR_BACKEND ]]
		then
			output_tag="$output_tag-$RSTAR_BACKEND"
		fi
	fi

	dockerfile=$(tempfile)

	# We use the Dockerfile template for this image + tag pair if it exists,
	# or use the image default template
	if [[ -z $template ]] && [[ -f "$BASEDIR/lib/docker/$base_image/$base_tag" ]]
	then
		template="$BASEDIR/lib/docker/$base_image/$base_tag"
	else
		if [[ -f "$BASEDIR/lib/docker/$base_image" ]]
		then
			template="$BASEDIR/lib/docker/$base_image"
		else
			# Die if we have not found a template to use
			emerg "No Dockerfile template found for '$base_image:$base_tag'!"
			exit 2
		fi
	fi

	# Fill in template placeholders
	if [[ ! -z $RSTAR_BACKEND ]]
	then
		install_options="$install_options -b "'"'"$RSTAR_BACKEND"'"'
	fi

	sed < "$template" > "$dockerfile" \
		-e "s/{{INSTALL_OPTIONS}}/$install_options/" \
		-e "s/{{TAG}}/$base_tag/"

	# Build the image with the generated Dockerfile
	docker build -t "$output_tag" -f "$dockerfile" "$BASEDIR"

	shift $(( OPTIND -1 ))
}