-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.sh
76 lines (61 loc) · 1.98 KB
/
deploy.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
#!/bin/bash
##
## This script takes one paramater - the target DHP environment. If not specified, dev is the default.
[[ -n "$1" ]] && env=$1 || env=dev
echo "Deploying for $env"
source docker-env.sh
if [ -z "$BUILD_NUMBER" ] ;
then
build_number=0
else
build_number=$BUILD_NUMBER
fi
# Find the running container, if there is one, and save off the container id and image
running_container=`docker ps --filter name=${app_name} --format {{.ID}}`
if [ -n "${running_container}" ] ;
then
running_image=`docker ps -f name=${app_name} --format {{.Image}}`
echo "DOCKER: Container ${running_container} based on image ${running_image} was running."
docker stop ${running_container}
fi
current_container=`docker ps -a --filter name=${app_name} --format {{.ID}}`
if [ -n "${current_container}" ] ;
then
echo "DOCKER: Removing Container ${current_container}."
docker rm ${current_container}
fi
# Remove old containers and images
old_images=`docker images ${app_name} --format "{{.Repository}}:{{.Tag}}\t{{.Tag}}" |sort -n -k2 -r |tail -n +4`
echo "Old images: $old_images"
# set linefeed as the field separator IFS in the next two for loops
(
IFS='
'
for i in $old_images;
do
old_image_name=`echo $i | awk '{print $1}'`
old_containers=`docker ps -a -q --filter "ancestor=${old_image_name}"`
for c in $old_containers;
do
echo "DOCKER: Removing Old Docker Container $c"
docker rm $c
done
echo "DOCKER: Removing Old Docker Image ${old_image_name}"
docker rmi ${old_image_name}
done
)
#Create the new image
image_name=${app_name}:${build_number}
echo "DOCKER: Creating new image ${image_name}"
docker build -t ${image_name} .
echo "DOCKER: Starting Container ${app_name}"
#Start a container with this image
./create-container.sh $image_name
if [ $? -eq 0 ]
then
echo "Successfully deployed."
exit 0
else
echo "Errors encountered while deploying the app."
exit 1
fi