-
Notifications
You must be signed in to change notification settings - Fork 9
/
docker_build.sh
executable file
·104 lines (80 loc) · 3.14 KB
/
docker_build.sh
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
102
103
104
#!/bin/bash
set -e
function expandDockerfile {
[ -f "$1" ] && echo "$1"
[ -f "$1/Dockerfile" ] && echo "$1/Dockerfile"
echo
}
# Checking number of arguments
if [ "$#" -lt 4 ]; then
echo "You need to provide a directory with a Dockerfile in it, Docker image name, the base-dir and one or more tags."
exit 1
fi
# Checking DOCKER_ORGANIZATION environment variable
echo "--------------------------------------------------------------------------------------------"
# shellcheck disable=SC2153
docker_organization=$DOCKER_ORGANIZATION
if [ -z "$REGISTRY_URL" ]; then
if [ -z "$docker_organization" ]; then
echo " No DOCKER_ORGANIZATION set. This is mandatory when using docker.io"
exit 1
fi
registry_url_prefix="docker.io/$docker_organization"
echo "Docker organization: $docker_organization"
else
registry_url_prefix="$REGISTRY_URL"
fi
echo "registry_url_prefix: $registry_url_prefix"
# Checking GITHUB_ORGANIZATION environment variable
# shellcheck disable=SC2153
github_organization=$GITHUB_ORGANIZATION
if [ -z "$github_organization" ]; then
echo " No GITHUB_ORGANIZATION set. Using the DOCKER_ORGANIZATION ( $docker_organization ) instead."
github_organization=$docker_organization
fi
echo "Github organization: $github_organization"
echo "--------------------------------------------------------------------------------------------"
# Checking DOCKER_BUILD_ARGS environment variable
# shellcheck disable=SC2153
docker_build_args=$DOCKER_BUILD_ARGS
if [ -z "$docker_build_args" ]; then
echo " No DOCKER_BUILD_ARGS provided."
else
echo " Using provided DOCKER_BUILD_ARGS"
fi
dockerfile=$1
shift
imagename=$1
shift
basedir=$1
shift
alltags=$*
IFS=' '
read -ra tags <<<"$alltags"
basetag=${tags[0]}
echo "Switching to $basedir"
echo "--------------------------------------------------------------------------------------------"
cd "$basedir"
dockerfilepath=$(expandDockerfile "$dockerfile")
project=${GITHUB_REPOSITORY}
commitsha=${GITHUB_SHA}
if [ -z "$GITHUB_SHA" ]; then
echo " No GITHUB_SHA set. Try to get it myself with git."
commitsha=$(git rev-parse --verify HEAD)
fi
echo "Building docker image from: $dockerfilepath with name: $imagename/$basetag"
echo "--------------------------------------------------------------------------------------------"
echo "tags: $alltags"
echo "$alltags" >TAGS
echo "repo: https://github.com/$project/tree/$commitsha"
echo "https://github.com/$project/tree/$commitsha" >REPO
# shellcheck disable=SC2086
docker build . -f "$dockerfilepath" -t "$registry_url_prefix"/"$imagename":"$basetag" $docker_build_args
echo "--------------------------------------------------------------------------------------------"
for tag in "${tags[@]:1}"; do
echo "Tagging $registry_url_prefix/$imagename:$basetag as $registry_url_prefix/$imagename:$tag"
docker tag "$registry_url_prefix"/"$imagename":"$basetag" "$registry_url_prefix"/"$imagename":"$tag"
done
echo "============================================================================================"
echo "Finished building docker images from: $dockerfilepath"
echo "============================================================================================"