-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy_activate.sh
59 lines (48 loc) · 1.73 KB
/
deploy_activate.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
#!/bin/bash
# Please export following variables:
# SSH_USER
# SSH_PORT
# SSH_PRIVATE_KEY
# SSH_HOSTKEYS (i.e. copied from your local ~/.ssh/known_hosts)
# SERVER_PATH
# WARMUP_SCRIPT (optional)
# ACTIVATE_SCRIPT (optional)
# ...and as CLI-parameter: $0 hosts...
# assert ENV
[ -z "$SSH_PORT" ] && SSH_PORT="22"
[ -z "$SSH_USER" ] && echo "SSH_USER empty" && exit 1
[ -z "$SSH_PRIVATE_KEY" ] && echo "SSH_PRIVATE_KEY empty" && exit 1
[ -z "$SSH_HOSTKEYS" ] && echo "SSH_HOSTKEYS empty" && exit 1
[ -z "$SERVER_PATH" ] && echo "SERVER_PATH empty" && exit 1
[ -z "$WARMUP_SCRIPT" ] && echo "[INFO]: WARMUP_SCRIPT empty"
[ -z "$ACTIVATE_SCRIPT" ] && echo "[INFO]: ACTIVATE_SCRIPT empty"
[ $# -eq 0 ] && echo "No deploy target(s) -- Syntax: $0 host1 host2 host3 ..." && exit 1
echo ">>> Starting ssh-agent"
# run ssh-agent in background
eval $(ssh-agent -s)
ssh-add <(echo "$SSH_PRIVATE_KEY")
echo ">>> Add remote host key to known_hosts"
mkdir -p ~/.ssh
echo "$SSH_HOSTKEYS" > ~/.ssh/known_hosts
echo ">>> Start Deployment..."
for SSH_HOST in "$@"
do
echo ">>> Prepare dir: $SSH_HOST:$SERVER_PATH"
ssh -p$SSH_PORT $SSH_USER@$SSH_HOST "mkdir -p $SERVER_PATH" || exit 1
echo ">>> Copy files to $SSH_HOST"
scp -P$SSH_PORT -r $CI_PROJECT_DIR/* $SSH_USER@$SSH_HOST:$SERVER_PATH || exit 1
if [[ ! -z "$WARMUP_SCRIPT" ]]; then
echo ">>> Run script on remote host: $SSH_HOST > $WARMUP_SCRIPT"
ssh -p$SSH_PORT $SSH_USER@$SSH_HOST "$WARMUP_SCRIPT" || exit 1
fi
done
if [[ ! -z "$ACTIVATE_SCRIPT" ]]; then
echo ">>> Activate new version..."
RESULT=0
for SSH_HOST in "$@"
do
echo ">>> Run script on remote host: $SSH_HOST > $ACTIVATE_SCRIPT"
ssh -p$SSH_PORT $SSH_USER@$SSH_HOST "$ACTIVATE_SCRIPT" || RESULT=1
done
fi
exit $RESULT