-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.sh
executable file
·125 lines (103 loc) · 4.23 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
#!/bin/bash
set -e
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
git log -1 --decorate=short > git_last_commit
currentBranch=$(git rev-parse --abbrev-ref HEAD)
if [[ -n "${BRANCH_NAME}" ]]; then
echo "============================================"
echo "INFO: Detected BRANCH_NAME env var from Jenkins, using that"
currentBranch=${BRANCH_NAME}
fi
echo "============================================"
echo "INFO: Running on branch ${currentBranch}"
echo ${currentBranch} > git_branch
function hasBranch {
local testBranch; testBranch=$1
if [ -z "$(git branch -r | sed 's/^..//' | grep origin/${testBranch})" ]; then
return 1
fi
return 0
}
function resolveBranch {
local testBranch; testBranch=$1
local fallback1; fallback1=next
local fallback2; fallback2=master
if hasBranch ${testBranch}; then
echo ${testBranch}
return 0
elif hasBranch ${fallback1}; then
echo ${fallback1}
return 0
elif hasBranch ${fallback2}; then
echo ${fallback2}
return 0
fi
return 1
}
echo "============================================"
echo "INFO: Checking out wicked.node-sdk and setting correct branch"
rm -rf sdk-tmp
mkdir -p sdk-tmp
pushd sdk-tmp > /dev/null
git clone https://github.com/apim-haufe-io/wicked.node-sdk
pushd wicked.node-sdk
sdkBranch=$(resolveBranch ${currentBranch})
echo "INFO: Using branch ${sdkBranch} of wicked.node-sdk"
git checkout ${sdkBranch}
echo "INFO: Packing node SDK into wicked-sdk.tgz"
cp -f ../../build/Dockerfile-build-sdk ./Dockerfile
docker build -t wicked-node-sdk:${currentBranch} . --no-cache
echo "INFO: Copying wicked-sdk.tgz from builder image"
docker create --name wicked-node-sdk-${currentBranch} wicked-node-sdk:${currentBranch} > /dev/null
docker cp wicked-node-sdk-${currentBranch}:/usr/src/app/wicked-sdk.tgz ../../wicked-sdk.tgz
echo "INFO: Cleaning up..."
docker rm -f wicked-node-sdk-${currentBranch} > /dev/null
docker rmi wicked-node-sdk:${currentBranch} > /dev/null
echo ""
echo "INFO: Resulting wicked-sdk.tgz:"
ls -la ../../wicked-sdk.tgz
popd > /dev/null # wicked.node-sdk
popd > /dev/null # sdk-tmp
echo "============================================"
echo "Building normal image..."
echo "============================================"
docker build --pull -t ${DOCKER_PREFIX}env:${DOCKER_TAG}-onbuild --no-cache .
echo "============================================"
echo "Building alpine image..."
echo "============================================"
docker build --pull -f Dockerfile-alpine -t ${DOCKER_PREFIX}env:${DOCKER_TAG}-onbuild-alpine --no-cache .
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 ${DOCKER_PREFIX}env:${DOCKER_TAG}-onbuild"
echo "============================================"
docker push ${DOCKER_PREFIX}env:${DOCKER_TAG}-onbuild
echo "============================================"
echo "Pushing ${DOCKER_PREFIX}env:${DOCKER_TAG}-onbuild-alpine"
echo "============================================"
docker push ${DOCKER_PREFIX}env:${DOCKER_TAG}-onbuild-alpine
else
if [ ! -z "$1" ]; then
echo "WARNING: Unknown parameter '$1'; did you mean --push?"
fi
fi