|
1 | 1 | # NOTE: The script will try to create the ECR repository if it doesn't exist. Please grant the necessary permissions to the IAM user or role. |
2 | 2 | # Usage: |
3 | | -# cd scripts |
| 3 | +# cd server/scripts |
4 | 4 | # bash ./push-to-ecr.sh |
5 | 5 |
|
6 | 6 | set -o errexit # exit on first error |
7 | 7 | set -o nounset # exit on using unset variables |
8 | 8 | set -o pipefail # exit on any error in a pipeline |
9 | 9 |
|
10 | | -# Define variables |
11 | | -TAG="latest" |
12 | | -ARCHS=("amd64" "arm64") |
13 | | -AWS_REGIONS=("us-east-1") # List of AWS region, use below list if you don't enable ECR repository replication |
14 | | -# AWS_REGIONS=("us-west-2" "us-east-1" "ap-south-1" "ap-southeast-1" "ap-southeast-2" "ap-northeast-1" "ca-central-1" |
15 | | -# "eu-central-1" "eu-west-2" "eu-west-3" "sa-east-1") # List of supported AWS regions |
| 10 | +# Check prerequisites |
| 11 | +echo "================================================" |
| 12 | +echo "SwiftChat - Build and Push to ECR" |
| 13 | +echo "================================================" |
| 14 | +echo "" |
16 | 15 |
|
17 | | -build_and_push_images() { |
| 16 | +echo "Checking prerequisites..." |
| 17 | + |
| 18 | +# Check if Docker is available |
| 19 | +if ! command -v docker &> /dev/null; then |
| 20 | + echo "❌ ERROR: Docker is not installed or not in PATH." |
| 21 | + echo "Please install Docker Desktop or Docker Engine before running this script." |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +# Check if Docker daemon is running |
| 26 | +if ! docker info >/dev/null 2>&1; then |
| 27 | + echo "❌ ERROR: Docker daemon is not running." |
| 28 | + echo "Please start Docker Desktop or Docker daemon before running this script." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +# Check if AWS CLI is available |
| 33 | +if ! command -v aws &> /dev/null; then |
| 34 | + echo "❌ ERROR: AWS CLI is not installed or not in PATH." |
| 35 | + echo "Please install AWS CLI before running this script." |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +# Check if AWS credentials are configured |
| 40 | +if ! aws sts get-caller-identity >/dev/null 2>&1; then |
| 41 | + echo "❌ ERROR: AWS credentials are not configured." |
| 42 | + echo "Please run 'aws configure' or set up your AWS credentials before running this script." |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +echo "✅ All prerequisites are met." |
| 47 | +echo "" |
| 48 | + |
| 49 | +# Prompt user for inputs |
| 50 | + |
| 51 | +# Get repository name |
| 52 | +read -p "Enter ECR repository name (default: swift-chat-api): " REPO_NAME |
| 53 | +REPO_NAME=${REPO_NAME:-swift-chat-api} |
| 54 | + |
| 55 | +# Get image tag |
| 56 | +read -p "Enter image tag (default: latest): " TAG |
| 57 | +TAG=${TAG:-latest} |
| 58 | + |
| 59 | +# Get AWS region |
| 60 | +read -p "Enter AWS region (default: us-east-1): " AWS_REGION |
| 61 | +AWS_REGION=${AWS_REGION:-us-east-1} |
| 62 | + |
| 63 | +echo "" |
| 64 | +echo "Configuration:" |
| 65 | +echo " Repository: $REPO_NAME" |
| 66 | +echo " Image Tag: $TAG" |
| 67 | +echo " AWS Region: $AWS_REGION" |
| 68 | +echo "" |
| 69 | +read -p "Continue with these settings? (y/n): " CONFIRM |
| 70 | +if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then |
| 71 | + echo "Aborted." |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | +echo "" |
| 75 | + |
| 76 | +# Acknowledgment about ECR repository creation |
| 77 | +echo "ℹ️ NOTICE: This script will automatically create ECR repository if it doesn't exist." |
| 78 | +echo " The repository will be created with the following default settings:" |
| 79 | +echo " - Image tag mutability: MUTABLE (allows overwriting tags)" |
| 80 | +echo " - Image scanning: Disabled" |
| 81 | +echo " - Encryption: AES256 (AWS managed encryption)" |
| 82 | +echo "" |
| 83 | +echo " You can modify these settings later in the AWS ECR Console if needed." |
| 84 | +echo " Required IAM permissions: ecr:CreateRepository, ecr:GetAuthorizationToken," |
| 85 | +echo " ecr:BatchCheckLayerAvailability, ecr:InitiateLayerUpload, ecr:UploadLayerPart," |
| 86 | +echo " ecr:CompleteLayerUpload, ecr:PutImage, ecr-public:GetAuthorizationToken" |
| 87 | +echo "" |
| 88 | +read -p "Do you acknowledge and want to proceed? (y/n): " ACK_CONFIRM |
| 89 | +if [[ ! "$ACK_CONFIRM" =~ ^[Yy]$ ]]; then |
| 90 | + echo "Aborted." |
| 91 | + exit 1 |
| 92 | +fi |
| 93 | +echo "" |
| 94 | + |
| 95 | +build_and_push_image() { |
18 | 96 | local IMAGE_NAME=$1 |
19 | 97 | local TAG=$2 |
20 | | - local ENABLE_MULTI_ARCH=${3:-true} # Parameter for enabling multi-arch build, default is true |
21 | | - local DOCKERFILE_PATH=${4:-"../src/Dockerfile"} # Parameter for Dockerfile path, default is "../src/Dockerfile_ecs" |
22 | | - |
23 | | - # Build Docker image for each architecture |
24 | | - if [ "$ENABLE_MULTI_ARCH" == "true" ]; then |
25 | | - for ARCH in "${ARCHS[@]}" |
26 | | - do |
27 | | - # Build multi-architecture Docker image |
28 | | - docker buildx build --platform linux/$ARCH -t $IMAGE_NAME:$TAG-$ARCH -f $DOCKERFILE_PATH --load ../src/ |
29 | | - done |
30 | | - else |
31 | | - # Build single architecture Docker image |
32 | | - docker buildx build --platform linux/${ARCHS[0]} -t $IMAGE_NAME:$TAG -f $DOCKERFILE_PATH --load ../src/ |
| 98 | + local DOCKERFILE_PATH=$3 |
| 99 | + local REGION=$AWS_REGION |
| 100 | + local ARCH="amd64" # Single architecture for simplicity |
| 101 | + |
| 102 | + echo "Logging in to AWS Public ECR..." |
| 103 | + # Log in to AWS Public ECR for pulling base images |
| 104 | + if ! aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws; then |
| 105 | + echo "❌ ERROR: Failed to login to AWS Public ECR. Please check your AWS credentials." |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | + |
| 109 | + echo "Building $IMAGE_NAME:$TAG..." |
| 110 | + |
| 111 | + # Build Docker image |
| 112 | + if ! docker buildx build --platform linux/$ARCH -t $IMAGE_NAME:$TAG -f $DOCKERFILE_PATH --load ../src/; then |
| 113 | + echo "❌ ERROR: Failed to build Docker image." |
| 114 | + exit 1 |
| 115 | + fi |
| 116 | + |
| 117 | + echo "Getting AWS account ID..." |
| 118 | + # Get the account ID |
| 119 | + if ! ACCOUNT_ID=$(aws sts get-caller-identity --region $REGION --query Account --output text 2>/dev/null); then |
| 120 | + echo "❌ ERROR: Failed to get AWS account ID. Please check your AWS credentials and region." |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | + |
| 124 | + # Create repository URI |
| 125 | + REPOSITORY_URI="${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${IMAGE_NAME}" |
| 126 | + |
| 127 | + echo "Creating ECR repository if it doesn't exist..." |
| 128 | + # Create ECR repository if it doesn't exist |
| 129 | + aws ecr create-repository --repository-name "${IMAGE_NAME}" --region $REGION >/dev/null 2>&1 || true |
| 130 | + |
| 131 | + echo "Logging in to ECR..." |
| 132 | + # Log in to ECR |
| 133 | + if ! aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $REPOSITORY_URI; then |
| 134 | + echo "❌ ERROR: Failed to login to ECR. Please check your AWS credentials and permissions." |
| 135 | + exit 1 |
| 136 | + fi |
| 137 | + |
| 138 | + echo "Tagging image for ECR..." |
| 139 | + # Tag the image for ECR |
| 140 | + if ! docker tag $IMAGE_NAME:$TAG $REPOSITORY_URI:$TAG; then |
| 141 | + echo "❌ ERROR: Failed to tag Docker image." |
| 142 | + exit 1 |
33 | 143 | fi |
34 | 144 |
|
35 | | - # Push Docker image to ECR for each architecture in each AWS region |
36 | | - for REGION in "${AWS_REGIONS[@]}" |
37 | | - do |
38 | | - # Get the account ID for the current region |
39 | | - ACCOUNT_ID=$(aws sts get-caller-identity --region $REGION --query Account --output text) |
40 | | - |
41 | | - # Create repository URI |
42 | | - REPOSITORY_URI="${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${IMAGE_NAME}" |
43 | | - |
44 | | - # Create ECR repository if it doesn't exist |
45 | | - aws ecr create-repository --repository-name "${IMAGE_NAME}" --region $REGION || true |
46 | | - |
47 | | - # Log in to ECR |
48 | | - aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $REPOSITORY_URI |
49 | | - |
50 | | - # Push the image to ECR for each architecture |
51 | | - if [ "$ENABLE_MULTI_ARCH" == "true" ]; then |
52 | | - for ARCH in "${ARCHS[@]}" |
53 | | - do |
54 | | - # Tag the image for the current region |
55 | | - docker tag $IMAGE_NAME:$TAG-$ARCH $REPOSITORY_URI:$TAG-$ARCH |
56 | | - # Push the image to ECR |
57 | | - docker push $REPOSITORY_URI:$TAG-$ARCH |
58 | | - # Create a manifest for the image |
59 | | - docker manifest create $REPOSITORY_URI:$TAG $REPOSITORY_URI:$TAG-$ARCH --amend |
60 | | - # Annotate the manifest with architecture information |
61 | | - docker manifest annotate $REPOSITORY_URI:$TAG "$REPOSITORY_URI:$TAG-$ARCH" --os linux --arch $ARCH |
62 | | - done |
63 | | - |
64 | | - # Push the manifest to ECR |
65 | | - docker manifest push $REPOSITORY_URI:$TAG |
66 | | - else |
67 | | - # Tag the image for the current region |
68 | | - docker tag $IMAGE_NAME:$TAG $REPOSITORY_URI:$TAG |
69 | | - # Push the image to ECR |
70 | | - docker push $REPOSITORY_URI:$TAG |
71 | | - fi |
72 | | - |
73 | | - echo "Pushed $IMAGE_NAME:$TAG to $REPOSITORY_URI" |
74 | | - done |
| 145 | + echo "Pushing image to ECR..." |
| 146 | + # Push the image to ECR |
| 147 | + if ! docker push $REPOSITORY_URI:$TAG; then |
| 148 | + echo "❌ ERROR: Failed to push image to ECR." |
| 149 | + exit 1 |
| 150 | + fi |
| 151 | + |
| 152 | + echo "✅ Successfully pushed $IMAGE_NAME:$TAG to $REPOSITORY_URI" |
| 153 | + echo "" |
| 154 | + |
| 155 | + # Return the image URI for later use |
| 156 | + echo "$REPOSITORY_URI:$TAG" |
75 | 157 | } |
76 | 158 |
|
77 | | -build_and_push_images "swift-chat-api" "$TAG" "true" "../src/Dockerfile" |
| 159 | +echo "Building and pushing SwiftChat image..." |
| 160 | +IMAGE_URI=$(build_and_push_image "$REPO_NAME" "$TAG" "../src/Dockerfile") |
| 161 | + |
| 162 | +echo "================================================" |
| 163 | +echo "✅ Image successfully pushed!" |
| 164 | +echo "================================================" |
| 165 | +echo "" |
| 166 | +echo "Your container image URI:" |
| 167 | +echo " $IMAGE_URI" |
| 168 | +echo "" |
| 169 | +echo "Next steps:" |
| 170 | +echo " 1. Download the CloudFormation templates from server/template/ folder" |
| 171 | +echo " 2. Update the ContainerImageUri parameter with your image URI above" |
| 172 | +echo " 3. Deploy the stack via AWS CloudFormation Console" |
| 173 | +echo "" |
0 commit comments