-
Notifications
You must be signed in to change notification settings - Fork 24
/
template_docker_image.sh
executable file
·135 lines (103 loc) · 3.25 KB
/
template_docker_image.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
#!/bin/bash
# rather than change every echo statement...
# alias stdout
exec 3>&1
# option to output everything to stderr
# except things explicitly redirected to 3
if [ -n "$REDIRECT_MESSAGES" ]; then
exec 1>&2
fi
echo "***********************************************************************"
echo "****************** Configuring and Error Checking *********************"
echo "***********************************************************************"
BASENAME="Library"
echo "Image base name set to $BASENAME"
if [ ! -z "$IMAGE_BASE_NAME" ]; then
BASENAME="$IMAGE_BASE_NAME"
echo "Image base name changed to $IMAGE_BASE_NAME"
fi
PROJECTTITLE=${PROJECT_TITLE}
echo "Project title set to $PROJECTTITLE"
if [ ! -z "$DOCKER_PROJECT_TITLE" ]; then
PROJECTTITLE="$DOCKER_PROJECT_TITLE"
echo "Project title changed to $PROJECTTITLE"
fi
BUILD_DIR="./$PROJECTTITLE/app"
echo "Docker build directory set to $BUILD_DIR"
if [ ! -z "$DOCKER_BUILD_DIR" ]; then
BUILD_DIR="./$DOCKER_BUILD_DIR"
echo "Docker build directory changed to $BUILD_DIR"
fi
DOCKER_REPO_SECURITY="private"
if [ "$DOCKER_REPO_TYPE" == "public" ]; then
DOCKER_REPO_SECURITY="public"
fi
echo "Checking for /var/run/docker.sock..."
if [ ! -e /var/run/docker.sock ]; then
echo "*** ERROR! Could not find /var/run/docker.sock"
exit -1
fi
echo "Found."
echo "Checking for /bin/docker..."
if [ ! -e /bin/docker ]; then
echo "*** ERROR! Could not find /bin/docker"
exit -1
fi
echo "Found."
echo "Checking for /home/user/.dockercfg..."
if [ ! -e /home/user/.dockercfg ]; then
echo "*** ERROR! Could not find /home/user/.dockercfg"
exit -1
fi
echo "Found."
echo "Building image..."
echo "Moving into app folder.."
cd "$BUILD_DIR"
DOCKER_FULL_NAME="$BASENAME/$PROJECTTITLE:${PROJECT_BRANCH}-${BUILD_NUMBER}"
echo "Starting docker build at $(date +%H:%M:%S)"
docker build -t "$DOCKER_FULL_NAME" .
CMDRET=$?
echo "Docker build complete at $(date +%H:%M:%S)"
if [ $CMDRET -ne 0 ]; then
echo "*** ERROR! Image build failed with error code $CMDRET"
exit -1
fi
echo "Checking if repo exists..."
CMDRET=`curl -s -o /dev/null -w "%{http_code}" https://registry.hub.docker.com/v1/repositories/${BASENAME}/${PROJECTTITLE}/tags`
if [ $DOCKER_REPO_SECURITY == "public" ]; then
if [ $CMDRET -ne 200 ]; then
echo "*** ERROR! $BASENAME/$PROJECTTITLE does not exist."
echo "A public repo must be manually created in order to push"
exit -1
fi
else
if [ $CMDRET -eq 200 ]; then
echo "*** ERROR! $BASENAME/$PROJECTTITLE exists as a public repo"
echo "*** Cannot push a private build to a public repo, aborting"
exit -1
fi
fi
TRY=0
MAXTRIES=3
DELAY=10
CMDRET=1
while [ $MAXTRIES -ne $TRY ] && [ $CMDRET -ne 0 ]; do
if [ $TRY -gt 0 ]; then
echo "Push failed. Sleeping $DELAY seconds before trying again."
sleep $DELAY
fi
echo "Starting docker push attempt number ${TRY} at $(date +%H:%M:%S)"
docker push -f "$DOCKER_FULL_NAME"
CMDRET=$?
: $(( TRY++ ))
done
if [ $CMDRET -eq 0 ]; then
echo "Docker push complete at $(date +%H:%M:%S)"
echo "Removing local copy of image: $DOCKER_FULL_NAME"
docker rmi "$DOCKER_FULL_NAME"
else
echo "*** ERROR! Docker push failed"
exit 1
fi
# finally, output the full name of container
echo "$DOCKER_FULL_NAME" 1>&3