-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·76 lines (63 loc) · 3.21 KB
/
deploy.sh
File metadata and controls
executable file
·76 lines (63 loc) · 3.21 KB
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
# Exit on any error
set -e
#TODOs
# Tag the commit with the deploy environment. Check in. Will that break something if there's other changes on the vine?
# Checkout the tag at the far end, ready for a docker-enabled account to do the rest.
# Investigate if 'docker compose build' is better than 'docker build'
# Check whether the docker-compose directory, and the symlink to the compose file, already exists. Handle if not.
# There's a potential sequencing bug, where if something fails after the checkout (e.g image build), then the live symlink to the docker-compose-### is pointing to the 'wrong' file. One fix is to copy the file instead of symlink. This opens new permissions questions.
# add django unit tests (runtests) check to non-dev deployment steps.
# vars
REMOTE_SERVER="feeldsparror.cubecinema.com"
TOOLKIT_USER="toolkit" ## run checkout tasks as toolkit, requires entry in authorized_keys.
TOOLKIT_REMOTE="$TOOLKIT_USER@$REMOTE_SERVER"
TOOLKIT_BASE_DIR="/home/$TOOLKIT_USER"
# Get the current commit hash
COMMIT_HASH=$(git rev-parse --short=10 HEAD)
echo '''
This is your current commit:
------------------------------------------------------------------
'''
git log -1 "$COMMIT_HASH"
echo '''
------------------------------------------------------------------
'''
GOOD_OPT=false
while ! $GOOD_OPT; do
read -p "If you want to deploy the above commit, type an environment name (staging/production) or type 'exit' (or just Ctrl+C) to end without deploying.
" DEPLOY_ENV
case $DEPLOY_ENV in
staging | production | exit)
GOOD_OPT=true
;;
esac
if [ "$DEPLOY_ENV" = 'exit' ]; then
echo 'Laters.'
exit
fi
if ! $GOOD_OPT; then
echo "We're being quite specific on this one. The response has to match *exactly*."
fi
done
# for now, create a git archive copy that over, unpack it, then remove the .tgz files
ARCHIVE_FILE="$DEPLOY_ENV"_code_export.tgz
CHECKOUT_DIR="$TOOLKIT_BASE_DIR/checkout/$DEPLOY_ENV"
DOCKER_COMPOSE_DIR="/opt/stacks/toolkit-$DEPLOY_ENV"
echo "***************** build and send $DEPLOY_ENV archive *****************"
git archive --format=tgz -o "$ARCHIVE_FILE" "$COMMIT_HASH"
rsync -avz --delete ./"$ARCHIVE_FILE" "$TOOLKIT_REMOTE:$TOOLKIT_BASE_DIR/tmp"
rm ./"$ARCHIVE_FILE"
# unpack
echo "***************** unpack $DEPLOY_ENV archive *****************"
ssh "$TOOLKIT_REMOTE" "if [ -d '${CHECKOUT_DIR}' ]; then rm -Rf '$CHECKOUT_DIR'; fi"
ssh "$TOOLKIT_REMOTE" "if [ ! -d '${CHECKOUT_DIR}' ]; then mkdir '$CHECKOUT_DIR'; fi"
ssh "$TOOLKIT_REMOTE" "tar -xzf '$TOOLKIT_BASE_DIR'/tmp/'$ARCHIVE_FILE' -C '$CHECKOUT_DIR'"
echo Done.
# docker build
# run docker ops as own account. Requires the TOOLKIT group to access files and DOCKER group to run docker.
# pass in hardcoded UID of the toolkit user to simplify bind mount things. TODO: If someone wants to make this fancy and dynamic go for it
echo "***************** build $DEPLOY_ENV image *****************"
ssh "$REMOTE_SERVER" "cd '$CHECKOUT_DIR' && docker build --build-arg ENV_NAME=$DEPLOY_ENV --build-arg TOOLKIT_UID=1004 --tag toolkit:'$DEPLOY_ENV' ."
echo "***************** start $DEPLOY_ENV container(s) *****************"
ssh "$REMOTE_SERVER" "cd '$DOCKER_COMPOSE_DIR' && docker compose up --detach"