forked from librenms/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·139 lines (130 loc) · 4.42 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
#!/usr/bin/env bash
set -e
PROJECT=librenms
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
BUILD_TAG=docker_build
BUILD_WORKINGDIR=${BUILD_WORKINGDIR:-.}
DOCKERFILE=${DOCKERFILE:-Dockerfile}
VCS_REF=${TRAVIS_COMMIT::8}
RUNNING_TIMEOUT=120
RUNNING_LOG_CHECK="snmpd entered RUNNING state"
PUSH_LATEST=${PUSH_LATEST:-true}
DOCKER_USERNAME=${DOCKER_USERNAME:-librenms}
DOCKER_REPONAME=${DOCKER_REPONAME:-librenms}
DOCKER_LOGIN=${DOCKER_LOGIN:-librenmsbot}
QUAY_USERNAME=${QUAY_USERNAME:-librenms}
QUAY_REPONAME=${QUAY_REPONAME:-librenms}
QUAY_LOGIN=${QUAY_LOGIN:-librenms+travis}
# Check local or travis
BRANCH=${TRAVIS_BRANCH:-local}
if [[ ${TRAVIS_PULL_REQUEST} == "true" ]]; then
BRANCH=${TRAVIS_PULL_REQUEST_BRANCH}
fi
DOCKER_TAG=${BRANCH:-local}
if [[ "$BRANCH" == "master" ]]; then
DOCKER_TAG=latest
elif [[ "$BRANCH" == "local" ]]; then
BUILD_DATE=
VERSION=local
fi
echo "PROJECT=${PROJECT}"
echo "VERSION=${VERSION}"
echo "BUILD_DATE=${BUILD_DATE}"
echo "BUILD_TAG=${BUILD_TAG}"
echo "BUILD_WORKINGDIR=${BUILD_WORKINGDIR}"
echo "DOCKERFILE=${DOCKERFILE}"
echo "VCS_REF=${VCS_REF}"
echo "PUSH_LATEST=${PUSH_LATEST}"
echo "DOCKER_LOGIN=${DOCKER_LOGIN}"
echo "DOCKER_USERNAME=${DOCKER_USERNAME}"
echo "DOCKER_REPONAME=${DOCKER_REPONAME}"
echo "QUAY_LOGIN=${QUAY_LOGIN}"
echo "QUAY_USERNAME=${QUAY_USERNAME}"
echo "QUAY_REPONAME=${QUAY_REPONAME}"
echo "TRAVIS_BRANCH=${TRAVIS_BRANCH}"
echo "TRAVIS_PULL_REQUEST=${TRAVIS_PULL_REQUEST}"
echo "BRANCH=${BRANCH}"
echo "DOCKER_TAG=${DOCKER_TAG}"
echo
# Build
echo "### Build"
docker build \
--build-arg BUILD_DATE=${BUILD_DATE} \
--build-arg VCS_REF=${VCS_REF} \
--build-arg VERSION=${VERSION} \
-t ${BUILD_TAG} -f ${DOCKERFILE} ${BUILD_WORKINGDIR}
echo
echo "### Test"
docker rm -f ${PROJECT} ${PROJECT}-db > /dev/null 2>&1 || true
docker network rm ${PROJECT} > /dev/null 2>&1 || true
docker network create -d bridge ${PROJECT}
docker run -d --network=${PROJECT} --name ${PROJECT}-db --hostname ${PROJECT}-db \
-e "MYSQL_ALLOW_EMPTY_PASSWORD=yes" \
-e "MYSQL_DATABASE=librenms" \
-e "MYSQL_USER=librenms" \
-e "MYSQL_PASSWORD=asupersecretpassword" \
mariadb:10.2 \
mysqld --sql-mode= --innodb-file-per-table=1 --lower-case-table-names=0
docker run -d --network=${PROJECT} --link ${PROJECT}-db \
-e "DB_HOST=${PROJECT}-db" \
-e "DB_NAME=librenms" \
-e "DB_USER=librenms" \
-e "DB_PASSWORD=asupersecretpassword" \
--name ${PROJECT} ${BUILD_TAG}
echo
echo "### Waiting for ${PROJECT} to be up..."
TIMEOUT=$((SECONDS + RUNNING_TIMEOUT))
while read LOGLINE; do
echo ${LOGLINE}
if [[ ${LOGLINE} == *"${RUNNING_LOG_CHECK}"* ]]; then
echo "Container up!"
break
fi
if [[ $SECONDS -gt ${TIMEOUT} ]]; then
>&2 echo "ERROR: Failed to run ${PROJECT} container"
docker rm -f ${PROJECT} > /dev/null 2>&1 || true
exit 1
fi
done < <(docker logs -f ${PROJECT} 2>&1)
echo
CONTAINER_STATUS=$(docker container inspect --format "{{.State.Status}}" ${PROJECT})
if [[ ${CONTAINER_STATUS} != "running" ]]; then
>&2 echo "ERROR: Container ${PROJECT} returned status '$CONTAINER_STATUS'"
docker rm -f ${PROJECT} > /dev/null 2>&1 || true
exit 1
fi
docker rm -f ${PROJECT} > /dev/null 2>&1 || true
echo
if [ "${VERSION}" == "local" -o "${TRAVIS_PULL_REQUEST}" == "true" ]; then
echo "INFO: This is a PR or a local build, skipping push..."
exit 0
fi
if [[ ! -z ${DOCKER_PASSWORD} ]]; then
echo "### Push to Docker Hub..."
echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_LOGIN" --password-stdin > /dev/null 2>&1
if [ "${DOCKER_TAG}" == "latest" -a "${PUSH_LATEST}" == "true" ]; then
docker tag ${BUILD_TAG} ${DOCKER_USERNAME}/${DOCKER_REPONAME}:${DOCKER_TAG}
fi
if [[ "${VERSION}" != "latest" ]]; then
docker tag ${BUILD_TAG} ${DOCKER_USERNAME}/${DOCKER_REPONAME}:${VERSION}
fi
docker push ${DOCKER_USERNAME}/${DOCKER_REPONAME}
if [[ ! -z ${MICROBADGER_HOOK} ]]; then
echo "Call MicroBadger hook"
curl -X POST ${MICROBADGER_HOOK}
echo
fi
echo
fi
if [[ ! -z ${QUAY_PASSWORD} ]]; then
echo "### Push to Quay..."
echo "$QUAY_PASSWORD" | docker login quay.io --username "$QUAY_LOGIN" --password-stdin > /dev/null 2>&1
if [ "${DOCKER_TAG}" == "latest" -a "${PUSH_LATEST}" == "true" ]; then
docker tag ${BUILD_TAG} quay.io/${QUAY_USERNAME}/${QUAY_REPONAME}:${DOCKER_TAG}
fi
if [[ "${VERSION}" != "latest" ]]; then
docker tag ${BUILD_TAG} quay.io/${QUAY_USERNAME}/${QUAY_REPONAME}:${VERSION}
fi
docker push quay.io/${QUAY_USERNAME}/${QUAY_REPONAME}
echo
fi