-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployer.sh
86 lines (64 loc) · 2.23 KB
/
deployer.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
else
echo "Error: .env file not found!"
exit 1
fi
USERNAME="$USERNAME"
TOKEN="$TOKEN"
SCRIPT_DIR="generated-script"
mkdir -p "$SCRIPT_DIR"
read -p "Choose build type (PM2/DOCKER): " BUILD_TYPE
read -p "Enter repository folder name: " REPO_DIR
if [[ "$BUILD_TYPE" == "pm2" || "$BUILD_TYPE" == "PM2" ]]; then
read -p "Enter application name for PM2: " APP_NAME
SCRIPT_NAME=${SCRIPT_NAME:-backend-deploy-$REPO_DIR.sh}
cat <<EOL > $SCRIPT_DIR/$SCRIPT_NAME
#!/bin/bash
USERNAME="$USERNAME"
TOKEN="$TOKEN"
REPO_DIR="$REPO_DIR"
cd "\$REPO_DIR" || { echo "Repository directory not found"; exit 1; }
echo "Pulling latest changes..."
GIT_ASKPASS=\$(mktemp)
echo "#!/bin/bash" > \$GIT_ASKPASS
echo "echo '\$TOKEN'" >> \$GIT_ASKPASS
chmod +x \$GIT_ASKPASS
GIT_ASKPASS=\$GIT_ASKPASS git pull --no-ff
rm \$GIT_ASKPASS
echo "Building the project..."
npm run build || { echo "Build failed"; exit 1; }
echo "Restarting the application..."
pm2 restart $APP_NAME || { echo "PM2 restart failed"; exit 1; }
echo "Pull, build, and restart process completed successfully!"
EOL
chmod +x $SCRIPT_DIR/$SCRIPT_NAME
echo "Deployment script for $REPO_DIR has been generated: $SCRIPT_DIR/$SCRIPT_NAME"
elif [[ "$BUILD_TYPE" == "docker" || "$BUILD_TYPE" == "DOCKER" ]]; then
SCRIPT_NAME=${SCRIPT_NAME:-backend-deploy-$REPO_DIR.sh}
cat <<EOL > $SCRIPT_DIR/$SCRIPT_NAME
#!/bin/bash
USERNAME="$USERNAME"
TOKEN="$TOKEN"
REPO_DIR="$REPO_DIR"
cd "\$REPO_DIR" || { echo "Repository directory not found"; exit 1; }
echo "Pulling latest changes..."
GIT_ASKPASS=\$(mktemp)
echo "#!/bin/bash" > \$GIT_ASKPASS
echo "echo '\$TOKEN'" >> \$GIT_ASKPASS
chmod +x \$GIT_ASKPASS
GIT_ASKPASS=\$GIT_ASKPASS git pull --no-ff
rm \$GIT_ASKPASS
echo "Stopping Docker containers..."
sudo docker compose down || { echo "Docker down failed"; exit 1; }
echo "Starting Docker containers..."
sudo docker compose up -d || { echo "Docker up failed"; exit 1; }
echo "Pull, build, and restart process completed successfully!"
EOL
chmod +x $SCRIPT_DIR/$SCRIPT_NAME
echo "Deployment Docker script for $REPO_DIR has been generated: $SCRIPT_DIR/$SCRIPT_NAME"
else
echo "Invalid build type. Please choose either PM2 or DOCKER."
exit 1
fi