-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-docker-image
executable file
·139 lines (109 loc) · 2.87 KB
/
deploy-docker-image
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
136
137
138
139
#!/usr/bin/env bash
set -e
WORKINGDIR=$(pwd)
BASEDIR=$(dirname $0)
if [ -f "$WORKINGDIR/.env" ]; then
echo "Deployer> Found '.env' file. Using it."
source .env
fi
if [ -f "$WORKINGDIR/package.json" ]; then
echo "Deployer> Found 'package.json' file. Using it."
PKG_NAME=$(jq -r .name package.json)
fi
# --------------------------------------------------------------
# Parse arguments.
#
# Argument parsing code from:
# https://medium.com/@Drew_Stokes/bash-argument-parsing-54f3b81a6a8f
# --------------------------------------------------------------
PARAMS=""
while (( "$#" )); do
case "$1" in
-n|--name)
CLI_NAME=$2
shift 2
;;
-s|--server)
CLI_SERVER=$2
shift 2
;;
--) # End argument parsing.
shift
break
;;
-*|--*=) # Unsupported flags.
echo "Deployer> Error: Unsupported flag $1" >&2
exit 1
;;
*) # Preserve positional arguments.
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
# --------------------------------------------------------------
# Check variables.
# --------------------------------------------------------------
# Search in .env…
# Priority: 1) cli arg, 2) .env var, 3) package.json property.
if [ "$CLI_NAME" ]; then
NAME=$CLI_NAME
elif [ "$DEPLOY_NAME" ]; then
NAME=$DEPLOY_NAME
elif [ "$PKG_NAME" ]; then
NAME=$PKG_NAME
else
echo "Deployer> Error: missing name."
exit 1
fi
if [ "$CLI_SERVER" ]; then
SERVER=$CLI_SERVER
elif [ "$DEPLOY_SERVER" ]; then
SERVER=$DEPLOY_SERVER
else
echo "Deployer> Error: missing server."
exit 1
fi
echo ""
echo "Deployer> Creating '$NAME' image for '$SERVER'…"
# --------------------------------------------------------------
# Create.
# --------------------------------------------------------------
echo ""
echo "Deployer> Building…"
docker build -t "$NAME" .
echo "Deployer> Done."
echo ""
echo "Deployer> Saving…"
docker save "$NAME" -o "./$NAME.dockerimage"
echo "Deployer> Done."
# --------------------------------------------------------------
# Send.
# --------------------------------------------------------------
echo ""
echo "Deployer> Sending…"
rsync -vazh "./$NAME.dockerimage" $SERVER:/tmp
echo "Deployer> Done."
echo ""
echo "Deployer> Cleaning…"
rm "./$NAME.dockerimage"
echo "Deployer> Done."
# --------------------------------------------------------------
# Run.
# --------------------------------------------------------------
echo ""
echo "Deployer> Restarting…"
echo ""
ssh $SERVER "
docker stop $NAME
docker rm $NAME
docker load < /tmp/$NAME.dockerimage
docker run --name $NAME --restart always --env-file /etc/env/$NAME.env --detach $NAME
"
# --------------------------------------------------------------
# End.
# --------------------------------------------------------------
echo ""
echo "Deployer> All done!"