-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·56 lines (40 loc) · 1.28 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
#!/bin/bash
# exit when any command fails
set -e
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap '[ $? -ne 0 ] && echo "\"${last_command}\" command filed with exit code $?."' EXIT
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
cd $DIR
USER=podradioff
SSH_HOST=$USER@$SERVER
BASE=/home/$USER/production
KEEP_RELEASES=2
RELEASEN=$(date -u +%Y%m%d%H%M%S)
echo "Creating release"
ssh $SSH_HOST BASE=$BASE RELEASEN=$RELEASEN 'bash -s' <<'CMD'
mkdir -vp $BASE/releases/$RELEASEN
CMD
rsync -avzPhc --recursive --files-from=deploy.files . $SSH_HOST:$BASE/releases/$RELEASEN/
ssh $SSH_HOST BASE=$BASE KEEP_RELEASES=$KEEP_RELEASES RELEASEN=$RELEASEN 'bash -s' <<'CMD'
# exit when any command fails
set -e
echo "Launching release"
cd $BASE/releases/$RELEASEN
mv docker-compose.prod.yml docker-compose.yml
./containerctl.sh pull
./containerctl.sh up -d --remove-orphans
echo "Linking release"
ln -nfs $BASE/releases/$RELEASEN $BASE/current
cd $BASE/releases
RELEASES=$(ls -1d */ | sort -r)
COUNT=$(echo "$RELEASES" | wc -l)
if [ "$COUNT" -gt "$KEEP_RELEASES" ]; then
echo "Cleaning old releases"
echo "$RELEASES" | tail -n +3 | xargs rm -rvf
fi
CMD
echo
echo "Done!"
echo