Skip to content

Commit ae9ac56

Browse files
authored
Dynamically load port
1 parent 8225951 commit ae9ac56

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

β€ŽScript.shβ€Ž

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,41 @@
22

33
echo "πŸš€ Starting deployment process..."
44

5-
# Define variables
5+
CONFIG_REPO_PATH="/home/weberqbot/infra-config"
6+
CONFIG_REPO_URL="git@github.com:weberq/infra-config.git"
7+
8+
# Clone or update the infra-config repo
9+
if [ ! -d "$CONFIG_REPO_PATH/.git" ]; then
10+
echo "πŸ“₯ Cloning infra-config repo..."
11+
git clone "$CONFIG_REPO_URL" "$CONFIG_REPO_PATH"
12+
else
13+
echo "πŸ“¦ Updating infra-config repo..."
14+
cd "$CONFIG_REPO_PATH" && git pull
15+
fi
16+
17+
# Define environment and project
18+
ENVIRONMENT="dev1" # Change this per server
19+
PROJECT_NAME="stm.weberq.in"
20+
21+
# Path to port registry
22+
PORT_REGISTRY="$CONFIG_REPO_PATH/$ENVIRONMENT/port_registry.txt"
23+
24+
# Load the assigned port for this project
25+
PORT=$(grep "^$PROJECT_NAME=" "$PORT_REGISTRY" | cut -d'=' -f2)
26+
27+
if [ -z "$PORT" ]; then
28+
echo "❌ Port not defined for $PROJECT_NAME in $PORT_REGISTRY. Exiting."
29+
exit 1
30+
fi
31+
32+
echo "ℹ️ Using port $PORT for $PROJECT_NAME..."
33+
34+
# Define project details
635
PROJECT_DIR="/home/weberqbot/orbits/stm.org"
7-
CONTAINER_NAME="stm.weberq.in"
8-
IMAGE_NAME="stm-image"
36+
CONTAINER_NAME="$PROJECT_NAME"
37+
IMAGE_NAME="${PROJECT_NAME//./-}-image"
938

10-
echo "πŸ“ Changing to project directory..."
39+
echo "πŸ“ Navigating to project directory..."
1140
cd "$PROJECT_DIR" || { echo "❌ Failed to change directory."; exit 1; }
1241

1342
echo "πŸ“¦ Pulling latest changes from Git..."
@@ -19,39 +48,36 @@ git pull || {
1948
}
2049
}
2150

22-
# Detect changes that require a Docker rebuild
23-
echo "πŸ” Checking for changes that require a Docker rebuild..."
51+
# Check for rebuild necessity
2452
CHANGED_FILES=$(git diff --name-only HEAD@{1} HEAD)
25-
2653
if echo "$CHANGED_FILES" | grep -qE 'Dockerfile|composer.json|composer.lock'; then
27-
echo "πŸ“¦ Changes detected in Dockerfile or composer files. Rebuilding image..."
54+
echo "πŸ“¦ Changes detected that require Docker rebuild."
2855

29-
echo "🧹 Cleaning up old Docker container and image (if any)..."
56+
echo "🧹 Stopping and cleaning old container/image..."
3057
docker stop $CONTAINER_NAME 2>/dev/null || echo "No running container to stop."
3158
docker rm $CONTAINER_NAME 2>/dev/null || echo "No container to remove."
32-
docker rmi $IMAGE_NAME 2>/dev/null || echo "No old image to remove."
59+
docker rmi $IMAGE_NAME 2>/dev/null || echo "No image to remove."
3360

3461
echo "🐳 Building Docker image..."
3562
docker build -t $IMAGE_NAME . || { echo "❌ Docker build failed."; exit 1; }
3663

37-
echo "🚒 Starting Docker container..."
38-
docker run -d -p 8081:80 --name $CONTAINER_NAME $IMAGE_NAME || {
64+
echo "🚒 Starting new Docker container on port $PORT..."
65+
docker run -d -p $PORT:80 --name $CONTAINER_NAME $IMAGE_NAME || {
3966
echo "❌ Failed to start container."; exit 1
4067
}
4168

42-
echo "βœ… Deployment complete with full rebuild!"
4369
else
44-
echo "βœ… No changes requiring Docker rebuild. Syncing updated code into container..."
70+
echo "βœ… No rebuild required. Syncing code..."
4571

46-
echo "πŸ“¦ Copying updated code into running container..."
72+
# Sync code into the container
4773
docker cp . $CONTAINER_NAME:/var/www/html || {
4874
echo "❌ Failed to copy updated files to container."; exit 1;
4975
}
5076

51-
echo "πŸ” Restarting container to reflect changes..."
77+
echo "πŸ” Restarting container to apply changes..."
5278
docker restart $CONTAINER_NAME || {
5379
echo "❌ Failed to restart container."; exit 1;
5480
}
55-
56-
echo "βœ… Code sync complete without rebuild!"
5781
fi
82+
83+
echo "βœ… Deployment complete!"

0 commit comments

Comments
Β (0)