-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·165 lines (135 loc) · 3.49 KB
/
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
function __load_travisci_environment() {
TAG=${TRAVIS_TAG}
BRANCH=${TRAVIS_BRANCH}
BUILD_NUMBER=${TRAVIS_BUILD_NUMBER}
COMMIT=${TRAVIS_COMMIT}
}
function __load_circleci_environment() {
TAG=${CIRCLE_TAG}
BRANCH=${CIRCLE_BRANCH}
BUILD_NUMBER=${CIRCLE_BUILD_NUM}
COMMIT=${CIRCLE_SHA1}
}
function __load_github_actions_environment() {
if [[ $GITHUB_REF == refs/heads/* ]]; then
BRANCH=${GITHUB_REF#"refs/heads/"}
TAG=""
elif [[ $GITHUB_REF == refs/tags/* ]]; then
TAG=${GITHUB_REF#"refs/tags/"}
BRANCH=""
fi
BUILD_NUMBER=${GITHUB_RUN_NUMBER}
COMMIT=${GITHUB_SHA}
}
dockerSetup() {
if [ "$TRAVIS" = "true" ]; then
__load_travisci_environment
elif [ "$CIRCLECI" = "true" ]; then
__load_circleci_environment
elif [ "$GITHUB_ACTIONS" = "true" ]; then
__load_github_actions_environment
else
echo "Could not detect CI environment"
exit 1
fi
if [[ $* == *latest* ]]
then
echo "ERROR: 'latest' flag was removed - check the up-to-date docs"
exit 2
fi
if [[ $* == *--skip-login* ]]
then
DO_LOGIN=0
else
DO_LOGIN=1
fi
local DEVELOPMENT_BRANCH="${DEV_BRANCH:-main}"
if [[ -n "$TAG" ]]; then
VERSION="$TAG (build $BUILD_NUMBER)"
DOCKER_TAG="$TAG"
DOCKER_TAG_AS_LATEST="true"
if [[ "$DOCKER_TAG" =~ ^([0-9]+\.[0-9]+)\.[0-9]+$ ]]; then
EXTRA_DOCKER_TAG=${BASH_REMATCH[1]}
fi
elif [[ -n "$BRANCH" ]]; then
case $BRANCH in
"$DEVELOPMENT_BRANCH")
VERSION="dev-${COMMIT::7} (build $BUILD_NUMBER)"
DOCKER_TAG="dev"
;;
"$STABLE_BRANCH")
VERSION="rc-${COMMIT::7} (build $BUILD_NUMBER)"
DOCKER_TAG="rc"
;;
release/*)
VERSION="${BRANCH##release/}-dev-${COMMIT::7} (build $BUILD_NUMBER)"
DOCKER_TAG="${BRANCH##release/}-dev"
;;
preview/*)
VERSION="${BRANCH##preview/}-${COMMIT::7} (build $BUILD_NUMBER)"
DOCKER_TAG="${BRANCH##preview/}"
;;
*) # if we couldn't set a DOCKER_TAG, stop setup
return
esac
fi
echo "Version: ${VERSION}"
if [ $DO_LOGIN -eq 1 ]
then
# See https://stackoverflow.com/a/4775845/641451 for the `<<< "$VARIABLE"` syntax
docker login --username="${DOCKER_USER}" --password-stdin "${DOCKER_REGISTRY}" <<< "${DOCKER_PASS}"
fi
}
__dockerTagAndPush() {
local EXTRA_TAG="${1}"
local EXTRA_IMAGE="${REPO}:${EXTRA_TAG}"
echo "Tagging also as $EXTRA_IMAGE"
docker tag "${IMAGE}" "${EXTRA_IMAGE}"
echo "Pushing ${EXTRA_IMAGE}"
docker push "${EXTRA_IMAGE}"
}
dockerBuildAndPush() {
if [[ -z "$DOCKER_TAG" ]]; then
echo "Not building because DOCKER_TAG is undefined"
return
fi
local REPO=$DOCKER_REPOSITORY
local DIR="."
local OPTIND
local TAG_SUFFIX
local BUILD_OPTS
while getopts ":r:d:s:t:o:" opt "$@"; do
case ${opt} in
r)
REPO=$OPTARG
;;
s)
REPO="${DOCKER_REPOSITORY}${OPTARG}"
;;
d)
DIR=$OPTARG
;;
t)
TAG_SUFFIX=$OPTARG
;;
o)
BUILD_OPTS=$OPTARG
;;
*)
;;
esac
done
local IMAGE="${REPO}:${DOCKER_TAG}${TAG_SUFFIX}"
echo "Building image ${IMAGE} from ${DIR}"
# shellcheck disable=SC2086
docker build ${BUILD_OPTS} -t "${IMAGE}" "${DIR}"
echo "Pushing ${IMAGE}"
docker push "${IMAGE}"
if [[ -n "$EXTRA_DOCKER_TAG" ]]; then
__dockerTagAndPush "${EXTRA_DOCKER_TAG}${TAG_SUFFIX}"
fi
if [[ -n "$DOCKER_TAG_AS_LATEST" ]]; then
__dockerTagAndPush "latest"
fi
}