-
Notifications
You must be signed in to change notification settings - Fork 6
/
travis.script.sh
executable file
·175 lines (147 loc) · 6.01 KB
/
travis.script.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
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
#exit script on any error
set -e
#Shell Colour constants for use in 'echo -e'
#e.g. echo -e "My message ${GREEN}with just this text in green${NC}"
# shellcheck disable=SC2034
{
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m' # No Colour
}
readonly REPO="gchq/stroom-ui"
readonly CONTEXT_ROOT="docker/."
# This is a whitelist of branches to produce docker builds for
readonly BRANCH_WHITELIST_REGEX='(^dev$|^master$|^[0-9]+\.[0-9]+$)'
# Tags matching this regex will trigger a bintray release
readonly RELEASE_VERSION_REGEX='^v[0-9]+\.[0-9]+.*$'
readonly LATEST_SUFFIX="-LATEST"
version_fixed_tag=""
snapshot_floating_tag=""
major_ver_floating_tag=""
minor_ver_floating_tag=""
is_docker_build_required=false
extra_build_args=""
echo_travis_env_vars() {
# Dump all the travis env vars to the console for debugging
echo -e "TRAVIS_BUILD_NUMBER: [${GREEN}${TRAVIS_BUILD_NUMBER}${NC}]"
echo -e "TRAVIS_COMMIT: [${GREEN}${TRAVIS_COMMIT}${NC}]"
echo -e "TRAVIS_BRANCH: [${GREEN}${TRAVIS_BRANCH}${NC}]"
echo -e "TRAVIS_TAG: [${GREEN}${TRAVIS_TAG}${NC}]"
echo -e "TRAVIS_PULL_REQUEST: [${GREEN}${TRAVIS_PULL_REQUEST}${NC}]"
echo -e "TRAVIS_EVENT_TYPE: [${GREEN}${TRAVIS_EVENT_TYPE}${NC}]"
}
extract_build_vars() {
# Normal commit/PR/tag build
if [ -n "$TRAVIS_TAG" ]; then
echo -e "This is a tagged build"
VERSION="${TRAVIS_TAG}"
is_docker_build_required=true
# This is a tagged commit, so create a docker image with that tag
version_fixed_tag="${TRAVIS_TAG}"
# Extract the major version part for a floating tag
majorVer=$(echo "${TRAVIS_TAG}" | grep -oP "v[0-9]+")
if [ -n "${majorVer}" ]; then
major_ver_floating_tag="${majorVer}${LATEST_SUFFIX}"
fi
# Extract the minor version part for a floating tag
minorVer=$(echo "${TRAVIS_TAG}" | grep -oP "v[0-9]+\.[0-9]+")
if [ -n "${minorVer}" ]; then
minor_ver_floating_tag="${minorVer}${LATEST_SUFFIX}"
fi
if [[ "$TRAVIS_TAG" =~ ${RELEASE_VERSION_REGEX} ]]; then
echo "This is a release version so add gradle arg for publishing libs to Bintray"
extra_build_args="bintrayUpload"
fi
elif [[ "$TRAVIS_BRANCH" =~ $BRANCH_WHITELIST_REGEX ]]; then
echo -e "This is a white-listed branch build"
# This is a branch we want to create a floating snapshot docker image for
snapshot_floating_tag="${TRAVIS_BRANCH}-SNAPSHOT"
is_docker_build_required=true
else
# No tag so use the branch name as the version, e.g. dev
VERSION="${TRAVIS_BRANCH}"
fi
}
echo_build_vars() {
echo -e "VERSION: [${GREEN}${VERSION}${NC}]"
echo -e "version fixed docker tag: [${GREEN}${version_fixed_tag}${NC}]"
echo -e "snapshot floating docker tag: [${GREEN}${snapshot_floating_tag}${NC}]"
echo -e "major ver floating docker tag: [${GREEN}${major_ver_floating_tag}${NC}]"
echo -e "minor ver floating docker tag: [${GREEN}${minor_ver_floating_tag}${NC}]"
echo -e "is_docker_build_required: [${GREEN}${is_docker_build_required}${NC}]"
echo -e "extra_build_args: [${GREEN}${extra_build_args}${NC}]"
}
prep_ui_build() {
pushd ${CONTEXT_ROOT}
echo -e "Copying UI files"
./prep.sh
popd
}
do_docker_build_if_required() {
# Don't do a docker build for pull requests
if [ "${is_docker_build_required}" = true ] && [ "$TRAVIS_PULL_REQUEST" = "false" ] ; then
echo -e "${GREEN}Running docker build${NC}"
# TODO - the major and minor floating tags assume that the release builds are all done in strict sequence
# If say the build for v6.0.1 is re-run after the build for v6.0.2 has run then v6.0-LATEST will point to v6.0.1
# which is incorrect, hopefully this course of events is unlikely to happen
all_docker_tags=( \
"${version_fixed_tag}" \
"${snapshot_floating_tag}" \
"${major_ver_floating_tag}" \
"${minor_ver_floating_tag}" \
)
echo -e "all_docker_tags: [${GREEN}${all_docker_tags[*]}${NC}]"
echo -e "Preparing for ui docker build"
mkdir -p "${CONTEXT_ROOT}/work"
cp -r build/* "${CONTEXT_ROOT}/work"
release_to_docker_hub "${REPO}" "${CONTEXT_ROOT}" "${all_docker_tags[@]}"
fi
}
release_to_docker_hub() {
if [ $# -lt 3 ]; then
echo "Incorrect args, expecting at least 3"
exit 1
fi
docker_repo="$1"
context_root="$2"
#shift the the args so we can loop round the open ended list of tags, $1 is now the first tag
shift 2
all_tag_args=()
for tag_version_part in "$@"; do
if [ "x${tag_version_part}" != "x" ]; then
all_tag_args+=( "--tag=${docker_repo}:${tag_version_part}" )
fi
done
echo -e "Building a docker image with tags: ${GREEN}${all_tag_args[*]}${NC}"
echo -e "docker_repo: [${GREEN}${docker_repo}${NC}]"
echo -e "context_root: [${GREEN}${context_root}${NC}]"
# If we have a TRAVIS_TAG (git tag) then use that, else use the floating tag
docker build \
"${all_tag_args[@]}" \
--build-arg GIT_COMMIT="${TRAVIS_COMMIT}" \
--build-arg GIT_TAG="${TRAVIS_TAG:-${snapshot_floating_tag}}" \
"${context_root}"
echo -e "Logging in to Docker"
#The username and password are configured in the travis gui
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin >/dev/null 2>&1
echo -e "Pushing the docker image to ${GREEN}${docker_repo}${NC} with tags: ${GREEN}${all_tag_args[*]}${NC}"
docker push "${docker_repo}" >/dev/null 2>&1
echo -e "Logging out of Docker"
docker logout >/dev/null 2>&1
}
main() {
echo_travis_env_vars
extract_build_vars
echo_build_vars
# Build the project
echo -e "${GREEN}Running npm install${NC}"
yarn install
echo -e "${GREEN}Running npm build${NC}"
yarn build
do_docker_build_if_required
exit 0
}
main "$@"