backend / scripts /deploy_ecr.sh
Monteeq CI
πŸš€ Deploy from GitHub @ cba5553c1ba43be5e631fd87423a45e1515bfb0b
f1bfaaf
Raw History Blame Contribute Delete
2.34 kB
#!/bin/bash
# Script to build and push backend image to Amazon ECR
# Exit immediately if a command exits with a non-zero status
set -e
# Configuration variables (Modify these to match your AWS environment)
AWS_REGION="${AWS_REGION:-us-west-2}"
AWS_ACCOUNT_ID="${AWS_ACCOUNT_ID:-037302670720}"
REPO_NAME="${REPO_NAME:-monteeq}"
IMAGE_TAG="${IMAGE_TAG:-latest}"
ECR_URL="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
echo "------------------------------------------------"
echo "πŸš€ Preparing ECR deployment for ${REPO_NAME}"
echo "πŸ“ Region: ${AWS_REGION}"
echo "πŸ“ ECR URL: ${ECR_URL}"
echo "------------------------------------------------"
# Resolve aws CLI binary path
AWS_CLI="aws"
if ! command -v aws &> /dev/null; then
if [ -f "$HOME/bin/aws" ]; then
AWS_CLI="$HOME/bin/aws"
else
echo "❌ Error: aws CLI command not found. Please install it or add it to PATH."
exit 1
fi
fi
# Detect if docker requires sudo
DOCKER_CMD="docker"
if ! docker info &>/dev/null; then
DOCKER_CMD="sudo docker"
echo "⚠️ Note: Current user lacks Docker socket permissions. Using 'sudo docker' for container operations."
fi
# Step 1: Login to ECR
echo "πŸ” Logging into Amazon ECR..."
${AWS_CLI} ecr get-login-password --region "${AWS_REGION}" | ${DOCKER_CMD} login --username AWS --password-stdin "${ECR_URL}"
# Step 2: Create ECR repository if it doesn't exist
echo "πŸ“¦ Checking/creating repository ${REPO_NAME} in ECR..."
${AWS_CLI} ecr describe-repositories --repository-names "${REPO_NAME}" --region "${AWS_REGION}" >/dev/null 2>&1 || \
${AWS_CLI} ecr create-repository --repository-name "${REPO_NAME}" --region "${AWS_REGION}"
# Step 3: Build Docker Image
echo "πŸ—οΈ Building Docker image..."
# Run build from backend directory
cd "$(dirname "$0")/.."
${DOCKER_CMD} build -t "${REPO_NAME}" .
# Step 4: Tag Image
echo "🏷️ Tagging image..."
${DOCKER_CMD} tag "${REPO_NAME}:latest" "${ECR_URL}/${REPO_NAME}:${IMAGE_TAG}"
# Step 5: Push Image to ECR
echo "πŸš€ Pushing image to ECR..."
${DOCKER_CMD} push "${ECR_URL}/${REPO_NAME}:${IMAGE_TAG}"
echo "------------------------------------------------"
echo "βœ… Deployment image successfully pushed to ECR!"
echo "🐳 Image URI: ${ECR_URL}/${REPO_NAME}:${IMAGE_TAG}"
echo "------------------------------------------------"