-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
78 lines (68 loc) · 1.84 KB
/
run.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
#!/bin/sh
export DOCKER_SCAN_SUGGEST=false
stop_timeout=10
need_build=false
need_start=false
option1="$1"
option2="$2"
set -e;
function echo_title {
line=$(echo "$1" | sed -r 's/./-/g')
printf "\n$line\n$1\n$line\n\n"
}
function has_option {
if [ "$option1" == "$1" ] || [ "$option2" == "$1" ] ||
[ "$option1" == "$2" ] || [ "$option2" == "$2" ] ; then
echo "true"
else
echo "false"
fi
}
# goto script directory
pushd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" > /dev/null
tag=$(cat Dockerfile | grep -oP 'cicd="\K\w+' | tail -1)
if [ -z "$tag" ] ; then
printf "\nNo cicd LABEL found in Dockerfile.\n\n"
exit 1
fi
if [ $(has_option "--force" "-f") == "true" ] ; then
need_pull=true
else
need_pull=$(git fetch --dry-run 2>&1)
fi
if [ -n "$need_pull" ] ; then
echo_title "PULLING LATEST SOURCE CODE"
git reset --hard
git pull
git log --pretty=oneline -1
need_build=true
elif [ -z "$(docker images | grep $tag || true)" ] ; then
need_build=true
fi
status=$(docker-compose --env-file .env ps --status running -q)
if [ "$need_build" == true ] ; then
if [ ! -z "$status" ] ; then
echo_title "STOPPING RUNNING CONTAINER"
docker-compose stop -t $stop_timeout
fi
need_start=true
elif [ -z $status ] ; then
need_start=true
fi
if [ "$need_start" == false ] ; then
printf "\nNo changes found. Container is already running.\n"
elif [ "$need_build" == true ]; then
echo_title "BUILDING & STARTING CONTAINER"
docker-compose --env-file .env up -d --build
else
echo_title "STARTING CONTAINER"
docker-compose --env-file .env up -d
fi
if [ $(has_option "--full_cleanup" "-fcu") == "true" ] ; then
echo_title "FULL CLEAN-UP"
docker image prune --force
elif [ $(has_option "--cleanup" "-cu") == "true" ] ; then
echo_title "CLEAN-UP"
docker image prune --force --filter "label=cicd=$tag"
fi
echo ""