-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
64 lines (49 loc) · 2.29 KB
/
deploy.sh
File metadata and controls
64 lines (49 loc) · 2.29 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
#!/bin/bash
#APP_DIR="${APP_DIR:-/home/ubuntu/app}"
APP_DIR="$(pwd)"
APP_NAME=tgbot-gpt
LOG_FILE="/var/log/$APP_NAME-deploy.log"
sudo chown ubuntu:ubuntu "$LOG_FILE"
echo "$(date '+%Y-%m-%d %H:%M:%S') Starting deploy script execution" >> "$LOG_FILE"
ENV_FILE=".env"
echo "Create $ENV_FILE file" >> "$LOG_FILE"
> $ENV_FILE
sudo chown ubuntu:ubuntu $ENV_FILE
echo "Retrieving $ENV_FILE from SSM Parameter Store..." >> "$LOG_FILE"
SSM_PARAMETER_ENV_FILE="/$APP_NAME/dot_env"
aws ssm get-parameter --name "$SSM_PARAMETER_ENV_FILE" --with-decryption --query Parameter.Value --output text > "$ENV_FILE"
if [ $? -ne 0 ]; then
echo "Failed to retrieve $ENV_FILE from SSM. Check IAM permissions and parameter name." >> "$LOG_FILE"
exit 1
fi
DOCKER_COMPOSE_FILE="compose.yaml"
echo "Create $DOCKER_COMPOSE_FILE file" >> "$LOG_FILE"
> $DOCKER_COMPOSE_FILE
sudo chown ubuntu:ubuntu $DOCKER_COMPOSE_FILE
echo "Retrieving $DOCKER_COMPOSE_FILE from SSM Parameter Store..." >> "$LOG_FILE"
SSM_PARAMETER_DOCKER_COMPOSE_FILE="/$APP_NAME/docker_compose_yml"
aws ssm get-parameter --name "$SSM_PARAMETER_DOCKER_COMPOSE_FILE" --with-decryption --query Parameter.Value --output text > "$DOCKER_COMPOSE_FILE"
if [ $? -ne 0 ]; then
echo "Failed to retrieve $DOCKER_COMPOSE_FILE from SSM. Check IAM permissions and parameter name." >> "$LOG_FILE"
exit 1
fi
echo "--- $(date) ---"
echo "Starting deployment..." >> "$LOG_FILE"
echo "Application directory: $APP_DIR" >> "$LOG_FILE"
cd "$APP_DIR" || { echo "Error: Cannot change directory to $APP_DIR. Exiting." >> "$LOG_FILE"; exit 1; }
echo "Stopping existing Docker Compose services..." >> "$LOG_FILE"
sudo -u ubuntu docker compose down -v --remove-orphans || true
if [ $? -ne 0 ]; then
echo "Warning: docker compose down encountered issues, but continuing. Check logs if needed." >> "$LOG_FILE"
fi
echo "Existing services stopped." >> "$LOG_FILE"
echo "Pull the latest image from ECR" >> "$LOG_FILE"
sudo -u ubuntu docker compose pull
echo "Starting new Docker Compose services..." >> "$LOG_FILE"
sudo -u ubuntu docker compose up -d --force-recreate
if [ $? -ne 0 ]; then
echo "Error: Failed to start Docker Compose services. Exiting." >> "$LOG_FILE"
exit 1
fi
echo "Application deployed and running." >> "$LOG_FILE"
echo "Deployment script finished successfully." >> "$LOG_FILE"