-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.sh
executable file
·104 lines (84 loc) · 3.68 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
#!/bin/bash
set -e
ls -la
if [[ $(id -u) == 0 ]]; then
echo "*** BUILD SCRIPT RUNNING AS root"
echo "*** Don't do this. Exiting."
exit 1
fi
if [ -z "$DOCKER_PREFIX" ]; then
echo "WARNING: Env var DOCKER_PREFIX not set, assuming haufelexware/wicked."
export DOCKER_PREFIX="haufelexware/wicked."
fi
if [ -z "$DOCKER_TAG" ]; then
echo "WARNING: Env var DOCKER_TAG is not set, assuming 'dev'."
export DOCKER_TAG=dev
fi
if [[ "haufelexware/wicked." == "$DOCKER_PREFIX" ]] && [[ "$1" == "--push" ]]; then
echo "INFO: Resolving env base tag for target tag ${DOCKER_TAG}..."
if [ -z "$DOCKER_REGISTRY" ]; then
echo "WARNING: Env var DOCKER_REGISTRY not set, assuming official docker hub."
docker login -u ${DOCKER_REGISTRY_USER} -p ${DOCKER_REGISTRY_PASSWORD}
else
docker login -u ${DOCKER_REGISTRY_USER} -p ${DOCKER_REGISTRY_PASSWORD} ${DOCKER_REGISTRY}
fi
docker pull haufelexware/wicked.env:next-onbuild-alpine
export PORTAL_ENV_TAG=$(docker run --rm haufelexware/wicked.env:next-onbuild-alpine node node_modules/portal-env/getMatchingTag.js haufelexware wicked.env ${DOCKER_TAG})
if [ -z "$PORTAL_ENV_TAG" ]; then
echo "ERROR: Could not resolve wicked.env base tag!"
exit 1
fi
else
export PORTAL_ENV_TAG=${DOCKER_TAG}-onbuild
fi
echo "INFO: Using base image tag ${PORTAL_ENV_TAG}"
echo "============================================"
echo "Building normal image..."
echo "============================================"
export BUILD_ALPINE=""
perl -pe 's;(\\*)(\$([a-zA-Z_][a-zA-Z_0-9]*)|\$\{([a-zA-Z_][a-zA-Z_0-9]*)\})?;substr($1,0,int(length($1)/2)).($2&&length($1)%2?$2:$ENV{$3||$4});eg' Dockerfile.template > Dockerfile
normalImageName="${DOCKER_PREFIX}api:${DOCKER_TAG}"
if [[ "dev" = "${DOCKER_TAG}" ]]; then
docker build -t ${normalImageName} .
else
docker build --pull -t ${normalImageName} . --no-cache
fi
echo "============================================"
echo "Building alpine image..."
echo "============================================"
export BUILD_ALPINE="-alpine"
perl -pe 's;(\\*)(\$([a-zA-Z_][a-zA-Z_0-9]*)|\$\{([a-zA-Z_][a-zA-Z_0-9]*)\})?;substr($1,0,int(length($1)/2)).($2&&length($1)%2?$2:$ENV{$3||$4});eg' Dockerfile.template > Dockerfile-alpine
alpineImageName="${DOCKER_PREFIX}api:${DOCKER_TAG}-alpine"
if [[ "dev" = "${DOCKER_TAG}" ]]; then
docker build -f Dockerfile-alpine -t ${alpineImageName} .
else
docker build --pull -f Dockerfile-alpine -t ${alpineImageName} . --no-cache
fi
if [ "$1" = "--push" ]; then
echo "============================================"
echo "Logging in to registry..."
echo "============================================"
if [ -z "$DOCKER_REGISTRY_USER" ] || [ -z "$DOCKER_REGISTRY_PASSWORD" ]; then
echo "ERROR: Env vars DOCKER_REGISTRY_USER and/or DOCKER_REGISTRY_PASSWORD not set."
echo "Cannot push images, exiting."
exit 1
fi
if [ -z "$DOCKER_REGISTRY" ]; then
echo "WARNING: Env var DOCKER_REGISTRY not set, assuming official docker hub."
docker login -u ${DOCKER_REGISTRY_USER} -p ${DOCKER_REGISTRY_PASSWORD}
else
docker login -u ${DOCKER_REGISTRY_USER} -p ${DOCKER_REGISTRY_PASSWORD} ${DOCKER_REGISTRY}
fi
echo "============================================"
echo "Pushing ${normalImageName}"
echo "============================================"
docker push ${normalImageName}
echo "============================================"
echo "Pushing ${alpineImageName}"
echo "============================================"
docker push ${alpineImageName}
else
if [ ! -z "$1" ]; then
echo "WARNING: Unknown parameter '$1'; did you mean --push?"
fi
fi