This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdeploy.sh
54 lines (45 loc) · 1.72 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
#!/bin/bash
eval "$(
cat .env | awk '!/^\s*#/' | awk '!/^\s*$/' | while IFS='' read -r line; do
key=$(echo "$line" | cut -d '=' -f 1)
value=$(echo "$line" | cut -d '=' -f 2-)
echo "export $key=\"$value\""
done
)"
# Variables
DOCKER_COMPOSE_FILE="./docker-compose.prod.yml"
ENVIRONMENT_FILE="./.env"
KEY_PATH="./ssh/ConnectKey.pem"
REMOTE_PATH="./"
if [ -z "${CLOUD_REMOTE_USER}" ]; then
echo "CLOUD_REMOTE_USER is not set. Exiting.";
exit -1;
fi
if [ -z "${CLOUD_REMOTE_HOST}" ]; then
echo "CLOUD_REMOTE_HOST is not set. Exiting.";
exit -1;
fi
if ssh -i "$KEY_PATH" "$CLOUD_REMOTE_USER@$CLOUD_REMOTE_HOST" "test -e $DOCKER_COMPOSE_FILE"; then
# your file exists
echo "Docker compose exists, stopping"
ssh -i "$KEY_PATH" "$CLOUD_REMOTE_USER@$CLOUD_REMOTE_HOST" "sudo docker-compose -f $DOCKER_COMPOSE_FILE down --rmi all"
scp -i "$KEY_PATH" "$DOCKER_COMPOSE_FILE" "$CLOUD_REMOTE_USER@$CLOUD_REMOTE_HOST":"$REMOTE_PATH"
scp -i "$KEY_PATH" "$ENVIRONMENT_FILE" "$CLOUD_REMOTE_USER@$CLOUD_REMOTE_HOST":"$REMOTE_PATH"
else
# your file doesn't exist
echo "Docker compose file does not exist, copying it"
scp -i "$KEY_PATH" "$DOCKER_COMPOSE_FILE" "$CLOUD_REMOTE_USER@$CLOUD_REMOTE_HOST":"$REMOTE_PATH"
scp -i "$KEY_PATH" "$ENVIRONMENT_FILE" "$CLOUD_REMOTE_USER@$CLOUD_REMOTE_HOST":"$REMOTE_PATH"
fi
# Use SSH to check if the remote directory exists, then copy the file if it does
ssh -i "$KEY_PATH" "$CLOUD_REMOTE_USER@$CLOUD_REMOTE_HOST" bash -c "'
echo \"Starting docker-compose.\"
sudo docker-compose -f $DOCKER_COMPOSE_FILE pull
sudo docker-compose -f $DOCKER_COMPOSE_FILE up -d
'"
if [ $? -eq 0 ]; then
echo "Deployed successfully."
else
echo "Error deploying."
exit 1
fi