#!/bin/bash set -e # Exit immediately if a command fails # --- Configuration --- APP_DIR="/home/ubuntu/itinerary-generator" cd ${APP_DIR} export CONTAINER_NAME="ec2-api-doc-tool-generator" # 1. Get environment name from CodeDeploy APP_ENV=${DEPLOYMENT_GROUP_NAME} if [[ -z "$APP_ENV" ]]; then echo "ERROR: DEPLOYMENT_GROUP_NAME is not set." exit 1 fi # 2. Read the image tag if [[ -f "image-tag.txt" ]]; then export IMAGE_TAG=$(cat image-tag.txt) else echo "ERROR: image-tag.txt not found" exit 1 fi echo "=== Deployment Variables ===" echo "Project Name (APP_ENV)=${APP_ENV}" echo "Container Name=${CONTAINER_NAME}" echo "Image Tag=${IMAGE_TAG}" # 3. ✅ CREATE THE .env FILE FIRST echo "=== Creating .env file ===" PARAM_PATH="/api-doc-tool-generator" echo "AWS_PARAMETER_STORE_PATH=${PARAM_PATH}" > .env echo "APP_ENV=${APP_ENV}" >> .env echo "Created .env file with SSM Path: ${PARAM_PATH}" # 4. Login to AWS ECR echo "=== Login AWS ECR ===" aws ecr get-login-password --region ap-southeast-3 | docker login --username AWS --password-stdin 825765383758.dkr.ecr.ap-southeast-3.amazonaws.com # 5. Stop and Pull echo "=== Stopping containers ===" # ✅ FIX: Use 'docker-compose.yaml' docker-compose -p "${APP_ENV}" -f docker-compose.yaml down || true echo "=== Pulling images ===" # ✅ FIX: Use 'docker-compose.yaml' docker-compose -p "${APP_ENV}" -f docker-compose.yaml pull # 6. Start Containers echo "=== Starting containers ===" # ✅ FIX: Use 'docker-compose.yaml' docker-compose -p "${APP_ENV}" -f docker-compose.yaml up -d # 7. Prune echo "=== Prune unused images ===" docker image prune -af echo "=== Deployment completed successfully ==="